java实现空心验证码_Java实现验证码

这篇博客介绍了如何使用Java生成空心验证码。首先定义了图片的尺寸和验证码的位数,然后通过创建BufferedImage对象并设置背景颜色。接着,利用Graphics对象绘制干扰线和噪点。博主使用了特定字符集,随机生成验证码并将其颜色和位置随机化。最后,将生成的验证码保存到session,并输出为JPG图片。
摘要由CSDN通过智能技术生成

public void getYZM(HttpServletRequest request, HttpServletResponse response){

//验证码图片宽度

final int IMG_WIDTH = 116;

//验证码图片高度

final int IMG_HEIGHT = 36;

//验证码位数

final int CODE_LEN = 5;

// 验证码干扰线数

int lineCount = 20;

Random random = new Random();

//用于绘制图片,设置图片的长宽和图片类型

BufferedImage bi = new BufferedImage(IMG_WIDTH,IMG_HEIGHT,BufferedImage.TYPE_INT_RGB);

//获取绘图工具

Graphics graphics = bi.getGraphics();

//设置背景颜色

graphics.setColor(getRandColor(200, 250));

//填充矩形区域

graphics.fillRect(0,0,IMG_WIDTH,IMG_HEIGHT);

//设置字体

Font font = new Font("Fixedsys", Font.BOLD, 30);

graphics.setFont(font);

// 设置干扰线

for (int i = 0; i < lineCount; i++) {

int xs = random.nextInt(IMG_WIDTH);

int ys = random.nextInt(IMG_HEIGHT);

int xe = xs + random.nextInt(IMG_WIDTH);

int ye = ys + random.nextInt(IMG_HEIGHT);

graphics.setColor(getRandColor(1, 255));

graphics.drawLine(xs, ys, xe, ye);

}

// 添加噪点

float yawpRate = 0.01f;// 噪声率

int area = (int) (yawpRate * IMG_WIDTH * IMG_HEIGHT);

for (int i = 0; i < area; i++) {

int x = random.nextInt(IMG_WIDTH);

int y = random.nextInt(IMG_HEIGHT);

bi.setRGB(x, y, random.nextInt(255));

}

//验证码使用到的字符

char[] codeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456".toCharArray();

//存放生产的验证码

String captcha = "";

//循环绘制验证码

for(int i=0;i

int index = random.nextInt(codeChars.length);

//s随机生生成验证码颜色

graphics.setColor(getRandColor(1, 255));

//将字符绘制到图片

graphics.drawString(codeChars[index]+"",(i*20)+15,20);

captcha += codeChars[index];

}

//将生辰的验证码放入session

request.getSession().setAttribute("code",captcha);

//输出图片

try {

ImageIO.write(bi,"JPG",response.getOutputStream());

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 产生随机字体

*/

private Font getFont(int size) {

Random random = new Random();

Font font[] = new Font[5];

font[0] = new Font("Ravie", Font.PLAIN, size);

font[1] = new Font("Antique Olive Compact", Font.PLAIN, size);

font[2] = new Font("Fixedsys", Font.PLAIN, size);

font[3] = new Font("Wide Latin", Font.PLAIN, size);

font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, size);

return font[random.nextInt(5)];

}

// 得到随机颜色

private Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色

Random random = new Random();

if (fc > 255)

fc = 255;

if (bc > 255)

bc = 255;

int r = fc + random.nextInt(bc - fc);

int g = fc + random.nextInt(bc - fc);

int b = fc + random.nextInt(bc - fc);

return new Color(r, g, b);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值