1 import android.graphics.Bitmap;
2 import android.graphics.Canvas;
3 import android.graphics.Color;
4 import android.graphics.Paint;
5 import java.util.Random;
6
7 /**
8 * Created by ekikousei易皇星 on 16/11/21.
9 * E-mail:13764664731@163.com
10 * Signature:缘分是本书,翻的不经意会错过,读的太认真会流泪!!
11 *
12 * TODO:类描述: 用于图形验证码的工具类
13 */
14 public class CodeUtils {
15
16 // private static final char[] CHARS = {
17 // '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
18 // 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
19 // 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
20 // 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
21 // 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
22 // };
23
24 private static final char[] CHARS = {
25
26 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
27 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
28
29 };
30
31 private static CodeUtils mCodeUtils;
32 private int mPaddingLeft, mPaddingTop;
33 private StringBuilder mBuilder = new StringBuilder();
34 private Random mRandom = new Random();
35
36 //Default Settings
37 // private static final int DEFAULT_CODE_LENGTH = 6;//验证码的长度 这里是6位
38 private static final int DEFAULT_CODE_LENGTH = 4;//验证码的长度 这里是4位
39 private static final int DEFAULT_FONT_SIZE = 60;//字体大小
40 private static final int DEFAULT_LINE_NUMBER = 3;//多少条干扰线
41 private static final int BASE_PADDING_LEFT = 40; //左边距
42 private static final int RANGE_PADDING_LEFT = 30;//左边距范围值
43 private static final int BASE_PADDING_TOP = 70;//上边距
44 private static final int RANGE_PADDING_TOP = 15;//上边距范围值
45 private static final int DEFAULT_WIDTH = 300;//默认宽度.图片的总宽
46 private static final int DEFAULT_HEIGHT = 100;//默认高度.图片的总高
47 private static final int DEFAULT_COLOR = 0xDF;//默认背景颜色值
48
49 private String code;
50
51 public static CodeUtils getInstance() {
52 if (mCodeUtils == null) {
53 mCodeUtils = new CodeUtils();
54 }
55 return mCodeUtils;
56 }
57
58 //生成验证码图片 返回类型为bitmap 直接用imageview.setbitmap()即可
59 public Bitmap createBitmap() {
60 mPaddingLeft = 0; //每次生成验证码图片时初始化
61 mPaddingTop = 0;
62
63 Bitmap bitmap = Bitmap.createBitmap(DEFAULT_WIDTH, DEFAULT_HEIGHT, Bitmap.Config.ARGB_8888);
64 Canvas canvas = new Canvas(bitmap);
65
66 code = createCode();
67
68 canvas.drawColor(Color.rgb(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR));
69 Paint paint = new Paint();
70 paint.setTextSize(DEFAULT_FONT_SIZE);
71
72 for (int i = 0; i < code.length(); i++) {
73 randomTextStyle(paint);
74 randomPadding();
75 canvas.drawText(code.charAt(i) + "", mPaddingLeft, mPaddingTop, paint);
76 }
77
78 //干扰线
79 for (int i = 0; i < DEFAULT_LINE_NUMBER; i++) {
80 drawLine(canvas, paint);
81 }
82
83 canvas.save(Canvas.ALL_SAVE_FLAG);//保存
84 canvas.restore();
85 return bitmap;
86 }
87
88 /**
89 * 得到图片中的验证码字符串
90 *
91 * @return
92 */
93 public String getCode() {
94 return code;
95 }
96
97 //生成验证码
98 public String createCode() {
99 mBuilder.delete(0, mBuilder.length()); //使用之前首先清空内容
100
101 for (int i = 0; i < DEFAULT_CODE_LENGTH; i++) {
102 mBuilder.append(CHARS[mRandom.nextInt(CHARS.length)]);
103 }
104
105 return mBuilder.toString();
106 }
107
108 //生成干扰线
109 private void drawLine(Canvas canvas, Paint paint) {
110 int color = randomColor();
111 int startX = mRandom.nextInt(DEFAULT_WIDTH);
112 int startY = mRandom.nextInt(DEFAULT_HEIGHT);
113 int stopX = mRandom.nextInt(DEFAULT_WIDTH);
114 int stopY = mRandom.nextInt(DEFAULT_HEIGHT);
115 paint.setStrokeWidth(1);
116 paint.setColor(color);
117 canvas.drawLine(startX, startY, stopX, stopY, paint);
118 }
119
120 //随机颜色
121 private int randomColor() {
122 mBuilder.delete(0, mBuilder.length()); //使用之前首先清空内容
123
124 String haxString;
125 for (int i = 0; i < 3; i++) {
126 haxString = Integer.toHexString(mRandom.nextInt(0xFF));
127 if (haxString.length() == 1) {
128 haxString = "0" + haxString;
129 }
130
131 mBuilder.append(haxString);
132 }
133
134 return Color.parseColor("#" + mBuilder.toString());
135 }
136
137 //随机文本样式
138 private void randomTextStyle(Paint paint) {
139 int color = randomColor();
140 paint.setColor(color);
141 paint.setFakeBoldText(mRandom.nextBoolean()); //true为粗体,false为非粗体
142 float skewX = mRandom.nextInt(11) / 10;
143 skewX = mRandom.nextBoolean() ? skewX : -skewX;
144 paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
145 // paint.setUnderlineText(true); //true为下划线,false为非下划线
146 // paint.setStrikeThruText(true); //true为删除线,false为非删除线
147 }
148
149 //随机间距
150 private void randomPadding() {
151 mPaddingLeft += BASE_PADDING_LEFT + mRandom.nextInt(RANGE_PADDING_LEFT);
152 mPaddingTop = BASE_PADDING_TOP + mRandom.nextInt(RANGE_PADDING_TOP);
153 }
154 }
转载自:https://blog.csdn.net/jky_yihuangxing/article/details/53301834
很简单,直接上代码了。