Android client Random verification code generation implementation

Source: Internet
Author: User

Because the verification code is used in the project, I found some information and tried to give it out. The code is not a lot and it is relatively simple, the following shows how I implemented this function:

 

 

 

First of all, write XML and paste the code.

 
               
                
             
   
  
             
               
                
             
   
  
       
                 
                      
                       
                    
    
   
                  
  
                     
           
    
   
  
 


Paste the MainActivity code below:

The comment in it is very detailed, so I will not talk about it much!

Import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. menu; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. imageView; import android. widget. toast; public class MainActivity extends Activity {ImageView vc_image; // icon Button vc_shuaixi, vc_ OK; // confirm and refresh the verification code String GetCode = null; // obtain the value of the Verification Code EditText vc_code; // The value of the text box @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); vc_image = (ImageView) findViewById (R. id. vc_image); vc_image.setImageBitmap (Code. getInstance (). getBitmap (); vc_code = (EditText) findViewById (R. id. vc_code); getCode = Code. getInstance (). getCode (); // obtain the displayed Verification Code Log. e (info, getCod E + ----); vc_shuaixi = (Button) findViewById (R. id. vc_shuaixi); vc_shuaixi.setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubvc_image.setImageBitmap (Code. getInstance (). getBitmap (); getCode = Code. getInstance (). getCode () ;}}); vc_ OK = (Button) findViewById (R. id. vc_ OK); vc_ OK .setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View V) {// TODO Auto-generated method stubStringv_code = vc_code.getText (). toString (). trim (); if (v_code = null | v_code.equals () {Toast. makeText (MainActivity. this, no verification code is entered, 2 ). show ();} else if (! V_code.equals (getCode) {Toast. makeText (MainActivity. this indicates that the verification code is incorrect. 2 ). show ();} else {Toast. makeText (MainActivity. this, operation successful, 2 ). show ();}}});}


Finally, paste a required class Code for the verification Code:

Import java. util. random; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. bitmap. config; public class Code {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 Code bpUtil; private Code () {}; public static Code getInstance () {if (bpUtil = null) bpUtil = new Code (); return bpUtil;} // default settingsprivate static final int DEFAULT_CODE_LENGTH = 4; // The length of the verification code is 4-bit private static final int DEFAULT_FONT_SIZ E = 60; // font size private static final int DEFAULT_LINE_NUMBER = 3; // Number of interfering lines private static final int BASE_PADDING_LEFT = 20; // The left side is private static final int RANGE_PADDING_LEFT = 35; // The left side is private static final int BASE_PADDING_TOP = 42; // The top margin is private static final int RANGE_PADDING_TOP = 15; // The value of the top margin range is private static final int DEFAULT_WIDTH = 200; // The default width. the total width of the image is private static final int DEFAULT_HEIGHT = 70; // The default height. figure The total height of the slice is private final int DEFAULT_COLOR = 0xdf; // Default background color value // settings decided by the layout xml // canvas width and heightprivate int width = DEFAULT_WIDTH; private int height = DEFAULT_HEIGHT; // random word space and comment int base_padding_left = BASE_PADDING_LEFT; private int range_padding_left = RANGE_PADDING_LEFT; private int comment = comment; private int range_padding_top = RA Bytes; // number of chars, lines; font sizeprivate int codeLength = DEFAULT_CODE_LENGTH; private int line_number = DEFAULT_LINE_NUMBER; private int font_size = DEFAULT_FONT_SIZE; // variablesprivate String code; // Save the generated Verification Code private int padding_left, padding_top; private Random random = new Random (); private Bitmap createBitmap () {padding_left = 0; Bitmap bp = Bitmap. createBitmap (width, height, Confi G. ARGB_8888); Canvas c = new Canvas (bp); code = createCode (); c. drawColor (Color. rgb (DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR); Paint paint = new Paint (); paint. setTextSize (font_size); for (int I = 0; I <code. length (); I ++) {randomTextStyle (paint); randomPadding (); c. drawText (code. charAt (I) +, padding_left, padding_top, paint) ;}for (int I = 0; I <line_number; I ++) {drawLine (c, paint);} c. save (Canva S. ALL_SAVE_FLAG); // save c. restore (); // return bp;} public String getCode () {return code. toLowerCase ();} public Bitmap getBitmap () {return createBitmap ();} private String createCode () {StringBuilder buffer = new StringBuilder (); for (int I = 0; I <codeLength; I ++) {buffer. append (CHARS [random. nextInt (CHARS. length)]);} return buffer. toString ();} 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 int randomColor () {return randomColor (1);} private int randomColor (int rate) {int red = random. nextInt (256)/rate; int green = random. nextI Nt (256)/rate; int blue = random. nextInt (256)/rate; return Color. rgb (red, green, blue);} private void randomTextStyle (Paint paint) {int color = randomColor (); paint. setColor (color); paint. setFakeBoldText (random. nextBoolean (); // true indicates bold, and false indicates non-bold float skewX = random. nextInt (11)/10; skewX = random. nextBoolean ()? SkewX:-skewX; paint. setTextSkewX (skewX); // float type parameter. A negative number indicates the Right oblique, and an integer indicates the left oblique. // paint. setUnderlineText (true); // true indicates the underline, and false indicates the non-Underline. // paint. setStrikeThruText (true); // true indicates strikethrough, and false indicates non-strikethrough.} private void randomPadding () {padding_left + = base_padding_left + random. nextInt (range_padding_left); padding_top = base_padding_top + random. nextInt (range_padding_top );}}


The Code has been pasted. Try again!

 

 

 

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.