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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
|
package com.test.test;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Random;
import javax.imageio.ImageIO;
import org.apache.log4j.Logger;
/**
* 验证码工具类:
* 随机字体、字体样式、字体大小(验证码图片宽度 - 8 ~ 验证码图片宽度 + 10)
* 彩色字符 每个字符的颜色随机,一定会不相同
* 随机字符 阿拉伯数字 + 小写字母 + 大写字母
* 3D中空自定义字体,需要单独使用,只有阿拉伯数字和大写字母
* gif支持动态输出
* 2017-06-09 解决压测时自定义字符字体不停的new,内存溢出问题。因为自定义字体不会变,提成static,建议用64bit jdk,自定义自动字符串太长,32bit偶尔编译会报错字符串太长
* @author Anttu
* @date 2017年5月9日 下午7:27:55
*/
public class RandonImgCodeUtil
{
private static Logger logger = Logger.getLogger(RandomVerifyImgCodeUtil.class);
/**
* 随机类
*/
private static Random random = new Random();
// 放到session中的key
public static final String RANDOMCODEKEY = "RANDOMVALIDATECODEKEY";
// 验证码来源范围,去掉了0,1,I,O,l,o几个容易混淆的字符
public static final String VERIFY_CODES = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";
private static ImgFontByte imgFontByte = new ImgFontByte();
private static Font baseFont;
static
{
try
{
baseFont = Font.createFont(Font.TRUETYPE_FONT,
RandomVerifyImgCodeUtils.class.getClassLoader().getResourceAsStream("fonts/ActionJackson.TTF"));
}
catch (FontFormatException e)
{
logger.error("new img font font format failed. e: " + e.getMessage(), e);
}
catch (IOException e)
{
logger.error("new img font io failed. e: " + e.getMessage(), e);
}
}
// 字体类型
private static String[] fontName =
{
"Algerian", "Arial", "Arial Black", "Agency FB", "Calibri", "Cambria", "Gadugi", "Georgia", "Consolas", "Comic Sans MS", "Courier New",
"Gill sans", "Time News Roman", "Tahoma", "Quantzite", "Verdana"
};
// 字体样式
private static int[] fontStyle =
{
Font.BOLD, Font.ITALIC, Font.ROMAN_BASELINE, Font.PLAIN, Font.BOLD + Font.ITALIC
};
// 颜色
private static Color[] colorRange =
{
Color.WHITE, Color.CYAN, Color.GRAY, Color.LIGHT_GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.YELLOW, Color.GREEN, Color.BLUE,
Color.DARK_GRAY, Color.BLACK, Color.RED
};
/**
* 使用系统默认字符源生成验证码
*
* @param verifySize 验证码长度
* @return
*/
public static String generateVerifyCode(int verifySize)
{
return generateVerifyCode(verifySize, VERIFY_CODES);
}
/**
* 使用指定源生成验证码
*
* @param verifySize 验证码长度
* @param sources 验证码字符源
* @return
*/
private static String generateVerifyCode(int verifySize, String sources)
{
if (sources == null || sources.length() == 0)
{
sources = VERIFY_CODES;
}
int codesLen = sources.length();
Random rand = new Random(System.currentTimeMillis());
StringBuilder verifyCode = new StringBuilder(verifySize);
for (int i = 0; i < verifySize; i++)
{
verifyCode.append(sources.charAt(rand.nextInt(codesLen - 1)));
}
return verifyCode.toString();
}
/**
* 输出指定验证码图片流
*
* @param w 验证码图片的宽
* @param h 验证码图片的高
* @param os 流
* @param code 验证码
* @param type 场景类型,login:登录,
* coupons:领券 登录清晰化,领券模糊化
* 3D: 3D中空自定义字体
* GIF:普通动态GIF
* GIF3D:3D动态GIF
* mix2: 普通字体和3D字体混合
* mixGIF: 混合动态GIF
* @throws IOException
*/
public static void outputImage(int w, int h, OutputStream os, String code, String type) throws IOException
{
int verifySize = code.length();
BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Random rand = new Random();
Graphics2D g2 = image.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color[] colors = new Color[5];
Color[] colorSpaces = colorRange;
float[] fractions = new float[colors.length];
for (int i = 0; i < colors.length; i++)
{
colors[i] = colorSpaces[rand.nextInt(colorSpaces.length)];
fractions[i] = rand.nextFloat();
}
Arrays.sort(fractions);
g2.setColor(Color.GRAY);// 设置边框色
g2.fillRect(0, 0, w, h);
Color c = getRandColor(200, 250);
g2.setColor(c);// 设置背景色
g2.fillRect(0, 2, w, h - 4);
char[] charts = code.toCharArray();
for (int i = 0; i < charts.length; i++)
{
g2.setColor(c);// 设置背景色
g2.setFont(getRandomFont(h, type));
g2.fillRect(0, 2, w, h - 4);
}
// 1.绘制干扰线
Random random = new Random();
g2.setColor(getRandColor(160, 200));// 设置线条的颜色
int lineNumbers = 20;
if (type.equals("login") || type.contains("mix") || type.contains("3D"))
{
lineNumbers = 20;
}
else if (type.equals("coupons"))
{
lineNumbers = getRandomDrawLine();
}
else
{
lineNumbers = getRandomDrawLine();
}
for (int i = 0; i < lineNumbers; i++)
{
int x = random.nextInt(w - 1);
int y = random.nextInt(h - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g2.drawLine(x, y, x + xl + 40, y + yl + 20);
}
// 2.添加噪点
float yawpRate = 0.05f;
if (type.equals("login") || type.contains("mix") || type.contains("3D"))
{
yawpRate = 0.05f; // 噪声率
}
else if (type.equals("coupons"))
{
yawpRate = getRandomDrawPoint(); // 噪声率
}
else
{
yawpRate = getRandomDrawPoint(); // 噪声率
}
int area = (int) (yawpRate * w * h);
for (int i = 0; i < area; i++)
{
int x = random.nextInt(w);
int y = random.nextInt(h);
int rgb = getRandomIntColor();
image.setRGB(x, y, rgb);
}
// 3.使图片扭曲
shear(g2, w, h, c);
char[] chars = code.toCharArray();
Double rd = rand.nextDouble();
Boolean rb = rand.nextBoolean();
if (type.equals("login"))
{
for (int i = 0; i < verifySize; i++)
{
g2.setColor(getRandColor(100, 160));
g2.setFont(getRandomFont(h, type));
AffineTransform affine = new AffineTransform();
affine.setToRotation(Math.PI / 4 * rd * (rb ? 1 : -1), (w / verifySize) * i + (h - 4) / 2, h / 2);
g2.setTransform(affine);
g2.drawOval(random.nextInt(w), random.nextInt(h), 5 + random.nextInt(10), 5 + random.nextInt(10));
g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + (h - 4) / 2 - 10);
}
g2.dispose();
ImageIO.write(image, "jpg", os);
}
else if (type.contains("GIF") || type.contains("mixGIF"))
{
GifEncoder gifEncoder = new GifEncoder(); // gif编码类,这个利用了洋人写的编码类
// 生成字符
gifEncoder.start(os);
gifEncoder.setQuality(180);
gifEncoder.setDelay(150);
gifEncoder.setRepeat(0);
AlphaComposite ac3;
for (int i = 0; i < verifySize; i++)
{
g2.setColor(getRandColor(100, 160));
g2.setFont(getRandomFont(h, type));
for (int j = 0; j < verifySize; j++)
{
AffineTransform affine = new AffineTransform();
affine.setToRotation(Math.PI / 4 * rd * (rb ? 1 : -1), (w / verifySize) * i + (h - 4) / 2, h / 2);
g2.setTransform(affine);
g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + (h - 4) / 2 - 10);
ac3 = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getAlpha(j, i, verifySize));
g2.setComposite(ac3);
g2.drawOval(random.nextInt(w), random.nextInt(h), 5 + random.nextInt(10), 5 + random.nextInt(10));
gifEncoder.addFrame(image);
image.flush();
}
}
gifEncoder.finish();
g2.dispose();
}
else
{
for (int i = 0; i < verifySize; i++)
{
g2.setColor(getRandColor(100, 160));
g2.setFont(getRandomFont(h, type));
AffineTransform affine = new AffineTransform();
affine.setToRotation(Math.PI / 4 * rd * (rb ? 1 : -1), (w / verifySize) * i + (h - 4) / 2, h / 2);
g2.setTransform(affine);
g2.drawOval(random.nextInt(w), random.nextInt(h), 5 + random.nextInt(10), 5 + random.nextInt(10));
g2.drawChars(chars, i, 1, ((w - 10) / verifySize) * i + 5, h / 2 + (h - 4) / 2 - 10);
}
g2.dispose();
ImageIO.write(image, "jpg", os);
}
}
/**
* 获取随机颜色
*
* @param fc
* @param bc
* @return
*/
private static Color getRandColor(int fc, int bc)
{
if (fc > 255)
{
fc = 255;
}
if (bc > 255)
{
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
private static int getRandomIntColor()
{
int[] rgb = getRandomRgb();
int color = 0;
for (int c : rgb)
{
color = color << 8;
color = color | c;
}
return color;
}
private static int[] getRandomRgb()
{
int[] rgb = new int[3];
for (int i = 0; i < 3; i++)
{
rgb[i] = random.nextInt(255);
}
return rgb;
}
/**
* 随机字体、随机风格、随机大小
*
* @param h 验证码图片高
* @return
*/
private static Font getRandomFont(int h, String type)
{
// 字体
String name = fontName[random.nextInt(fontName.length)];
// 字体样式
int style = fontStyle[random.nextInt(fontStyle.length)];
// 字体大小
int size = getRandomFontSize(h);
if (type.equals("login"))
{
return new Font(name, style, size);
}
else if (type.equals("coupons"))
{
return new Font(name, style, size);
}
else if (type.contains("3D"))
{
return new ImgFontByte().getFont(size, style);
}
else if (type.contains("mix"))
{
int flag = random.nextInt(10);
if (flag > 4)
{
return new Font(name, style, size);
}
else
{
return new ImgFontByte().getFont(size, style);
}
}
else
{
return new Font(name, style, size);
}
}
/**
* 干扰线按范围获取随机数
*
* @return
*/
private static int getRandomDrawLine()
{
int min = 20;
int max = 155;
Random random = new Random();
return random.nextInt(max) % (max - min + 1) + min;
}
/**
* 噪点数率按范围获取随机数
*
* @return
*/
private static float getRandomDrawPoint()
{
float min = 0.05f;
float max = 0.1f;
return min + ((max - min) * new Random().nextFloat());
}
/**
* 获取字体大小按范围随机
*
* @param h 验证码图片高
* @return
*/
private static int getRandomFontSize(int h)
{
int min = h - 8;
// int max = 46;
Random random = new Random();
return random.nextInt(11) + min;
}
/**
* 3D中空字体自定义属性类
*
* @author Anttu
* @date 2017年5月15日 下午3:27:52
*/
static class ImgFontByte
{
public Font getFont(int fontSize, int fontStype)
{
try
{
Font font = baseFont;
if (baseFont == null)
{
font = Font.createFont(Font.TRUETYPE_FONT, RandomVerifyImgCodeUtils.class.getClassLoader()
.getResourceAsStream("fonts/ActionJackson.TTF"));
}
return font.deriveFont(fontStype, fontSize);
}
catch (Exception e)
{
return new Font("Arial", fontStype, fontSize);
}
}
}
/**
* 字符和干扰线扭曲
*
* @param g 绘制图形的java工具类
* @param w1 验证码图片宽
* @param h1 验证码图片高
* @param color 颜色
*/
private static void shear(Graphics g, int w1, int h1, Color color)
{
shearX(g, w1, h1, color);
shearY(g, w1, h1, color);
}
/**
* x轴扭曲
*
* @param g 绘制图形的java工具类
* @param w1 验证码图片宽
* @param h1 验证码图片高
* @param color 颜色
*/
private static void shearX(Graphics g, int w1, int h1, Color color)
{
int period = random.nextInt(2);
boolean borderGap = true;
int frames = 1;
int phase = random.nextInt(2);
for (int i = 0; i < h1; i++)
{
double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(0, i, w1, 1, (int) d, 0);
if (borderGap)
{
g.setColor(color);
g.drawLine((int) d, i, 0, i);
g.drawLine((int) d + w1, i, w1, i);
}
}
}
/**
* y轴扭曲
*
* @param g 绘制图形的java工具类
* @param w1 验证码图片宽
* @param h1 验证码图片高
* @param color 颜色
*/
private static void shearY(Graphics g, int w1, int h1, Color color)
{
int period = random.nextInt(40) + 10; // 50;
boolean borderGap = true;
int frames = 20;
int phase = 7;
for (int i = 0; i < w1; i++)
{
double d = (double) (period >> 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames);
g.copyArea(i, 0, 1, h1, 0, (int) d);
if (borderGap)
{
g.setColor(color);
g.drawLine(i, (int) d, i, 0);
g.drawLine(i, (int) d + h1, i, h1);
}
}
}
/**
* 获取透明度,从0到1,自动计算步长
*
* @param i
* @param j
* @return float 透明度
*/
private static float getAlpha(int i, int j, int verifySize)
{
int num = i + j;
float r = (float) 1 / verifySize, s = (verifySize + 1) * r;
return num > verifySize ? (num * r - s) : num * r;
}
/**
* 生成指定验证码图像文件 - 本地测试生成图片查看效果
*
* @param w 验证码图片宽
* @param h 验证码图片高
* @param outputFile 文件流
* @param code 随机验证码
* @throws IOException
*/
public static void outputImage(int w, int h, File outputFile, String code) throws IOException
{
if (outputFile == null)
{
return;
}
File dir = outputFile.getParentFile();
if (!dir.exists())
{
dir.mkdirs();
}
try
{
outputFile.createNewFile();
FileOutputStream fos = new FileOutputStream(outputFile);
// outputImage(w, h, fos, code, "login"); //测试登录,噪点和干扰线为0.05f和20
// outputImage(w, h, fos, code, "coupons"); //测试领券,噪点和干扰线为范围随机值0.05f ~ 0.1f和20 ~ 155
// outputImage(w, h, fos, code, "3D"); //测试领券,噪点和干扰线为范围随机值0.05f ~ 0.1f和20 ~ 155
// outputImage(w, h, fos, code, "GIF"); //测试领券,噪点和干扰线为范围随机值0.05f ~ 0.1f和20 ~ 155
// outputImage(w, h, fos, code, "GIF3D"); //测试领券,噪点和干扰线为范围随机值0.05f ~ 0.1f和20 ~ 155
// outputImage(w, h, fos, code, "mix2"); //测试领券,噪点和干扰线为范围随机值0.05f ~ 0.1f和20 ~ 155
outputImage(w, h, fos, code, "mixGIF"); // 测试领券,噪点和干扰线为范围随机值0.05f ~ 0.1f和20 ~ 155
fos.close();
}
catch (IOException e)
{
throw e;
}
}
/**
* 本地测试类,可以生成样例验证码图片供观看效果
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
File dir = new File("E:/logtest/verifies8");
int w = 120, h = 48;
for (int i = 0; i < 150; i++)
{
String verifyCode = generateVerifyCode(4);
File file = new File(dir, verifyCode + ".gif");
outputImage(w, h, file, verifyCode);
}
}
}
|