/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.htl.other;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.captcha.Captcha;
import org.jboss.seam.captcha.CaptchaResponse;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
@Name("org.jboss.seam.captcha.captcha")
@Scope(ScopeType.SESSION)
@Install(precedence = Install.APPLICATION)
public class Coder extends Captcha {
/**
*
*/
private static final long serialVersionUID = 4353904503753742537L;
private Color backgroundColor = new Color(0xf5,0xf5, 0xf5);
private Font textFont = new Font("verdana", Font.PLAIN, 30);
private int charsToPrint = 4;
private int width = 100;
private int height = 30;
private int circlesToDraw = 4;
private float horizMargin = 10.0f;
private double rotationRange = 0.2;
private String elegibleChars = "123456789";
private char[] chars = elegibleChars.toCharArray();
@Override
@Create
public void init() {
super.init();
StringBuffer finalString = new StringBuffer();
for (int i = 0; i < charsToPrint; i++) {
double randomValue = Math.random();
int randomIndex = (int) Math.round(randomValue * (chars.length - 1));
char characterToShow = chars[randomIndex];
finalString.append(characterToShow);
}
setChallenge(finalString.toString());
setCorrectResponse(finalString.toString());
}
@Override
public BufferedImage renderChallenge() {
// Background
BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
g.setColor(backgroundColor);
g.fillRect(0, 0, width, height);
// Some obfuscation circles
for (int i = 0; i < circlesToDraw; i++) {
int circleColor = 80 + (int)(Math.random() * 70);
float circleLinewidth = 0.3f + (float)(Math.random());
g.setColor(new Color(circleColor, circleColor, circleColor));
g.setStroke(new BasicStroke(circleLinewidth));
int circleRadius = (int) (Math.random() * height / 2.0);
int circleX = (int) (Math.random() * width - circleRadius);
int circleY = (int) (Math.random() * height - circleRadius);
g.drawOval(circleX, circleY, circleRadius * 2, circleRadius * 2);
}
// Text
g.setFont(textFont);
FontMetrics fontMetrics = g.getFontMetrics();
int maxAdvance = fontMetrics.getMaxAdvance();
int fontHeight = fontMetrics.getHeight();
float spaceForLetters = -horizMargin * 2 + width;
float spacePerChar = spaceForLetters / (charsToPrint - 1.0f);
char[] allChars = getChallenge().toCharArray();
for (int i = 0; i < allChars.length; i++ ) {
char charToPrint = allChars[i];
int charWidth = fontMetrics.charWidth(charToPrint);
int charDim = Math.max(maxAdvance, fontHeight);
int halfCharDim = (charDim / 2);
BufferedImage charImage = new BufferedImage(charDim, charDim, BufferedImage.TYPE_INT_ARGB);
Graphics2D charGraphics = charImage.createGraphics();
charGraphics.translate(halfCharDim, halfCharDim);
double angle = (Math.random() - 0.5) * rotationRange;
charGraphics.transform(AffineTransform.getRotateInstance(angle));
charGraphics.translate(-halfCharDim, -halfCharDim);
int charColor = 60 + (int)(Math.random() * 90);
charGraphics.setColor(new Color(charColor, charColor, charColor));
charGraphics.setFont(textFont);
int charX = (int) (0.5 * charDim - 0.5 * charWidth);
charGraphics.drawString("" + charToPrint, charX, ((charDim - fontMetrics.getAscent())/2 + fontMetrics.getAscent()));
float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
int y = ((height - charDim) / 2);
g.drawImage(charImage, (int) x, y, charDim, charDim, null, null);
charGraphics.dispose();
}
g.dispose();
return bufferedImage;
}
@CaptchaResponse(message = "校验码错误,请重新输入!")
public String getResponse() {
return super.getResponse();
}
}
想刷新页面需要在Page的页面中加入
<action execute="#{captcha.init()}"/>
分享到:
相关推荐
简单的验证码图片点击后实现图片刷新,并且进行输入框失去焦点后验证输入是否正确。
asp验证码-带刷新-最简洁的验证码代码.rar ASP验证非常优秀的ASP随机验证码.rar Classic-ASP-and-Google-Captcha-main.zip Classic-ASP-and-Google-reCAPTCHA-master.zip classic-asp-captcha-master.zip
今天在做验证码的时候发现在IE中,验证码不会刷新,而谷歌等其他浏览器没有问题,所以我想到应该是缓存问题,因为IE默认的设置是如果访问地址没变化就不会去获取而是加载缓存中的内容 所以解决方案就是在验证码的...
本文实例讲述了thinkPHP5.0框架验证码调用及点击图片刷新实现方法。分享给大家供大家参考,具体如下: 1、配置文件中增加验证码配置 'captcha' => [ // 验证码字符集合 'codeSet' => '23456789', // 验证码字体...
下列代码都是以自己的项目实例讲述的,相关的文本...主目录/utils/captcha/__init__.py import random import string # Image:一个画布 # ImageDraw:一个画笔 # ImageFont:画笔的字体 from PIL import Image, ImageDr
今天在做验证码的时候发现在IE中,验证码不会刷新,而谷歌等其他浏览器没有问题,所以我想到应该是缓存问题,因为IE默认的设置是如果访问地址没变化就不会去获取而是加载缓存中的内容 所以解决方案就是在验证码的...
Django Rest验证码django-simple-captcha轻量级版本,可与django-rest-framework 。特征速度:使用cache代替数据库安全性:用于生成密钥和图像的联合方法。 (一个键不能生成很多图像) 简单:只需一个rest api...
captcha-demo 制作一个web页面的验证码: 利用cavas实现验证码的显示,点击刷新 利用input输入框去做对比 实现一个简单的验证码功能
验证码图片 ImageView,相当实用使用:xml<test.jinesh.captcha.CaptchaImageView android:layout_width="wrap_content" android:id="@ id/image" android:layout_weight="6" android:layout_margin="5...
本文实例讲述了ThinkPHP5&5.1实现验证码的生成、使用及点击刷新功能。分享给大家供大家参考,具体如下: 验证码现在是用户登录、支付等很多环节的必备元素,ThinkPHP5&5.1给我们提供了验证码的生成方式,也是非常的...
验证码机制在CI框架中是通过一个辅助函数captcha()进行实现的——验证码辅助函数文件包含了一些帮助你创建验证码图片的函数。。 那么我们如何使用CI的captcha()辅助函数来完成验证码功能呢?下面我会先讲述如何使用C...
扩展将自动检测页面中的CAPTCHAS,但您必须选择正确的CAPTCHA图像和正确的CAPTCHA字段,然后刷新浏览器以便自动解决CAPTCHA。 这是多么容易...... 对于reCAPTCHA,只需安装此扩展名,您的reCAPTCHA将自动快速解决!...
Django Simple Captcha Django Simple Captcha是一个非常简单但高度可定制的Django应用程序,用于将Captcha图像添加到任何Django表单中。 功能非常易于设置和部署,但也很容易配置Django Simple Captcha Django ...
图形验证码(gregwar/captcha 包生成), 企业微信扫码登录, 见 vue-php-admin-V3 [X] 9. 以 restful 风格重新构建代码 20200401 ,GET/POST/PUT/DELETE [ ] 10. 界面主题优化 [ ] 11. 完全弃用 CI 自带数据库操作,...
由于计算机无法解答CAPTCHA的问题,所以回答出问题的用户就可以被认为是人类。 主要作用于:登录 和 注册 功能技术亮点:2,密码加密 MD5是哈希散列算法,对于MD5而言,有两个特性是很重要的,第一:明文数据经过...
语言:English autofills在Vit学术界页面... - 在Vit学生和家长登录(https://vtopbeta.vit.ac .in / vtop,https://vtopbeta.vit.ac.in/vtop/parentlogin)和付款门户 *更新为最新的CAPTCHA * *适用于刷新验证码*