blob: 689b8cea0e710e7a134159ab1a2f5b0b8892c3a9 [file] [log] [blame]
22301115cf6dba22025-03-25 19:06:21 +08001package com.example.myproject.utils;
2
3
4import javax.imageio.ImageIO;
5import java.awt.*;
6import java.awt.image.BufferedImage;
7import java.io.FileNotFoundException;
8import java.io.IOException;
9import java.io.OutputStream;
10import java.util.Random;
11
12import javax.imageio.ImageIO;
13import java.awt.*;
14import java.awt.image.BufferedImage;
15import java.io.IOException;
16import java.io.OutputStream;
17import java.util.Random;
18
19/*
20import javax.imageio.ImageIO;
21import java.awt.*;
22import java.awt.image.BufferedImage;
23import java.io.IOException;
24import java.io.OutputStream;
25import java.util.Random;
26
27/**
28 * @author 阿楠
29 * code生成工具类
30 */
31public class VerifyCode {
32 /**
33 * 生成验证码图片的宽度
34 */
35 private int width = 100;
36
37 /**
38 * 生成验证码图片的高度
39 */
40 private int height = 30;
41
42 /**
43 * 字符样式
44 */
45 private String[] fontNames = { "宋体", "楷体", "隶书", "微软雅黑" };
46
47 /**
48 * 定义验证码图片的背景颜色为白色
49 */
50 private Color bgColor = new Color(255, 255, 255);
51
52 /**
53 * 生成随机
54 */
55 private Random random = new Random();
56
57 /**
58 * 定义code字符
59 */
60 private String codes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
61
62 /**
63 * 记录随机字符串
64 */
65 private String text;
66
67 /**
68 * 获取一个随意颜色
69 * @return
70 */
71 private Color randomColor() {
72 int red = random.nextInt(150);
73 int green = random.nextInt(150);
74 int blue = random.nextInt(150);
75 return new Color(red, green, blue);
76 }
77
78 /**
79 * 获取一个随机字体
80 *
81 * @return
82 */
83 private Font randomFont() {
84 String name = fontNames[random.nextInt(fontNames.length)];
85 int style = random.nextInt(4);
86 int size = random.nextInt(5) + 24;
87 return new Font(name, style, size);
88 }
89
90 /**
91 * 获取一个随机字符
92 *
93 * @return
94 */
95 private char randomChar() {
96 return codes.charAt(random.nextInt(codes.length()));
97 }
98
99 /**
100 * 创建一个空白的BufferedImage对象
101 *
102 * @return
103 */
104 private BufferedImage createImage() {
105 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
106 Graphics2D g2 = (Graphics2D) image.getGraphics();
107 //设置验证码图片的背景颜色
108 g2.setColor(bgColor);
109 g2.fillRect(0, 0, width, height);
110 return image;
111 }
112
113 public BufferedImage getImage() {
114 BufferedImage image = createImage();
115 Graphics2D g2 = (Graphics2D) image.getGraphics();
116 StringBuffer sb = new StringBuffer();
117 for (int i = 0; i < 4; i++) {
118 String s = randomChar() + "";
119 sb.append(s);
120 g2.setColor(randomColor());
121 g2.setFont(randomFont());
122 float x = i * width * 1.0f / 4;
123 g2.drawString(s, x, height - 8);
124 }
125 this.text = sb.toString();
126 drawLine(image);
127 return image;
128 }
129
130 /**
131 * 绘制干扰线
132 *
133 * @param image
134 */
135 private void drawLine(BufferedImage image) {
136 Graphics2D g2 = (Graphics2D) image.getGraphics();
137 int num = 5;
138 for (int i = 0; i < num; i++) {
139 int x1 = random.nextInt(width);
140 int y1 = random.nextInt(height);
141 int x2 = random.nextInt(width);
142 int y2 = random.nextInt(height);
143 g2.setColor(randomColor());
144 g2.setStroke(new BasicStroke(1.5f));
145 g2.drawLine(x1, y1, x2, y2);
146 }
147 }
148
149 public String getText() {
150 return text;
151 }
152
153 public static void output(BufferedImage image, OutputStream out) throws IOException {
154 ImageIO.write(image, "JPEG", out);
155 }
156}