android生成验证码bitmap

时间:2021-12-11 05:58:43
不多说了,直接上代码,项目中用到的,未做优化,还有很多参数未设置。

[java] view plaincopy

1.import java.util.Random;
2.
3.import android.graphics.Bitmap;
4.import android.graphics.Canvas;
5.import android.graphics.Color;
6.import android.graphics.Paint;
7.import android.graphics.Bitmap.Config;
8.
9.public class BPUtil {
10.
11. private static final char[] CHARS = {
12. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
13. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
14. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
15. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
16. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
17. };
18.
19. private static BPUtil bpUtil;
20.
21. public static BPUtil getInstance() {
22. if(bpUtil == null)
23. bpUtil = new BPUtil();
24. return bpUtil;
25. }
26.
27.// width="60" height="30"
28.// base_padding_left="5"
29.// range_padding_left="10"
30.// base_padding_top="15"
31.// range_padding_top="10"
32.// codeLength="4"
33.// line_number="3"
34.// font_size="20"
35.
36. //default settings
37. private static final int DEFAULT_CODE_LENGTH = 4;
38. private static final int DEFAULT_FONT_SIZE = 20;
39. private static final int DEFAULT_LINE_NUMBER = 3;
40. private static final int BASE_PADDING_LEFT = 5, RANGE_PADDING_LEFT = 10, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 10;
41. private static final int DEFAULT_WIDTH = 60, DEFAULT_HEIGHT = 30;
42.
43. //settings decided by the layout xml
44. //canvas width and height
45. private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;
46.
47. //random word space and pading_top
48. private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT,
49. base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;
50.
51. //number of chars, lines; font size
52. private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;
53.
54. //variables
55. private String code;
56. private int padding_left, padding_top;
57. private Random random = new Random();
58.
59. public Bitmap createBitmap() {
60. padding_left = 0;
61.
62. Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
63. Canvas c = new Canvas(bp);
64.
65. code = createCode();
66.
67. c.drawColor(Color.WHITE);
68. Paint paint = new Paint();
69. paint.setTextSize(font_size);
70.
71. for (int i = 0; i < code.length(); i++) {
72. randomTextStyle(paint);
73. randomPadding();
74. c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);
75. }
76.
77. for (int i = 0; i < line_number; i++) {
78. drawLine(c, paint);
79. }
80.
81. c.save( Canvas.ALL_SAVE_FLAG );//保存
82. c.restore();//
83. return bp;
84. }
85.
86. public String getCode() {
87. return code;
88. }
89.
90. private String createCode() {
91. StringBuilder buffer = new StringBuilder();
92. for (int i = 0; i < codeLength; i++) {
93. buffer.append(CHARS[random.nextInt(CHARS.length)]);
94. }
95. return buffer.toString();
96. }
97.
98. private void drawLine(Canvas canvas, Paint paint) {
99. int color = randomColor();
100. int startX = random.nextInt(width);
101. int startY = random.nextInt(height);
102. int stopX = random.nextInt(width);
103. int stopY = random.nextInt(height);
104. paint.setStrokeWidth(1);
105. paint.setColor(color);
106. canvas.drawLine(startX, startY, stopX, stopY, paint);
107. }
108.
109. private int randomColor() {
110. return randomColor(1);
111. }
112.
113. private int randomColor(int rate) {
114. int red = random.nextInt(256) / rate;
115. int green = random.nextInt(256) / rate;
116. int blue = random.nextInt(256) / rate;
117. return Color.rgb(red, green, blue);
118. }
119.
120. private void randomTextStyle(Paint paint) {
121. int color = randomColor();
122. paint.setColor(color);
123. paint.setFakeBoldText(random.nextBoolean()); //true为粗体,false为非粗体
124. float skewX = random.nextInt(11) / 10;
125. skewX = random.nextBoolean() ? skewX : -skewX;
126. paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
127.// paint.setUnderlineText(true); //true为下划线,false为非下划线
128.// paint.setStrikeThruText(true); //true为删除线,false为非删除线
129. }
130.
131. private void randomPadding() {
132. padding_left += base_padding_left + random.nextInt(range_padding_left);
133. padding_top = base_padding_top + random.nextInt(range_padding_top);
134. }
135.} 使用方法 [java] view plaincopy 1.ImageView imageView = (ImageView)findViewById(R.id.imageView);
2.imageView.setImageBitmap(BPUtil.getInstance().createBitmap()); 摘自:http://blog.csdn.net/whumr1/article/details/7092013