Update CreateVerifiCodeImage.java

main
pveayojnc 4 months ago
parent f670e5d2f9
commit ec15593a84

@ -10,34 +10,46 @@ import java.util.Random;
*/
public class CreateVerifiCodeImage {
// 定义验证码图片的宽度为 90 像素
private static int WIDTH = 90;
// 定义验证码图片的高度为 35 像素
private static int HEIGHT = 35;
private static int FONT_SIZE = 20; //字符大小
private static char[] verifiCode; //验证码
private static BufferedImage verifiCodeImage; //验证码图片
// 定义验证码字符的大小为 20
private static int FONT_SIZE = 20;
// 用于存储生成的验证码字符数组
private static char[] verifiCode;
// 用于存储生成的验证码图片对象
private static BufferedImage verifiCodeImage;
/**
* @description:
* @param: no
* @return: java.awt.image.BufferedImage
* @param:
* @return: java.awt.image.BufferedImage
*/
public static BufferedImage getVerifiCodeImage() {
verifiCodeImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);// create a image
// 创建一个指定宽度、高度和图像类型TYPE_INT_BGR的 BufferedImage 对象
verifiCodeImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);
// 获取图片的 Graphics 对象,用于绘制图形和文本
Graphics graphics = verifiCodeImage.getGraphics();
// 生成验证码字符数组
verifiCode = generateCheckCode();
// 绘制验证码图片的背景
drawBackground(graphics);
// 在图片上绘制生成的验证码字符
drawRands(graphics, verifiCode);
// 释放 Graphics 对象所占用的系统资源
graphics.dispose();
// 返回生成的验证码图片
return verifiCodeImage;
}
/**
* @description:
* @param: no
* @return: char[]
* @param:
* @return: char
*/
public static char[] getVerifiCode() {
return verifiCode;
@ -45,63 +57,79 @@ public class CreateVerifiCodeImage {
/**
* @description:
* @param: no
* @return: char[]
* @param:
* @return: 4 char
*/
private static char[] generateCheckCode() {
// 定义包含数字、小写字母和大写字母的字符集字符串
String chars = "0123456789abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 创建一个长度为 4 的字符数组,用于存储生成的验证码字符
char[] rands = new char[4];
// 循环 4 次,每次生成一个随机字符并存储到字符数组中
for (int i = 0; i < 4; i++) {
// 生成一个随机索引,范围是 0 到字符集长度减 1
int rand = (int) (Math.random() * (10 + 26 * 2));
// 根据随机索引从字符集中获取一个字符,并存储到字符数组中
rands[i] = chars.charAt(rand);
}
// 返回生成的验证码字符数组
return rands;
}
/**
* @description:
* @param: g
* @param: rands
* @return: void
* @param: g Graphics
* @param: rands
* @return:
*/
private static void drawRands(Graphics g, char[] rands) {
// 设置绘制文本的字体为 Console 字体,加粗,大小为 FONT_SIZE
g.setFont(new Font("Console", Font.BOLD, FONT_SIZE));
// 遍历验证码字符数组,将每个字符绘制到图片上
for (int i = 0; i < rands.length; i++) {
// 获取一个随机颜色
g.setColor(getRandomColor());
// 在指定的 x 和 y 坐标位置绘制验证码字符
g.drawString("" + rands[i], i * FONT_SIZE + 10, 25);
}
}
/**
* @description:
* @param: g
* @return: void
* @param: g Graphics
* @return:
*/
private static void drawBackground(Graphics g) {
// 设置绘制颜色为白色
g.setColor(Color.white);
// 使用白色填充整个图片区域
g.fillRect(0, 0, WIDTH, HEIGHT);
// 绘制验证码干扰点
// 绘制 200 个干扰点
for (int i = 0; i < 200; i++) {
// 生成一个随机的 x 坐标
int x = (int) (Math.random() * WIDTH);
// 生成一个随机的 y 坐标
int y = (int) (Math.random() * HEIGHT);
// 获取一个随机颜色
g.setColor(getRandomColor());
// 在指定的 x 和 y 坐标位置绘制一个 1x1 的椭圆(即一个点)
g.drawOval(x, y, 1, 1);
}
}
/**
* @description:
* @param: no
* @return: java.awt.Color
* @param:
* @return: java.awt.Color
*/
private static Color getRandomColor() {
// 创建一个 Random 对象,用于生成随机数
Random ran = new Random();
// 根据随机生成的红、绿、蓝分量值创建一个颜色对象并返回
return new Color(ran.nextInt(220), ran.nextInt(220), ran.nextInt(220));
}
}
Loading…
Cancel
Save