android 生成验证码图片

(转自:http://blog.csdn.net/onlyonecoder/article/details/8231373)

android 生成验证码图片

  1 package com.nobeg.util;

  2 

  3 import java.util.Random;

  4 

  5 import android.graphics.Bitmap;

  6 import android.graphics.Canvas;

  7 import android.graphics.Color;

  8 import android.graphics.Paint;

  9 import android.graphics.Bitmap.Config;

 10 

 11 public class Code {

 12     

 13     private static final char[] CHARS = {

 14         '2', '3', '4', '5', '6', '7', '8', '9',

 15         'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 

 16         'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',

 17         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 

 18         'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'

 19     };

 20     

 21     private static Code bmpCode;

 22     

 23     public static Code getInstance() {

 24         if(bmpCode == null)

 25             bmpCode = new Code();

 26         return bmpCode;

 27     }

 28     

 29     //default settings

 30     private static final int DEFAULT_CODE_LENGTH = 3;

 31     private static final int DEFAULT_FONT_SIZE = 40;

 32     private static final int DEFAULT_LINE_NUMBER = 2;

 33     private static final int BASE_PADDING_LEFT = 20, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 30, RANGE_PADDING_TOP = 20;

 34     private static final int DEFAULT_WIDTH = 130, DEFAULT_HEIGHT = 80;

 35     

 36     //settings decided by the layout xml

 37     //canvas width and height

 38     private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT; 

 39     

 40     //random word space and pading_top

 41     private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT, 

 42             base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;

 43     

 44     //number of chars, lines; font size

 45     private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;

 46     

 47     //variables

 48     private String code;

 49     private int padding_left, padding_top;

 50     private Random random = new Random();

 51     //验证码图片

 52     public Bitmap createBitmap() {

 53         padding_left = 0;

 54         

 55         Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888); 

 56         Canvas c = new Canvas(bp);

 57 

 58         code = createCode();

 59         

 60         c.drawColor(Color.WHITE);

 61         Paint paint = new Paint();

 62         paint.setTextSize(font_size);

 63         

 64         for (int i = 0; i < code.length(); i++) {

 65             randomTextStyle(paint);

 66             randomPadding();

 67             c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);

 68         }

 69 

 70         for (int i = 0; i < line_number; i++) {

 71             drawLine(c, paint);

 72         }

 73         

 74         c.save( Canvas.ALL_SAVE_FLAG );//保存  

 75         c.restore();//

 76         return bp;

 77     }

 78     

 79     public String getCode() {

 80         return code;

 81     }

 82     

 83     //验证码

 84     private String createCode() {

 85         StringBuilder buffer = new StringBuilder();

 86         for (int i = 0; i < codeLength; i++) {

 87             buffer.append(CHARS[random.nextInt(CHARS.length)]);

 88         }

 89         return buffer.toString();

 90     }

 91     

 92     private void drawLine(Canvas canvas, Paint paint) {

 93         int color = randomColor();

 94         int startX = random.nextInt(width);

 95         int startY = random.nextInt(height);

 96         int stopX = random.nextInt(width);

 97         int stopY = random.nextInt(height);

 98         paint.setStrokeWidth(1);

 99         paint.setColor(color);

100         canvas.drawLine(startX, startY, stopX, stopY, paint);

101     }

102     

103     private int randomColor() {

104         return randomColor(1);

105     }

106 

107     private int randomColor(int rate) {

108         int red = random.nextInt(256) / rate;

109         int green = random.nextInt(256) / rate;

110         int blue = random.nextInt(256) / rate;

111         return Color.rgb(red, green, blue);

112     }

113     

114     private void randomTextStyle(Paint paint) {

115         int color = randomColor();

116         paint.setColor(color);

117         paint.setFakeBoldText(random.nextBoolean());  //true为粗体,false为非粗体

118         float skewX = random.nextInt(11) / 10;

119         skewX = random.nextBoolean() ? skewX : -skewX;

120         paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜

121 //        paint.setUnderlineText(true); //true为下划线,false为非下划线

122 //        paint.setStrikeThruText(true); //true为删除线,false为非删除线

123     }

124     

125     private void randomPadding() {

126         padding_left += base_padding_left + random.nextInt(range_padding_left);

127         padding_top = base_padding_top + random.nextInt(range_padding_top);

128     }

129 }

 调用:

    /**

     * 生成验证码

     */

    private void createConfirmCode(){

        mImageView.setImageBitmap(ConfirmCode.getInstance()

                .createBitmap());

        String Code = ConfirmCode.getInstance().getCode();

        

    }

 

你可能感兴趣的:(android)