欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android随机生成验证码

程序员文章站 2024-03-01 08:14:22
android随机生成验证码,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

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