验证码有很多开源组件,但还是参考网上自己实现了一个验证码,是很早之前写的,很简单:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;
/**
*
* 功能:验证码图片生成器
* Version: 1.0
* Author: DR.YangLong
* Date: 2013/6/13
* Time: 19:49
* Email:410357434@163.com
* Editor:
*/
public class ValidateImg {
private static Logger logger= LoggerFactory.getLogger(ValidateImg.class);
// 图片的宽度。
private int width = 120;
// 图片的高度。
private int height = 35;
// 验证码字符个数
private int codeCount = 4;
// 验证码干扰线数
private int lineCount = 80;
// 验证码
private String code = null;
// 验证码图片Buffer
private BufferedImage buffImg = null;
private char[] codeSequence = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z','0','1' ,'2', '3', '4', '5', '6', '7', '8', '9'};
// 生成随机数
private Random random = new Random();
public ValidateImg() {
this.createCode();
}
/**
* @param width 图片宽
* @param height 图片高
*/
public ValidateImg(int width, int height) {
this.width = width;
this.height = height;
this.createCode();
}
/**
* @param width 图片宽
* @param height 图片高
* @param codeCount 字符个数
* @param lineCount 干扰线条数
*/
public ValidateImg(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
this.createCode();
}
public void createCode() {
int codeX = 0;
int fontHeight = 0;
fontHeight = height - 8;// 字体的高度
codeX = width / (codeCount + 1);// 每个字符的宽度
// 图像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
// 将图像填充为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 创建字体
ImgFontByte imgFont = new ImgFontByte();
Font font = imgFont.getFont(fontHeight);
g.setFont(font);
// 绘制干扰线
for (int i = 0; i < lineCount; i++) {
int xs = getRandomNumber(width);
int ys = getRandomNumber(height);
int xe = xs + getRandomNumber(width / 8);
int ye = ys + getRandomNumber(height / 8);
g.setColor(getRandomColor());
g.drawLine(xs, ys, xe, ye);
}
StringBuffer randomCode = new StringBuffer();
// 随机产生验证码字符
for (int i = 0; i < codeCount; i++) {
String strRand = String.valueOf(codeSequence[random
.nextInt(codeSequence.length)]);
// 设置字体颜色
g.setColor(getRandomColor());
// 设置字体位置
g.drawString(strRand, (i + 1) * codeX,
getRandomNumber(height / 2) + 20);
randomCode.append(strRand);
}
code = randomCode.toString();
}
/**
* 获取随机颜色
*/
private Color getRandomColor() {
int r = getRandomNumber(255);
int g = getRandomNumber(255);
int b = getRandomNumber(255);
return new Color(r, g, b);
}
/**
* 获取随机数
*/
private int getRandomNumber(int number) {
return random.nextInt(number);
}
public void write(String path) throws IOException {
OutputStream sos = new FileOutputStream(path);
this.write(sos);
}
public void write(OutputStream sos) throws IOException {
ImageIO.write(buffImg, "png", sos);
sos.close();
}
//获取图片缓存
public BufferedImage getBuffImg() {
return buffImg;
}
//获取验证码
public String getCode() {
return code;
}
/**
* 字体样式类
*/
class ImgFontByte {
public Font getFont(int fontHeight) {
String path = ValidateImg.class.getClassLoader().getResource("").getPath()+File.separator+"fontstyle"+File.separator+"Multicolore.otf";
try {
File file = new File(path);
FileInputStream ios = new FileInputStream(file);
Font baseFont = Font.createFont(Font.TRUETYPE_FONT, ios);
return baseFont.deriveFont(Font.PLAIN, fontHeight);
} catch (Exception e) {
logger.error("获取字体文件出错,使用默认字体!\n位置:validate.ImgFontByte,\n错误信息:\n"+e.getMessage()+
"\n程序查找位置:"+path);
return new Font("Arial", Font.PLAIN, fontHeight);
}
}
}
}
分享到:
相关推荐
网页验证码实现jar
该组件主要是针对网页安全,介绍验证码的实现
易语言取网页验证码图片,实现及时抓取网页上的验证码。
简单的验证码实现 public static void generateTokenImage(HttpServletResponse response, HttpSession session, int width, int height) throws IOException { //设置响应内容为图片格式 response.setContentType...
PHP实例教程-网页登陆验证码的实现,通常验证码做的不好看,这段程序教你做一个好看且实用的验证码。
一个简单的实现网页验证码图片识别并转换为文本程序,工程在VS2010下编译通过,可以识别网页上字母和数字等,基本上只要不是很复杂的校验码都能识别成功 哦!
JavaScript实现页面动态验证码
web项目实现验证码判断登录、注册功能
实现了网页登陆,其他注册所需要的验证码的功能。实现了网页登陆,其他注册所需要的验证码的功能。实现了网页登陆,其他注册所需要的验证码的功能
java动态验证码Struts2集成实现
本文实例讲述了Bootstrap实现前端登录页面带验证码功能。分享给大家供大家参考,具体如下: Bootstrap有自定义的验证码样式,在前端页面可以直接使用,他的css、js在使用前要在开头引入 上代码: <!DOCTYPE ...
用java实现动态验证码的效果,源代码可以直接拷贝到项目中用
主要为大家详细介绍了java实现网页验证码功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
实现网页短信验证码登录,平台使用的是秒滴平台,序言去此平台注册
Asp.net(C#)实现验证码功能
MyEclipse实现页面验证码,是最简单的入门级页面验证码
java jsp实现网页上验证码产生功能,简单易懂。
网站验证码如何下载到本地是进行验证码识别的第一步,本示例程序完整实现了网页验证码的保存,为后续验证码识别打下基础。
首先需pip install Pillow==3.4.1,然后自行配置应用中的urls和views,资源为验证码的源码及显示的html网页,已实现看不清换一张功能
本资源实现一个简单的会员注册页面,使用到html、css、js,运用FormGroup的各类型实现用户名、密码、邮箱、生日、...运用js实现验证码的功能 配套博文:https://blog.csdn.net/air__Heaven/article/details/112696644