Android随机生成验证码

Android随机生成验证码,第1张

概述Android随机生成验证码,Android利用随机数绘制不规则的验证码,加强用户登录或者注册的安全性。

AndroID随机生成验证码,AndroID利用随机数绘制不规则的验证码,加强用户登录或者注册的安全性。
具体思路如下:
在一块固定宽高的画布上,画上固定个数的随机数字和字母,再画上固定条数的干扰线
随机数和干扰线的颜色随机生成,随机数的样式随机生成。

界面效果如下:

1、生成随机数代码,Code.java:

public class Code {   //随机数数组  private static final char[] CHARS = {    '2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','j','k','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 static Code bmpCode;   public static Code getInstance() {   if(bmpCode == null)    bmpCode = new Code();   return bmpCode;  }   //default settings  //验证码默认随机数的个数  private static final int DEFAulT_CODE_LENGTH = 4;  //默认字体大小  private static final int DEFAulT_Font_SIZE = 25;  //默认线条的条数  private static final int DEFAulT_liNE_NUMBER = 5;  //padding值  private static final int BASE_padding_left = 10,RANGE_padding_left = 15,BASE_padding_top = 15,RANGE_padding_top = 20;  //验证码的默认宽高  private static final int DEFAulT_WIDTH = 100,DEFAulT_HEIGHT = 40;   //settings decIDed by the layout xml  //canvas wIDth and height  private int wIDth = DEFAulT_WIDTH,height = DEFAulT_HEIGHT;   //random word space and pading_top  private int base_padding_left = BASE_padding_left,range_padding_left = RANGE_padding_left,base_padding_top = BASE_padding_top,range_padding_top = RANGE_padding_top;   //number of chars,lines; Font size  private int codeLength = DEFAulT_CODE_LENGTH,line_number = DEFAulT_liNE_NUMBER,Font_size = DEFAulT_Font_SIZE;   //variables  private String code;  private int padding_left,padding_top;  private Random random = new Random();  //验证码图片  public Bitmap createBitmap() {   padding_left = 0;    Bitmap bp = Bitmap.createBitmap(wIDth,height,Config.ARGB_8888);   Canvas c = new Canvas(bp);    code = createCode();    c.drawcolor(color.WHITE);   Paint paint = new Paint();   paint.setAntiAlias(true);   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 );//保存   c.restore();//   return bp;  }   public String getCode() {   return code;  }   //生成验证码  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.nextInt(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为粗体,false为非粗体   float skewX = random.nextInt(11) / 10;   skewX = random.nextBoolean() ? skewX : -skewX;   paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜   //paint.setUnderlineText(true); //true为下划线,false为非下划线   //paint.setStrikeThruText(true); //true为删除线,false为非删除线  }  //随机生成padding值  private voID randompadding() {   padding_left += base_padding_left + random.nextInt(range_padding_left);   padding_top = base_padding_top + random.nextInt(range_padding_top);  } } 

2、编写布局文件,activity_main.xml:

<?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" >   <relativeLayout   androID:layout_wIDth="match_parent"   androID:layout_height="50dp"   androID:background="@color/main_color_white" >   <TextVIEw    androID:ID="@+ID/tv_Title"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:layout_centerInParent="true"    androID:text="找回密码"    androID:textcolor="@color/loan_butBackground"    androID:textSize="20sp" />  </relativeLayout>   <linearLayout   androID:layout_wIDth="match_parent"   androID:layout_height="40dp"   androID:layout_margintop="30dp"   androID:orIEntation="vertical"   androID:layout_marginleft="15dp"   androID:layout_marginRight="15dp"   androID:background="@drawable/security_code_bg">        <linearLayout    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:gravity="center_vertical"    androID:orIEntation="horizontal" >     <TextVIEw     androID:layout_wIDth="wrap_content"     androID:layout_height="wrap_content"     androID:layout_marginleft="20dp"     androID:layout_marginRight="20dp"     androID:text="中国+86"     androID:textcolor="#A2CD5A"     androID:textSize="16sp" />     <VIEw     androID:layout_wIDth="0.1dp"     androID:layout_height="match_parent"     androID:background="@color/loan_butBackground" />     <EditText     androID:ID="@+ID/et_forgetpass_PhoneNum"     androID:layout_wIDth="match_parent"     androID:layout_height="wrap_content"     androID:layout_marginleft="20dp"     androID:background="@null"     androID:digits="0123456789"     androID:hint="请填入您的手机号"     androID:inputType="number"     androID:maxLength="11"     androID:textSize="16sp" />   </linearLayout>  </linearLayout>      <linearLayout      androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   androID:layout_marginleft="15dp"   androID:layout_marginRight="15dp"   androID:layout_margintop="20dp"   androID:orIEntation="horizontal" >    <linearLayout    androID:layout_wIDth="wrap_content"    androID:layout_height="40dp"    androID:background="@drawable/security_code_bg" >     <EditText     androID:ID="@+ID/et_phoneCodes"     androID:layout_wIDth="match_parent"     androID:layout_height="match_parent"     androID:layout_marginleft="10dp"     androID:layout_marginRight="10dp"     androID:background="@null"     androID:hint="请输入右侧验证码" />   </linearLayout>    <ImageVIEw    androID:ID="@+ID/iv_showCode"    androID:layout_wIDth="100dp"    androID:layout_marginleft="10dp"    androID:layout_height="match_parent" />     </linearLayout>   <button   androID:ID="@+ID/but_forgetpass_toSetCodes"   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   androID:layout_margin="20dp"   androID:background="@drawable/buttonshape"   androID:text="提交验证码"   androID:textcolor="@color/main_color_white" />  </linearLayout> 

3、在主界面生成随机数,校验验证码MainActivity.java:

public class MainActivity extends Activity implements OnClickListener {  public static final String TAG = MainActivity.class.getname();  private ImageVIEw iv_showCode;  private EditText et_phoneCode;  private EditText et_phoneNum;  //产生的验证码  private String realCode;   @OverrIDe  protected voID onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentVIEw(R.layout.activity_main);    et_phoneCode = (EditText) findVIEwByID(R.ID.et_phoneCodes);   button but_toSetCode = (button) findVIEwByID(R.ID.but_forgetpass_toSetCodes);   but_toSetCode.setonClickListener(this);   iv_showCode = (ImageVIEw) findVIEwByID(R.ID.iv_showCode);   //将验证码用图片的形式显示出来   iv_showCode.setimageBitmap(Code.getInstance().createBitmap());   realCode = Code.getInstance().getCode().tolowerCase();   iv_showCode.setonClickListener(this);  }   @OverrIDe  public voID onClick(VIEw v) {   switch (v.getID()) {    case R.ID.iv_showCode:     iv_showCode.setimageBitmap(Code.getInstance().createBitmap());     realCode = Code.getInstance().getCode().tolowerCase();     Log.v(TAG,"realCode"+realCode);     break;    case R.ID.but_forgetpass_toSetCodes:     String phoneCode = et_phoneCode.getText().toString().tolowerCase();     String msg = "生成的验证码:"+realCode+"输入的验证码:"+phoneCode;     Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();      if (phoneCode.equals(realCode)) {      Toast.makeText(MainActivity.this,phoneCode + "验证码正确",Toast.LENGTH_SHORT).show();     } else {      Toast.makeText(MainActivity.this,phoneCode + "验证码错误",Toast.LENGTH_SHORT).show();     }     break;   }  } } 

至此,基本功能已实现,源码下载:http://xiazai.jb51.net/201611/yuanma/AndroidSecurityCode(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android随机生成验证码全部内容,希望文章能够帮你解决Android随机生成验证码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142017.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存