Android Generate CAPTCHA Image

Source: Internet
Author: User

(Transferred from: http://blog.csdn.net/onlyonecoder/article/details/8231373)

1  PackageCom.nobeg.util;2 3 ImportJava.util.Random;4 5 ImportAndroid.graphics.Bitmap;6 ImportAndroid.graphics.Canvas;7 ImportAndroid.graphics.Color;8 ImportAndroid.graphics.Paint;9 ImportAndroid.graphics.Bitmap.Config;Ten  One  Public classCode { A      -     Private Static Final Char[] CHARS = { -' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', the' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' H ', ' J ', ' K ', ' l ', ' m ',  -' n ', ' 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 ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ' +     }; -      +     Private StaticCode Bmpcode; A      at      Public StaticCode getinstance () { -         if(Bmpcode = =NULL) -Bmpcode =NewCode (); -         returnBmpcode; -     } -      in     //Default Settings -     Private Static Final intDefault_code_length = 3; to     Private Static Final intDefault_font_size = 40; +     Private Static Final intDefault_line_number = 2; -     Private Static Final intBase_padding_left = Range_padding_left = 20, Base_padding_top = Range_padding_top; the     Private Static Final intDefault_width = Default_height = 80; *      $     //settings decided by the layout XMLPanax Notoginseng     //Canvas width and height -     Private intwidth = default_width, height =Default_height; the      +     //random word space and pading_top A     Private intBase_padding_left = base_padding_left, Range_padding_left =Range_padding_left, theBase_padding_top = base_padding_top, Range_padding_top =Range_padding_top; +      -     //Number of chars, lines; font size $     Private intCodelength = default_code_length, Line_number = default_line_number, font_size =default_font_size; $      -     //variables -     PrivateString Code; the     Private intPadding_left, Padding_top; -     PrivateRandom random =NewRandom ();Wuyi     //Verification Code Picture the      PublicBitmap CreateBitmap () { -Padding_left = 0; Wu          -Bitmap BP =bitmap.createbitmap (width, height, config.argb_8888); AboutCanvas C =NewCanvas (BP); $  -Code =Createcode (); -          - C.drawcolor (color.white); APaint paint =NewPaint (); + paint.settextsize (font_size); the          -          for(inti = 0; I < code.length (); i++) { $ Randomtextstyle (paint); the randompadding (); theC.drawtext (Code.charat (i) + "", Padding_left, padding_top, paint); the         } the  -          for(inti = 0; i < Line_number; i++) { in DrawLine (c, paint); the         } the          AboutC.save (Canvas.all_save_flag);//Save theC.restore ();// the         returnBP; the     } +      -      PublicString GetCode () { the         returnCode;Bayi     } the      the     //Verification Code -     PrivateString Createcode () { -StringBuilder buffer =NewStringBuilder (); the          for(inti = 0; i < codelength; i++) { the buffer.append (Chars[random.nextint (chars.length)); the         } the         returnbuffer.tostring (); -     } the      the     Private voiddrawLine (canvas canvas, paint paint) { the         intcolor =Randomcolor ();94         intStartX =random.nextint (width); the         intStarty =random.nextint (height); the         intSTOPX =random.nextint (width); the         intStopy =random.nextint (height);98Paint.setstrokewidth (1); About paint.setcolor (color); - canvas.drawline (StartX, Starty, stopx, stopy, paint);101     }102     103     Private intRandomcolor () {104         returnRandomcolor (1); the     }106 107     Private intRandomcolor (intRate ) {108         intRed = Random.nextint (256)/Rate ;109         intGreen = Random.nextint (256)/Rate ; the         intBlue = Random.nextint (256)/Rate ;111         returnColor.rgb (red, green, blue); the     }113      the     Private voidRandomtextstyle (Paint paint) { the         intcolor =Randomcolor (); the paint.setcolor (color);117Paint.setfakeboldtext (Random.nextboolean ());//true is bold, false is not bold118         floatSkewx = Random.nextint (11)/10;119Skewx = Random.nextboolean ()? SKEWX:-skewx; -Paint.settextskewx (SKEWX);//float type parameter, negative number for right oblique, integer left oblique121 //Paint.setunderlinetext (true);//true is underlined, false is non-underlined122 //Paint.setstrikethrutext (true);//true is strikethrough, false is non-strikethrough123     }124      the     Private voidrandompadding () {126Padding_left + = Base_padding_left +Random.nextint (range_padding_left);127Padding_top = Base_padding_top +Random.nextint (range_padding_top); -     }129}

Call:

    /**      * Generate Verification Code      *     /privatevoid  Createconfirmcode () {        Mimageview.setimagebitmap (Confirmcode.getinstance ()                . CreateBitmap ());         = confirmcode.getinstance (). GetCode ();            }

Android Generate CAPTCHA Image

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.