轉載自:https://blog.csdn.net/jky_yihuangxing/article/details/53301834 很簡單,直接上代碼了。 ...
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:[email protected] 10 * Signature:緣分是本書,翻的不經意會錯過,讀的太認真會流淚!! 11 * <p/> 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
很簡單,直接上代碼了。