Android功能模块化之生成验证码Bitmap

Android生成验证码Bitmap,主要使用Canvas绘制,实现的步骤如下:

1.生成验证码。主要采用java的随机函数生成序号,然后对应获取预设的Char数组的值,生成长度固定的验证码;

2.Canvas绘制验证码,生成Bitmap.

 

关键实现代码如下:

1)验证码Char数组

private static final char[] CHARS = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘,  
        ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘,  
        ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘,  
        ‘x‘, ‘y‘, ‘z‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘,  
        ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘,  
        ‘X‘, ‘Y‘, ‘Z‘ };  

2)生成验证码

public String createCode() {  
    StringBuffer buffer = new StringBuffer();  
    for (int i = 0; i < codeLength; i++) {//codeLength为生成的验证码的长度  
        buffer.append(CHARS[random.nextInt(CHARS.length)]);//random为Random变量  
}  
    return buffer.toString();  
} 

3)生成Bitmap

public Bitmap createCodeBitmap() {  
    padding_left = 0;  
    base_padding_left = width / codeLength;  
    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);  
    Canvas canvas = new Canvas(bitmap);  
    code = createCode();  
    canvas.drawColor(Color.WHITE);  
    Paint paint = new Paint();  
    paint.setTextSize(fontSize);  
    paint.setColor(Color.BLUE);  
    for (int i = 0; i < code.length(); i++) {  
        randomTextStyle(paint);  
        randomPadding(i);  
        canvas.drawText(String.valueOf(code.charAt(i)), padding_left,  
                base_padding_top + range_padding_top, paint);  
    }  
    for (int i = 0; i < lineNumber; i++) {  
        drawLine(canvas, paint);//在生成的bitmap上画线  
    }  
    for (int i = 0; i < 255; i++) {  
        drawPoints(canvas, paint);//在生成的bitmap上画点  
    }  
    canvas.save(Canvas.ALL_SAVE_FLAG);  
    canvas.restore();  
    return bitmap;  
}  

上述三个主要是该功能的主要函数及变量,而在生成验证码bitmap时,最主要的函数是canvas.drawText(String text,Float x,Float y,Paint),该函数在bitmap的(x,y)点上绘制text的内容。

使用方法如下:

ivVerifyNum = (ImageView) findViewById(R.id.iv_verify_num);  
        ivVerifyNum.setImageBitmap(SecurityCodeUtil.getInstance()  
                .createCodeBitmap()); 

SecurityCodeUtil.java

package com.gx.chezthb.util;

import java.util.Random;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;

/**
 * 
 * 
 * description: 动态验证码生成<br/>
 * 
 * author: fanruifa <br/>
 * 
 * date: 2013-4-16 <br/>
 * 
 */
public class SecurityCodeUtil {

    private static final int DEFAULT_CODE_LENGTH = 6;
    private static final int DEFAULT_FONT_SIZE = 30;
    private static final int DEFAULT_LINE_NUMBER = 3;
    private static final int BASE_PADDING_LEFT = 5;
    private static final int RANGE_PADDING_LEFT = 10;
    private static final int BASE_PADDING_TOP = 15;
    private static final int RANGE_PADDING_TOP = 10;
    private static final int DEFAULT_WIDTH = 140;
    private static final int DEFAULT_HEIGHT = 39;

    private static final char[] CHARS = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘,
            ‘7‘, ‘8‘, ‘9‘, ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘,
            ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘,
            ‘x‘, ‘y‘, ‘z‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘,
            ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘,
            ‘X‘, ‘Y‘, ‘Z‘ };

    private static SecurityCodeUtil securityCodeUtil = null;

    private int width = DEFAULT_WIDTH;
    private int height = DEFAULT_HEIGHT;
    private int base_padding_left = BASE_PADDING_LEFT;
    private int base_padding_top = BASE_PADDING_TOP;
    private int range_padding_left = RANGE_PADDING_LEFT;
    private int range_padding_top = RANGE_PADDING_TOP;
    private int codeLength = DEFAULT_CODE_LENGTH;
    private int lineNumber = DEFAULT_LINE_NUMBER;
    private int fontSize = DEFAULT_FONT_SIZE;

    private String code;
    private int padding_left, padding_top;
    private Random random = new Random();

    private SecurityCodeUtil() {
    }

    public static SecurityCodeUtil getInstance() {
        if (securityCodeUtil == null) {
            securityCodeUtil = new SecurityCodeUtil();
        }
        return securityCodeUtil;
    }

    public String createCode() {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < codeLength; i++) {
            buffer.append(CHARS[random.nextInt(CHARS.length)]);
        }
        return buffer.toString();
    }

    public Bitmap createCodeBitmap() {
        padding_left = 0;
        base_padding_left = width / codeLength;
        Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        code = createCode();
        canvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        paint.setTextSize(fontSize);
        paint.setColor(Color.BLUE);
        for (int i = 0; i < code.length(); i++) {
            randomTextStyle(paint);
            randomPadding(i);
            canvas.drawText(String.valueOf(code.charAt(i)), padding_left,

                    base_padding_top + range_padding_top, paint);
        }
        for (int i = 0; i < lineNumber; i++) {
            drawLine(canvas, paint);
        }
        for (int i = 0; i < 255; i++) {
            drawPoints(canvas, paint);
        }
        canvas.save(Canvas.ALL_SAVE_FLAG);
        canvas.restore();
        return bitmap;
    }

    public String getCode() {
        return code;
    }

    private int randomColor(int rate) {
        int red = random.nextInt(256) / rate;
        int green = random.nextInt(256) / rate;
        int blue = random.nextInt(256) / rate;
        return Color.rgb(red, green, blue);
    }

    private int randomColor() {
        return randomColor(1);
    }

    private void randomTextStyle(Paint paint) {
        int color = randomColor();
        paint.setColor(color);
        paint.setFakeBoldText(random.nextBoolean());
        float skewX = random.nextInt(11) / 10;
        skewX = random.nextBoolean() ? skewX : -skewX;
        paint.setTextSkewX(skewX);
    }

    private void randomPadding() {
        padding_left += base_padding_left + random.nextInt(range_padding_left);
        // padding_top += base_padding_top + random.nextInt(range_padding_top);
    }

    private void randomPadding(int i) {
        padding_left = base_padding_left * i
                + random.nextInt(range_padding_left);
    }

    private void drawLine(Canvas canvas, Paint paint) {
        int color = randomColor();
        int startX = random.nextInt(width);
        int startY = random.nextInt(height);
        int stopX = random.nextInt(width);
        int stopY = random.nextInt(height);
        paint.setStrokeWidth(1);
        paint.setColor(color);
        canvas.drawLine(startX, startY, stopX, stopY, paint);
    }
    
    private void drawPoints(Canvas canvas, Paint paint){
        int color = randomColor();
        int stopX = random.nextInt(width);
        int stopY = random.nextInt(height);
        paint.setStrokeWidth(1);
        paint.setColor(color);
        canvas.drawPoint(stopX, stopY, paint);
    }

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。