大家好,又见面了,我是你们的朋友全栈君。
1 packagecom.jiangbei.verifycodeutils;2
3 importjava.awt.BasicStroke;4 importjava.awt.Color;5 importjava.awt.Font;6 importjava.awt.Graphics2D;7 importjava.awt.image.BufferedImage;8 importjava.io.IOException;9 importjava.io.OutputStream;10 importjava.util.Random;11
12 importjavax.imageio.ImageIO;13
14 public classVerifyCode {15
16 private int w = 70;17
18 private int h = 35;19
20 private Random r = newRandom();21
22 //{“宋体”, “华文楷体”, “黑体”, “华文新魏”, “华文隶书”, “微软雅黑”, “楷体_GB2312”}
23
24 private String[] fontNames = {“宋体”, “华文楷体”, “黑体”, “微软雅黑”, “楷体_GB2312”};25
26 //可选字符
27
28 private String codes = “23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ”;29
30 //背景色
31
32 private Color bgColor = new Color(255, 255, 255);33
34 //验证码上的文本
35
36 privateString text ;37
38 //生成随机的颜色
39
40 privateColor randomColor () {41
42 int red = r.nextInt(150);43
44 int green = r.nextInt(150);45
46 int blue = r.nextInt(150);47
48 return newColor(red, green, blue);49
50 }51
52 //生成随机的字体
53
54 privateFont randomFont () {55
56 int index =r.nextInt(fontNames.length);57
58 String fontName = fontNames[index];//生成随机的字体名称
59
60 int style = r.nextInt(4);//生成随机的样式, 0(无样式), 1(粗体), 2(斜体), 3(粗体 斜体)
61
62 int size = r.nextInt(5) 24; //生成随机字号, 24 ~ 28
63
64 return newFont(fontName, style, size);65
66 }67
68 //画干扰线
69
70 private voiddrawLine (BufferedImage image) {71
72 int num = 3;//一共画3条
73
74 Graphics2D g2 =(Graphics2D)image.getGraphics();75
76 for(int i = 0; i < num; i ) {//生成两个点的坐标,即4个值
77
78 int x1 =r.nextInt(w);79
80 int y1 =r.nextInt(h);81
82 int x2 =r.nextInt(w);83
84 int y2 =r.nextInt(h);85
86 g2.setStroke(new BasicStroke(1.5F));87
88 g2.setColor(Color.BLUE); //干扰线是蓝色
89
90 g2.drawLine(x1, y1, x2, y2);//画线
91
92 }93
94 }95
96 //随机生成一个字符
97
98 private charrandomChar () {99
100 int index =r.nextInt(codes.length());101
102 returncodes.charAt(index);103
104 }105
106 //创建BufferedImage
107
108 privateBufferedImage createImage () {109
110 BufferedImage image = newBufferedImage(w, h, BufferedImage.TYPE_INT_RGB);111
112 Graphics2D g2 =(Graphics2D)image.getGraphics();113
114 g2.setColor(this.bgColor);115
116 g2.fillRect(0, 0, w, h);117
118 returnimage;119
120 }121
122 //调用这个方法得到验证码
123
124 publicBufferedImage getImage () {125
126 BufferedImage image = createImage();//创建图片缓冲区
127
128 Graphics2D g2 = (Graphics2D)image.getGraphics();//得到绘制环境
129
130 StringBuilder sb = new StringBuilder();//用来装载生成的验证码文本131
132 //向图片中画4个字符
133
134 for(int i = 0; i < 4; i ) {//循环四次,每次生成一个字符
135
136 String s = randomChar() “”;//随机生成一个字母
137
138 sb.append(s); //把字母添加到sb中
139
140 float x = i * 1.0F * w / 4; //设置当前字符的x轴坐标
141
142 g2.setFont(randomFont()); //设置随机字体
143
144 g2.setColor(randomColor()); //设置随机颜色
145
146 g2.drawString(s, x, h-5); //画图
147
148 }149
150 this.text = sb.toString(); //把生成的字符串赋给了this.text
151
152 drawLine(image); //添加干扰线
153
154 returnimage;155
156 }157
158 //返回验证码图片上的文本
159
160 publicString getText () {161
162 returntext;163
164 }165
166 //保存图片到指定的输出流
167
168 public static voidoutput (BufferedImage image, OutputStream out)169
170 throwsIOException {171
172 ImageIO.write(image, “JPEG”, out);173
174 }175
176 }
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/159575.html原文链接:https://javaforall.cn