android 產生驗證碼圖片

來源:互聯網
上載者:User

標籤:

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

  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 產生驗證碼圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.