专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java生成图片验证码的代码

Java生成图片验证码的代码

更新时间:2022-10-19 10:52:54 来源:动力节点 浏览875次

Java生成图片验证码的代码是什么?动力节点小编来告诉大家。 

package com.ws.frame.utils;
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;
import javax.servlet.http.HttpSession;
/**
* Random verification code generation rules , For page anti brute force cracking 
* @author YINGFU
*/
public class Captcha {
public static final String SESSION_KEY = "CAPTCHA"; // session The property name 
private static final int WIDTH = 80; // Image width 
private static final int HEIGHT = 26; // Picture height 
private static final int LINESIZE = 8; // The number of interference lines in the picture 
private static final int NUMBER = 4; // The number of characters produced 
private static final int FONTSIZE = 18; // Picture text size 
private static final int R = 255;
private static final int G = 255;
private static final int B = 255;
private static final int DEFAULT_FONT_STYLE = Font.CENTER_BASELINE; // Default style 
private static final String DEFAULT_FONT_FAMILY = Font.SANS_SERIF; // default font
/**
* Random number generation pool 
*/
private static final char[] CHARSPOOL = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z' };
private Random random = new Random();
/**
* Generate random characters 
* @return string
*/
private char randomCode(){
int length = CHARSPOOL.length;
int num = random.nextInt(length);
return CHARSPOOL[num];
}
/**
* Style random colors 
* @return color
*/
private Color colorStyle(){
int r = random.nextInt(R);
int g = random.nextInt(G);
int b = random.nextInt(B);
return new Color(r, g, b);
}
/**
* Set the basic style of the font 
* @return font
*/
private Font fontStyle(){
// by linux Server usage 
System.setProperty("java.awt.headless", "true");
return new Font(DEFAULT_FONT_FAMILY, DEFAULT_FONT_STYLE, FONTSIZE);
}
/**
* Set the color of the text 
* @return
*/
private Color fontColorStyle(){
int r = random.nextInt(100);
int g = random.nextInt(100);
int b = random.nextInt(100);
return new Color(r, g, b);
}
/**
* Drawing random numbers 
* @param g
* @param i
* @return char
*/
private char drawRandomCode(Graphics g, int i){
g.setFont(fontStyle());
// g.setColor(colorStyle());
g.setColor(fontColorStyle());
char randomCode = randomCode();
g.translate(random.nextInt(3) , random.nextInt(3));
g.drawString(String.valueOf(randomCode), 13 * i, 16);
return randomCode;
}
/**
* Draw interference line 
* @param g
*/
private void drawLine(Graphics g){
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
int x2 = random.nextInt(WIDTH/2);
int y2 = random.nextInt(HEIGHT/2);
g.drawLine(x, y, x2, y2);
}
/**
* Generate verification code 
* @param session
* @param response
*/
public void buildCaptcha(HttpSession session, HttpServletResponse response){
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setFont(fontStyle());
g.setColor(colorStyle());
for(int i=0;i<LINESIZE;i++){
drawLine(g);
}
StringBuilder code = new StringBuilder();
for(int i=1; i<= NUMBER; i++){
code.append(drawRandomCode(g, i));
}
session.removeAttribute(SESSION_KEY);
session.setAttribute(SESSION_KEY, code.toString());
g.dispose();
try {
ImageIO.write(image, "JPEG", response.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
}

图片字体和画线的颜色是随机生成的。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>