Generation of verification codes in Android development

Source: Internet
Author: User

Recently in the e-commerce Finance project, verification code generation method is essential, first learned a, after testing good, learn from elsewhere code, slightly changed to choose whether to support the identification of the case. directly on the code.

Import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.Toast;
public class Verifycodeactivity extends Activity {
Private ImageView vc_image; Captcha icon private String getcode = null; Gets the value of the verification code private EditText Vc_code; The value of the text box
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  Setcontentview (R.layout.verifycode);  Vc_image = (ImageView) Findviewbyid (R.ID.VERIFY_IMV);  Vc_code = (EditText) Findviewbyid (R.id.myedit); GetCode = Securitycode.getinstance (). GetCode (false); Gets the verification code shown Findviewbyid (r.id.vc_shuaxin). Setonclicklistener (New Onclicklistener () {
@Override public void OnClick (View v) {Vc_image.setimagebitmap (Securitycode.getinstance (). Getbitmap ());   GetCode = Securitycode.getinstance (). GetCode (false);  }  }); Findviewbyid (r.id.verfiy). Setonclicklistener (New Onclicklistener () {
@Override public void OnClick (View v) {String verfiystring = Vc_code.gettext (). toString ();    if (Verfiystring.equals (GetCode)) {Toast.maketext (Getapplicationcontext (), "Captcha entered correctly", Toast.length_long). Show ();    } else {Toast.maketext (Getapplicationcontext (), "Authenticode input Error", Toast.length_long). Show (); }   }  }); }}
Import Java.util.locale;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;
/** * @Description: Verification code Generation class */public classes Securitycode {
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 Securitycode Bputil;
public static Securitycode getinstance () {if (Bputil = = null) Bputil = new Securitycode (); return bputil; }
Default settings private static final int default_code_length = 4;//The length of the verification code here is the 4 bit private static final int Default_font _size = 60;//Font size private static final int default_line_number = 3;//How many lines of interference private static final int base_padding_left = 20; Left margin private static final int range_padding_left = 35;//Left margin range value private static final int base_padding_top = 42;//Top margin PR ivate static final int range_padding_top = 15;//Top margin range value private static final int default_width = 200;//default width. Total width of the picture private static final int default_height = 70;//default height. The total height of the picture private final int default_color = 0xdf;//default background color value
Settings decided by the layout XML//canvas width and height private int width = default_width; private int height = default_height;
Random word space and pading_top private int base_padding_left = Base_padding_left; private int range_padding_left = Range_padding_left; private int base_padding_top = Base_padding_top; private int range_padding_top = Range_padding_top;
Number of chars, lines; Font size private int codelength = Default_code_length; private int line_number = Default_line_number; private int font_size = Default_font_size;
Variables private String code;//save 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, config.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 (Canvas.all_save_flag);//Save C.restore ();//return BP; }
/** * @Description: Case Sensitivity */Public String GetCode (Boolean ismatchcase) {if (ismatchcase) {if (code! = null) {   Code.tolowercase (Locale.getdefault ());  }} else {return code; }
return null; }
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 (n)/rate;  int green = Random.nextint (n)/rate;  int blue = Random.nextint (n)/rate; Return Color.rgb (red, green, blue); }
private void Randomtextstyle (paint paint) {int color = Randomcolor ();  Paint.setcolor (color); Paint.setfakeboldtext (Random.nextboolean ());  True is bold, false is non-bold float skewx = Random.nextint (11)/10; Skewx = Random.nextboolean ()?  SKEWX:-skewx; Paint.settextskewx (SKEWX); Float type parameter, negative number for right oblique, integer left oblique//Paint.setunderlinetext (TRUE); True to underscore, false to non-underscore//Paint.setstrikethrutext (TRUE); True is strikethrough, false is not strikethrough}
private void Randompadding () {padding_left + = Base_padding_left + random.nextint (range_padding_left); Padding_top = Base_padding_top + random.nextint (range_padding_top); }}
Layout file <?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" > <imageview
Android:id= "@+id/verify_imv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/> <button
Android:id= "@+id/vc_shuaxin"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Get Verification Code"/> <edittext
Android:id= "@+id/myedit"
Android:layout_width= "200DP"
android:layout_height= "Wrap_content"
Android:hint= "Please enter the verification code"/> <button
Android:id= "@+id/verfiy"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:text= "Verify that the verification code entered is correct"/></linearlayout>



Generation of verification codes in Android development

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.