Update CreateVerifiCodeImage.java

main
pveayojnc 4 months ago
parent f670e5d2f9
commit ec15593a84

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