-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathVerifyCode.java
More file actions
108 lines (96 loc) · 2.61 KB
/
VerifyCode.java
File metadata and controls
108 lines (96 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.kevinblandy.simple.webchat.utils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
/**
* Created by KevinBlandy on 2017/1/10 14:29 验证码生成类
*/
public class VerifyCode {
private int w = 70;
private int h = 30;
private Random r = new Random();
private String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312" };
private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ";
private Color bgColor = new Color(255, 255, 255);
private String text;
public VerifyCode() {
}
/**
* 构造初始化图片宽高 默认 30 x 70
*
* @param width
* @param height
*/
public VerifyCode(int width, int height) {
if (width <= 0 || width > 100) {
width = 70;
}
if (height <= 0 || height > 100) {
height = 30;
}
this.w = width;
this.h = height;
}
private Color randomColor() {
int red = r.nextInt(150);
int green = r.nextInt(150);
int blue = r.nextInt(150);
return new Color(red, green, blue);
}
private Font randomFont() {
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];
int style = r.nextInt(4);
int size = r.nextInt(4) + 24;
return new Font(fontName, style, size);
}
private void drawLine(BufferedImage image) {
int num = 3;
Graphics2D g2 = (Graphics2D) image.getGraphics();
for (int x = 0; x < num; x++) {
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(Color.BLUE);
g2.drawLine(x1, y1, x2, y2);
}
}
private char randomChar() {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
private BufferedImage createImage() {
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_BGR);
Graphics2D g2 = (Graphics2D) image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, w, h);
return image;
}
public BufferedImage getImage() {
BufferedImage image = createImage();
Graphics2D g2 = (Graphics2D) image.getGraphics();
StringBuilder sb = new StringBuilder();
for (int x = 0; x < 4; x++) {
String s = randomChar() + "";
sb.append(s);
float f = x * 1.0F * w / 4;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, f, h - 5);
}
this.text = sb.toString();
drawLine(image);
return image;
}
public String getText() {
return text;
}
public static void output(BufferedImage image, OutputStream out) throws IOException {
ImageIO.write(image, "JPEG", out);
}
}