【EasyPan】项目常见问题解答(自用&持续更新中…)汇总版
一、HTTP请求处理流程
1. 控制器入口 /checkCode
@RequestMapping("/checkCode")
public void checkCode(HttpServletResponse response, HttpSession session, Integer type) throws IOException {
// 实例化验证码生成器(130x38像素,5字符,10干扰线)
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
// 设置HTTP响应头禁止缓存
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
// 获取生成的验证码
String code = vCode.getCode();
// 根据type参数决定存储到不同session域
if (type == null || type == 0) {
session.setAttribute(Constants.CHECK_CODE_KEY, code);
} else {
session.setAttribute(Constants.CHECK_CODE_KEY_EMAIL, code);
}
// 输出图片流到客户端
vCode.write(response.getOutputStream());
}
二、验证码生成核心流程
1. CreateImageCode
构造函数
public CreateImageCode(int width, int height, int codeCount, int lineCount) {
this.width = width; // 设置图片宽度
this.height = height; // 设置图片高度
this.codeCount = codeCount;// 设置验证码字符数
this.lineCount = lineCount;// 设置干扰线数量
createImage(); // 立即生成图片
}
2. 图片生成方法 createImage()
详细步骤:
-
初始化画布
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics g = buffImg.getGraphics();
-
绘制背景
g.setColor(getRandColor(200, 250)); // 浅色背景 g.fillRect(0, 0, width, height);
-
绘制干扰线
for (int i = 0; i < lineCount; i++) { // 随机生成线段起止坐标 g.setColor(getRandColor(1, 255)); g.drawLine(xs, ys, xe, ye); }
-
添加噪点
for (int i = 0; i < area; i++) { buffImg.setRGB(x, y, random.nextInt(255)); }
-
生成并绘制验证码
String str1 = randomStr(codeCount); this.code = str1; for (int i = 0; i < codeCount; i++) { g.drawString(strRand, i * fontWidth + 3, codeY); }
三、辅助方法实现
1. 随机字符串生成
private String randomStr(int n) {
String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxzy1234567890";
String str2 = "";
for (int i = 0; i < n; i++) {
double r = (Math.random()) * (str1.length() - 1);
str2 += str1.charAt((int) r);
}
return str2;
}
2. 随机颜色生成
private Color getRandColor(int fc, int bc) {
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);
}
四、输出与获取方法
1. 图片输出
public void write(OutputStream sos) throws IOException {
ImageIO.write(buffImg, "png", sos); // 注意实际输出PNG格式
sos.close();
}
2. 验证码获取
public String getCode() {
return code.toLowerCase(); // 统一返回小写
}
五、关键参数说明
参数名 | 默认值 | 作用描述 |
---|---|---|
width | 160 | 验证码图片宽度(像素) |
height | 40 | 验证码图片高度(像素) |
codeCount | 4 | 验证码字符数量 |
lineCount | 20 | 干扰线数量 |
yawpRate | 0.01f | 噪点密度(占图片面积比例) |
六、注意事项
- 格式不一致:响应头声明
image/jpeg
但实际输出PNG格式 - 资源释放:
OutputStream
应在finally块中关闭 - 线程安全:多个请求共享
Random
实例可能存在竞争 - 字符池优化:建议移除易混淆字符(如0/O,1/l等)
/**
* 获取验证码
*/
@RequestMapping("/checkCode")
public void checkCode(HttpServletResponse response, HttpSession session, Integer type) throws IOException {
CreateImageCode vCode = new CreateImageCode(130, 38, 5, 10);
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
String code = vCode.getCode();
if (type == null || type == 0) {
session.setAttribute(Constants.CHECK_CODE_KEY, code);
} else {
session.setAttribute(Constants.CHECK_CODE_KEY_EMAIL, code);
}
vCode.write(response.getOutputStream());
}
public class CreateImageCode {
//图片的宽度
private int width = 160;
//图片的高度
private int height = 40;
//验证码字符个数
private int codeCount = 4;
//验证码干扰线数
private int lineCount = 20;
//验证码
private String code = null;
//验证码图片Buffer
private BufferedImage buffImg = null;
Random random = new Random();
public CreateImageCode(){
createImage();
}
public CreateImageCode(int width, int height){
this.width = width;
this.height = height;
createImage();
}
public CreateImageCode(int width, int height, int codeCount, int lineCount){
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
createImage();
}
//生成图片
private void createImage(){
int fontWidth = width / codeCount; //字体的宽度
int fontHeight = height - 5; //字体的高度
int codeY = height - 8;
//图像buffer
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
Graphics g = buffImg.getGraphics();
//Graphics2D g = buffImg.createGraphics();
//设置背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
//设置字体
//Font font1 = getFont(fontHeight);
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
g.setFont(font);
//设置干扰线
for (int i = 0; i < lineCount; i++) {
int xs = random.nextInt(width);
int ys = random.nextInt(height);
int xe = xs + random.nextInt(width);
int ye = ys + random.nextInt(height);
g.setColor(getRandColor(1, 255));
g.drawLine(xs, ys, xe, ye);
}
//添加噪点
float yawpRate = 0.01f; //噪声率
int area = (int) (yawpRate * width * height);
for (int i = 0; i < area; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
buffImg.setRGB(x, y, random.nextInt(255));
}
String str1 = randomStr(codeCount); //得到随机字符
this.code = str1;
for (int i = 0; i < codeCount; i++) {
String strRand = str1.substring(i, i + 1);
g.setColor(getRandColor(1, 255));
//g.drawString(a, x, y);
//a为要画出来的东西,x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的 (x,y)位置处
g.drawString(strRand, i * fontWidth + 3, codeY);
}
}
//得到随机字符
private String randomStr(int n) {
String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxzy1234567890";
String str2 = "";
int len = str1.length() - 1;
double r;
for (int i = 0; i < n; i++) {
r = (Math.random()) * len;
str2 = str2 + str1.charAt((int) r);
}
return str2;
}
//得到随机颜色
private Color getRandColor(int fc, int bc) { //给定范围获得随机颜色
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);
}
//产生随机字体
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)];
}
public void write(OutputStream sos) throws IOException {
ImageIO.write(buffImg, "png", sos);
sos.close();
}
public BufferedImage getBuffImg(){
return buffImg;
}
public String getCode(){
return code.toLowerCase();
}
}