-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSecurityCodeUtil.java
155 lines (134 loc) · 4.34 KB
/
SecurityCodeUtil.java
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
package com.gx.chezthb.util;
import java.util.Random;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
/**
*
*
* description: 动态验证码生成<br/>
*
* author: fanruifa <br/>
*
* date: 2013-4-16 <br/>
*
*/
public class SecurityCodeUtil {
private static final int DEFAULT_CODE_LENGTH = 6;
private static final int DEFAULT_FONT_SIZE = 30;
private static final int DEFAULT_LINE_NUMBER = 3;
private static final int BASE_PADDING_LEFT = 5;
private static final int RANGE_PADDING_LEFT = 10;
private static final int BASE_PADDING_TOP = 15;
private static final int RANGE_PADDING_TOP = 10;
private static final int DEFAULT_WIDTH = 140;
private static final int DEFAULT_HEIGHT = 39;
private static final char[] CHARS = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z' };
private static SecurityCodeUtil securityCodeUtil = null;
private int width = DEFAULT_WIDTH;
private int height = DEFAULT_HEIGHT;
private int base_padding_left = BASE_PADDING_LEFT;
private int base_padding_top = BASE_PADDING_TOP;
private int range_padding_left = RANGE_PADDING_LEFT;
private int range_padding_top = RANGE_PADDING_TOP;
private int codeLength = DEFAULT_CODE_LENGTH;
private int lineNumber = DEFAULT_LINE_NUMBER;
private int fontSize = DEFAULT_FONT_SIZE;
private String code;
private int padding_left, padding_top;
private Random random = new Random();
private SecurityCodeUtil() {
}
public static SecurityCodeUtil getInstance() {
if (securityCodeUtil == null) {
securityCodeUtil = new SecurityCodeUtil();
}
return securityCodeUtil;
}
public String createCode() {
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < codeLength; i++) {
buffer.append(CHARS[random.nextInt(CHARS.length)]);
}
return buffer.toString();
}
public Bitmap createCodeBitmap() {
padding_left = 0;
base_padding_left = width / codeLength;
Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
code = createCode();
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setTextSize(fontSize);
paint.setColor(Color.BLUE);
for (int i = 0; i < code.length(); i++) {
randomTextStyle(paint);
randomPadding(i);
canvas.drawText(String.valueOf(code.charAt(i)), padding_left,
base_padding_top + range_padding_top, paint);
}
for (int i = 0; i < lineNumber; i++) {
drawLine(canvas, paint);
}
for (int i = 0; i < 255; i++) {
drawPoints(canvas, paint);
}
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bitmap;
}
public String getCode() {
return code;
}
private int randomColor(int rate) {
int red = random.nextInt(256) / rate;
int green = random.nextInt(256) / rate;
int blue = random.nextInt(256) / rate;
return Color.rgb(red, green, blue);
}
private int randomColor() {
return randomColor(1);
}
private void randomTextStyle(Paint paint) {
int color = randomColor();
paint.setColor(color);
paint.setFakeBoldText(random.nextBoolean());
float skewX = random.nextInt(11) / 10;
skewX = random.nextBoolean() ? skewX : -skewX;
paint.setTextSkewX(skewX);
}
private void randomPadding() {
padding_left += base_padding_left + random.nextInt(range_padding_left);
// padding_top += base_padding_top + random.nextInt(range_padding_top);
}
private void randomPadding(int i) {
padding_left = base_padding_left * i
+ random.nextInt(range_padding_left);
}
private void drawLine(Canvas canvas, Paint paint) {
int color = randomColor();
int startX = random.nextInt(width);
int startY = random.nextInt(height);
int stopX = random.nextInt(width);
int stopY = random.nextInt(height);
paint.setStrokeWidth(1);
paint.setColor(color);
canvas.drawLine(startX, startY, stopX, stopY, paint);
}
private void drawPoints(Canvas canvas, Paint paint){
int color = randomColor();
int stopX = random.nextInt(width);
int stopY = random.nextInt(height);
paint.setStrokeWidth(1);
paint.setColor(color);
canvas.drawPoint(stopX, stopY, paint);
}
}