Android Generate verification Code

Source: Internet
Author: User

First put on the picture of this little demo: Click on the image to replace the new verification code.


The captcha is displayed as a picture, so you need to write a custom Captcha control

public class Validateview extends View {/** * points */private int pointnum = 150;//background impurity/** * segment number */private int linenum = 3;/ /Background impurity/** * captcha */private int validatecodelenght = 6;//default length 6/** * captcha */private string[] validatecode;private Paint MT Emppaint = new Paint ();p rivate context mcontext;/** * CAPTCHA content */private static final string[] strcontent = new string[] {"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 "};p ublic Validateview (context context, AttributeSet Attrs) { Super (context, attrs); Mcontext = Context;mtemppaint.setantialias (True); Mtemppaint.settextsize (50); Mtemppaint.setstrokewidth (3); validatecode = new string[validatecodelenght];//Verification Code}public string[] GetValidateCode () { return validatecode;} public void Setvalidatecode (striNg[] validatecode) {this.validatecode = Validatecode;} public void OnDraw (canvas canvas) {canvas.drawcolor (Color.green);//Draw Verification Code final int height = getheight (); final int width = GetWidth (); int dx = 30;for (int i = 0; i < validatecodelenght; i++) {Canvas.drawtext ("" + validatecode[i], DX, getposit On (height), mtemppaint);d x + = width/(validatecodelenght + 1);} Int[] line;for (int i = 0; i < linenum; i++) {line = GetLine (height, width); Canvas.drawline (Line[0], line[1], line[2], LINE[3], mtemppaint);} Draw small Dot int[] point;for (int i = 0; i < Pointnum; i++) {point = GetPoint (height, width); canvas.drawcircle (point[0], poi Nt[1], 1, mtemppaint);}} /** * Update captcha shows */public void Invalichenknum () {invalidate ();} /** * Generate verification code and display on picture * * @return */public string[] Createandsetvalidatecode () {string[] Tempvalidatecode = new String[vali datecodelenght];for (int i = 0; i < validatecodelenght; i++) {Tempvalidatecode[i] = strcontent[(int) (Math.random () * s Trcontent.length)];} Validatecode = TempvalidAtecode;return Tempvalidatecode;} /** * Generate Verification code * * @return */public string[] Createchecknum () {string[] tempchecknum = new String[validatecodelenght];for (i NT i = 0; i < validatecodelenght; i++) {Tempchecknum[i] = strcontent[(int) (Math.random () * strcontent.length)];} return tempchecknum;} Private int[] GetLine (int height, int width) {int[] tempchecknum = new Int[validatecodelenght];for (int i = 0; i < Vali Datecodelenght; i + = 2) {Tempchecknum[i] = (int) (Math.random () * width); tempchecknum[i + 1] = (int) (Math.random () * height);} return tempchecknum;} Private int[] GetPoint (int height, int width) {int[] Tempchecknum = new Int[validatecodelenght];tempchecknum[0] = (int) (M Ath.random () * width); tempchecknum[1] = (int) (Math.random () * height); return tempchecknum;} /** * Verify correct, unit Test through * * @param usercheck * User Input Verification code * @param Validatecode * automatically generated verification code * @return */pu Blic boolean Validatecode (String usercheck, string[] code) {if (Usercheck.length ()! = ValidatecodelengHT) {return false;} String checkstring = ""; for (int i = 0; i < validatecodelenght; i++) {checkstring + = Code[i];} if (Usercheck.equals (checkstring)) {return true;} else {return false;}} /** * Get the ordinate of the verification code * * @param height * @return */public int Getpositon (int height) {int temppositoin = (int) (Math.random () * height); if (Temppositoin <) {Temppositoin + = 25;} return temppositoin;} public int Getpointnum () {return pointnum;} public void Setpointnum (int pointnum) {this.pointnum = Pointnum;} public int Getlinenum () {return linenum;} public void Setlinenum (int linenum) {this.linenum = LineNum;} public int getvalidatecodelenght () {return validatecodelenght;} public void setvalidatecodelenght (int validatecodelenght) {this.validatecodelenght = Validatecodelenght;validatecode = new string[validatecodelenght];//Verification Code}}
Simple XML layout:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:gravity= "Center_horizontal"    android:orientation= "vertical" >    < Com.example.validateviewdemo.ValidateView        android:id= "@+id/validateview"        android:layout_width= "180DP"        android:layout_height= "50DP"/></linearlayout>

For simple functions:

public class Mainactivity extends Activity {validateview validateview;private static final int lenght = 6;//verification Code length private String[] Code = new string[lenght];//Verification Code @overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); Validateview = (Validateview) findViewById ( R.id.validateview); Validateview.setvalidatecodelenght (lenght); code = Validateview.createandsetvalidatecode (); Validateview.setonclicklistener (New Onclicklistener () {//Click on the picture, replace the verification code @overridepublic void OnClick (View arg0) {code = Validateview.createandsetvalidatecode (); Validateview.invalidate ();}});}}
The general use of the method is to obtain the automatic generation of the verification code, and then from the input box to obtain the user input verification code, mutual comparison can be obtained results.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Generate verification Code

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.