注册功能需要随机生成二维码
生成二维码的Code文件:
1 package com.example.justloginregistertest; 2 3 import android.graphics.Bitmap; 4 import android.graphics.Canvas; 5 import android.graphics.Color; 6 import android.graphics.Paint; 7 8 import java.util.Random; 9 10 /** 11 * Created by littlecurl 2018/6/24 12 */ 13 14 public class Code { 15 /** 16 * 随机数数组 17 * 去除了易混淆的 数字 0 和 字母 o O 18 * 数字 1 和 字母 i I l L 19 * 数字 6 和 字母 b 20 * 数字 9 和 字母 q 21 * 字母 c C 和 G 22 * 字母 t (经常和随机线混在一起看不清) 23 */ 24 private static final char[] CHARS = { 25 '2', '3', '4', '5', '7', '8', 26 'a', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 27 'n', 'p', 'r', 's', 'u', 'v', 'w', 'x', 'y', 'z', 28 'A', 'B', 'D', 'E', 'F', 'H', 'J', 'K', 'M', 29 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' 30 }; 31 32 private static Code bmpCode; 33 34 public static Code getInstance() { 35 if(bmpCode == null) 36 bmpCode = new Code(); 37 return bmpCode; 38 } 39 40 //default settings 41 //验证码默认随机数的个数 42 private static final int DEFAULT_CODE_LENGTH = 4; 43 //默认字体大小 44 private static final int DEFAULT_FONT_SIZE = 25; 45 //默认线条的条数 46 private static final int DEFAULT_LINE_NUMBER = 5; 47 //padding值 48 private static final int BASE_PADDING_LEFT = 10, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20; 49 //验证码的默认宽高 50 private static final int DEFAULT_WIDTH = 100, DEFAULT_HEIGHT = 40; 51 52 //settings decided by the layout xml 53 //canvas width and height 54 private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT; 55 56 //random word space and pading_top 57 private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT, 58 base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP; 59 60 //number of chars, lines; font size 61 private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE; 62 63 //variables 64 private String code; 65 private int padding_left, padding_top; 66 private Random random = new Random(); 67 //验证码图片 68 public Bitmap createBitmap() { 69 padding_left = 0; 70 71 Bitmap bp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 72 Canvas c = new Canvas(bp); 73 74 code = createCode(); 75 76 c.drawColor(Color.WHITE); 77 Paint paint = new Paint(); 78 paint.setAntiAlias(true); 79 paint.setTextSize(font_size); 80 //画验证码 81 for (int i = 0; i < code.length(); i++) { 82 randomTextStyle(paint); 83 randomPadding(); 84 c.drawText(code.charAt(i) + "", padding_left, padding_top, paint); 85 } 86 //画线条 87 for (int i = 0; i < line_number; i++) { 88 drawLine(c, paint); 89 } 90 91 // c.save( Canvas.ALL_SAVE_FLAG );//保存 92 c.save();//保存 93 c.restore();// 94 return bp; 95 } 96 97 public String getCode() { 98 return code; 99 } 100 101 //生成验证码 102 private String createCode() { 103 StringBuilder buffer = new StringBuilder(); 104 for (int i = 0; i < codeLength; i++) { 105 buffer.append(CHARS[random.nextInt(CHARS.length)]); 106 } 107 return buffer.toString(); 108 } 109 //画干扰线 110 private void drawLine(Canvas canvas, Paint paint) { 111 int color = randomColor(); 112 int startX = random.nextInt(width); 113 int startY = random.nextInt(height); 114 int stopX = random.nextInt(width); 115 int stopY = random.nextInt(height); 116 paint.setStrokeWidth(1); 117 paint.setColor(color); 118 canvas.drawLine(startX, startY, stopX, stopY, paint); 119 } 120 //生成随机颜色 121 private int randomColor() { 122 return randomColor(1); 123 } 124 125 private int randomColor(int rate) { 126 int red = random.nextInt(256) / rate; 127 int green = random.nextInt(256) / rate; 128 int blue = random.nextInt(256) / rate; 129 return Color.rgb(red, green, blue); 130 } 131 //随机生成文字样式,颜色,粗细,倾斜度 132 private void randomTextStyle(Paint paint) { 133 int color = randomColor(); 134 paint.setColor(color); 135 paint.setFakeBoldText(random.nextBoolean()); //true为粗体,false为非粗体 136 float skewX = random.nextInt(11) / 10; 137 skewX = random.nextBoolean() ? skewX : -skewX; 138 paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜 139 //paint.setUnderlineText(true); //true为下划线,false为非下划线 140 //paint.setStrikeThruText(true); //true为删除线,false为非删除线 141 } 142 //随机生成padding值 143 private void randomPadding() { 144 padding_left += base_padding_left + random.nextInt(range_padding_left); 145 padding_top = base_padding_top + random.nextInt(range_padding_top); 146 } 147 }