Android random Verification Code Generation Technology

Source: Internet
Author: User

Android random Verification Code Generation Technology
In Android client application development, SMS verification codes or random verification codes are often required to restrict users' operations or authentication. The text message verification code is used to authenticate users through communication protocols such as Http. The Random verification code is largely restricted or prompts users for related operations. There are two methods to verify the Random verification code: Request server verification and Local verification. For example, when a user sends a login request to the server, we use a Random verification code (Local verification) to restrict the user from pressing the request button at will, as shown below:Source code practice (1) src/.../createCode. java Function: Creates a bitmap with four display styles and different positions by using Random, Canvas, and painting.

Package com. example. randomcode; 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;/*** project name/version: RandomCode/v1.0 * package name: com. example. randomcode * class description: (1) generate a set of random numbers; * (2) set the random number style and draw a canvas. drawText () in-place graph * (3) Draw disturbing lines with style changes on the in-place graph * created by: jiangdongguo * created at: 3:22:41 * blog address: http://blog.csdn.net/u012637501 */public class createCode {private static final char [] CHARS = {'1', '2', '3', '4', '5', '6 ', '7', '8', '9', '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'}; // the predefined random number library private static final int CodeLength = 4; // number of random numbers private static final int LineNumber = 8; // number of lines private static final int WIDTH = 140, HEIGHT = 80; // bitmap length, WIDTH private static final int FontSize = 40; // random font size private static int base_padding_left; private static final int random_padding_left = 23, base_padding_top = 45, random_padding_top = 10; private static Random random = new Random (); /*************************************** **************************************** * ** Method Name: createRandomBitmap * Function Description: generate a random Verification Code view * Data: [J] ************************************ **************************************** * ***/public static Bitmap createRandomBitmap () {/*** (1) generate a set of random numbers **/String code = createRandomText (); // generate 4 random numbers/*** (2) create Bitmap, canvas, initialize the Paint brush **/Bitmap bitmap = Bitmap. createBitmap (WIDTH, HEIGHT, Config. ARGB_8888); // creates a bitmap and specifies its length and width. Canvas canvas = new Canvas (bitmap); // creates a canvas Canvas for the specified bitmap. drawColor (Color. WHITE); // set the background of the canvas to WHITE Paint paint = new Paint (); // define the paint. setTextSize (FontSize); // set the paint brush font size/*** (3) generate four bitmap with different random numbers in different styles (color, position, shape) **/base_padding_left = 20; for (int I = 0; I
 
  
Note: The key to creating a random character bitmap is to design the display position of each character on the in-place graph. If the upper and lower margins of each character are not suitable, the characters cannot be displayed on the in-place graph. Pay special attention to the left and right margins of each character. The Code is as follows: private static int base_padding_left; private static final int random_padding_left = 23, base_padding_top = 45, random_padding_top = 10; // The distance between the first character and the left boundary of the In-Place graph is for (int I = 0; I
  
   
(2) src/.../MainActivity. java
   Function: display the created bitmap to the ImageButton component of the interface layout.
   
package com.example.randomcode;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;public class MainActivity extends Activity {     @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        final ImageButton imageButton = (ImageButton)findViewById(R.id.myImage);        imageButton.setImageBitmap(createCode.createRandomBitmap());        imageButton.setOnClickListener(new OnClickListener() {   public void onClick(View v) {             imageButton.setImageBitmap(createCode.createRandomBitmap());   }  });      }}
(3) res/layout/main. xml
        
     
    
(4) effect demonstration

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.