验证码生成java_生成验证码

[java]代码库import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.IOException;

import java.util.Random;

import javax.imageio.ImageIO;

import javax.servlet.http.HttpServletResponse;

public class RandImgCreater {

//默认字符列表

private static final String CODE_LIST = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";

//验证码图片高度

private static final int HEIGHT = 20;

//默认字符数

private static final int FONT_NUM = 4;

//生成验证码图片存放位置

private HttpServletResponse response;

//宽度

private int width = 0;

//实际字符个数

private int iNum = 0;

//实际字符列表

private String codeList = "";

//绘制背景

private boolean drawBgFlag = false;

//R

private int rBg = 0;

//G

private int gBg = 0;

//B

private int bBg = 0;

//构造函数(自定义存放位置)

public RandImgCreater(HttpServletResponse response) {

this.response = response;

this.width = 13 * FONT_NUM + 12;

this.iNum = FONT_NUM;

this.codeList = CODE_LIST;

}

//构造函数(自定义存放位置,字符数)

public RandImgCreater(HttpServletResponse response,int iNum) {

this.response = response;

this.width = 13 * iNum + 12;

this.iNum = iNum;

this.codeList = CODE_LIST;

}

//构造函数(自定义存放位置,字符数,字符列表)

public RandImgCreater(HttpServletResponse response, int iNum,

String codeList) {

this.response = response;

this.width = 13 * iNum + 12;

this.iNum = iNum;

this.codeList = codeList;

}

//绘制验证码

public String createRandImage() {

//1.生成BufferedImage对象,存放图片

BufferedImage image = new BufferedImage(width, HEIGHT,

BufferedImage.TYPE_INT_RGB);

//2.获得BufferedImage对象的画笔

Graphics g = image.getGraphics();

//生成随机数生成器

Random random = new Random();

//开始绘制背景

if (drawBgFlag) {

//设置画笔颜色

g.setColor(new Color(rBg, gBg, bBg));

//绘制矩形

g.fillRect(0, 0, width, HEIGHT);

} else {

//设置画笔颜色(随机)

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

//绘制矩形

g.fillRect(0, 0, width, HEIGHT);

//循环155次生成随机干扰线条

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

//设置颜色

g.setColor(getRandColor(140, 200));

//起始位置x

int x = random.nextInt(width);

//起始位置y

int y = random.nextInt(HEIGHT);

//结束位置x1

int xl = random.nextInt(12);

//结束位置y1

int yl = random.nextInt(12);

//绘制线条

g.drawLine(x, y, x + xl, y + yl);

}

}

//设置画笔字体、样式、大小

g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

//要返回的字符串

String sRand = "";

//循环内随机生成字符

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

//随机生成字符起始位置

int rand = random.nextInt(codeList.length());

//从字符起始位置截取1位字符

String strRand = codeList.substring(rand, rand + 1);

//把新生成的字符与旧字符连接(返回用)

sRand += strRand;

//设置前景色(字体颜色)

g.setColor(new Color(20 + random.nextInt(110), 20 + random

.nextInt(110), 20 + random.nextInt(110)));

//绘制字符,第一个字符从0位置开始,每个字符相隔13像素

g.drawString(strRand, 13 * i + 6, 16);

}

//释放资源

g.dispose();

try {

//把图片输出到相应位置

ImageIO.write(image, "JPEG", response.getOutputStream());

} catch (IOException e) {

}

//返回验证码的字符

return sRand;

}

//设置背景色(R,G,B颜色)

public void setBgColor(int r, int g, int b) {

drawBgFlag = true;

this.rBg = r;

this.gBg = g;

this.bBg = b;

}

//随机生成颜色

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);

}

}

[代码运行效果截图]

8c1c31c226a1d68eca8aad17086b7965.png

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值