android 随机生成验证码

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

随机数和干扰线的颜色随机生成,随机数的样式随机生成。

界面效果如下:


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

[java]  view plain  copy
  1. public class Code {  
  2.   
  3.     //随机数数组  
  4.     private static final char[] CHARS = {  
  5.             '2''3''4''5''6''7''8''9',  
  6.             'a''b''c''d''e''f''g''h''j''k''m',  
  7.             'n''p''q''r''s''t''u''v''w''x''y''z',  
  8.             'A''B''C''D''E''F''G''H''I''J''K''L''M',  
  9.             'N''P''Q''R''S''T''U''V''W''X''Y''Z'  
  10.     };  
  11.   
  12.     private static Code bmpCode;  
  13.   
  14.     public static Code getInstance() {  
  15.         if(bmpCode == null)  
  16.             bmpCode = new Code();  
  17.         return bmpCode;  
  18.     }  
  19.   
  20.     //default settings  
  21.     //验证码默认随机数的个数  
  22.     private static final int DEFAULT_CODE_LENGTH = 4;  
  23.     //默认字体大小  
  24.     private static final int DEFAULT_FONT_SIZE = 25;  
  25.     //默认线条的条数  
  26.     private static final int DEFAULT_LINE_NUMBER = 5;  
  27.     //padding值  
  28.     private static final int BASE_PADDING_LEFT = 10, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20;  
  29.     //验证码的默认宽高  
  30.     private static final int DEFAULT_WIDTH = 100, DEFAULT_HEIGHT = 40;  
  31.   
  32.     //settings decided by the layout xml  
  33.     //canvas width and height  
  34.     private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;  
  35.   
  36.     //random word space and pading_top  
  37.     private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT,  
  38.             base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;  
  39.   
  40.     //number of chars, lines; font size  
  41.     private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;  
  42.   
  43.     //variables  
  44.     private String code;  
  45.     private int padding_left, padding_top;  
  46.     private Random random = new Random();  
  47.     //验证码图片  
  48.     public Bitmap createBitmap() {  
  49.         padding_left = 0;  
  50.   
  51.         Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888);  
  52.         Canvas c = new Canvas(bp);  
  53.   
  54.         code = createCode();  
  55.   
  56.         c.drawColor(Color.WHITE);  
  57.         Paint paint = new Paint();  
  58.         paint.setAntiAlias(true);  
  59.         paint.setTextSize(font_size);  
  60.         //画验证码  
  61.         for (int i = 0; i < code.length(); i++) {  
  62.             randomTextStyle(paint);  
  63.             randomPadding();  
  64.             c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);  
  65.         }  
  66.         //画线条  
  67.         for (int i = 0; i < line_number; i++) {  
  68.             drawLine(c, paint);  
  69.         }  
  70.   
  71.         c.save( Canvas.ALL_SAVE_FLAG );//保存  
  72.         c.restore();//  
  73.         return bp;  
  74.     }  
  75.   
  76.     public String getCode() {  
  77.         return code;  
  78.     }  
  79.   
  80.     //生成验证码  
  81.     private String createCode() {  
  82.         StringBuilder buffer = new StringBuilder();  
  83.         for (int i = 0; i < codeLength; i++) {  
  84.             buffer.append(CHARS[random.nextInt(CHARS.length)]);  
  85.         }  
  86.         return buffer.toString();  
  87.     }  
  88.     //画干扰线  
  89.     private void drawLine(Canvas canvas, Paint paint) {  
  90.         int color = randomColor();  
  91.         int startX = random.nextInt(width);  
  92.         int startY = random.nextInt(height);  
  93.         int stopX = random.nextInt(width);  
  94.         int stopY = random.nextInt(height);  
  95.         paint.setStrokeWidth(1);  
  96.         paint.setColor(color);  
  97.         canvas.drawLine(startX, startY, stopX, stopY, paint);  
  98.     }  
  99.     //生成随机颜色  
  100.     private int randomColor() {  
  101.         return randomColor(1);  
  102.     }  
  103.   
  104.     private int randomColor(int rate) {  
  105.         int red = random.nextInt(256) / rate;  
  106.         int green = random.nextInt(256) / rate;  
  107.         int blue = random.nextInt(256) / rate;  
  108.         return Color.rgb(red, green, blue);  
  109.     }  
  110.     //随机生成文字样式,颜色,粗细,倾斜度  
  111.     private void randomTextStyle(Paint paint) {  
  112.         int color = randomColor();  
  113.         paint.setColor(color);  
  114.         paint.setFakeBoldText(random.nextBoolean());  //true为粗体,false为非粗体  
  115.         float skewX = random.nextInt(11) / 10;  
  116.         skewX = random.nextBoolean() ? skewX : -skewX;  
  117.         paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜  
  118.         //paint.setUnderlineText(true); //true为下划线,false为非下划线  
  119.         //paint.setStrikeThruText(true); //true为删除线,false为非删除线  
  120.     }  
  121.     //随机生成padding值  
  122.     private void randomPadding() {  
  123.         padding_left += base_padding_left + random.nextInt(range_padding_left);  
  124.         padding_top = base_padding_top + random.nextInt(range_padding_top);  
  125.     }  
  126. }  

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

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <RelativeLayout  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="50dp"  
  10.         android:background="@color/main_color_white" >  
  11.         <TextView  
  12.             android:id="@+id/tv_title"  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="wrap_content"  
  15.             android:layout_centerInParent="true"  
  16.             android:text="找回密码"  
  17.             android:textColor="@color/loan_butBackground"  
  18.             android:textSize="20sp" />  
  19.     </RelativeLayout>  
  20.   
  21.     <LinearLayout  
  22.         android:layout_width="match_parent"  
  23.         android:layout_height="40dp"  
  24.         android:layout_marginTop="30dp"  
  25.         android:orientation="vertical"   
  26.         android:layout_marginLeft="15dp"  
  27.         android:layout_marginRight="15dp"  
  28.         android:background="@drawable/security_code_bg">  
  29.   
  30.           
  31.   
  32.         <LinearLayout  
  33.             android:layout_width="match_parent"  
  34.             android:layout_height="match_parent"  
  35.             android:gravity="center_vertical"  
  36.             android:orientation="horizontal" >  
  37.   
  38.             <TextView  
  39.                 android:layout_width="wrap_content"  
  40.                 android:layout_height="wrap_content"  
  41.                 android:layout_marginLeft="20dp"  
  42.                 android:layout_marginRight="20dp"  
  43.                 android:text="中国+86"  
  44.                 android:textColor="#A2CD5A"  
  45.                 android:textSize="16sp" />  
  46.   
  47.             <View  
  48.                 android:layout_width="0.1dp"  
  49.                 android:layout_height="match_parent"  
  50.                 android:background="@color/loan_butBackground" />  
  51.   
  52.             <EditText  
  53.                 android:id="@+id/et_forgetPass_PhoneNum"  
  54.                 android:layout_width="match_parent"  
  55.                 android:layout_height="wrap_content"  
  56.                 android:layout_marginLeft="20dp"  
  57.                 android:background="@null"  
  58.                 android:digits="0123456789"  
  59.                 android:hint="请填入您的手机号"  
  60.                 android:inputType="number"  
  61.                 android:maxLength="11"  
  62.                 android:textSize="16sp" />  
  63.         </LinearLayout>  
  64.     </LinearLayout>  
  65.   
  66.      
  67.   
  68.     <LinearLayout  
  69.           
  70.         android:layout_width="match_parent"  
  71.         android:layout_height="wrap_content"  
  72.         android:layout_marginLeft="15dp"  
  73.         android:layout_marginRight="15dp"  
  74.         android:layout_marginTop="20dp"  
  75.         android:orientation="horizontal" >  
  76.   
  77.         <LinearLayout  
  78.             android:layout_width="wrap_content"  
  79.             android:layout_height="40dp"  
  80.             android:background="@drawable/security_code_bg" >  
  81.   
  82.             <EditText  
  83.                 android:id="@+id/et_phoneCodes"  
  84.                 android:layout_width="match_parent"  
  85.                 android:layout_height="match_parent"  
  86.                 android:layout_marginLeft="10dp"  
  87.                 android:layout_marginRight="10dp"  
  88.                 android:background="@null"  
  89.                 android:hint="请输入右侧验证码" />  
  90.         </LinearLayout>  
  91.   
  92.         <ImageView  
  93.             android:id="@+id/iv_showCode"  
  94.             android:layout_width="100dp"  
  95.             android:layout_marginLeft="10dp"  
  96.             android:layout_height="match_parent" />  
  97.           
  98.     </LinearLayout>  
  99.   
  100.     <Button  
  101.         android:id="@+id/but_forgetpass_toSetCodes"  
  102.         android:layout_width="match_parent"  
  103.         android:layout_height="wrap_content"  
  104.         android:layout_margin="20dp"  
  105.         android:background="@drawable/buttonshape"  
  106.         android:text="提交验证码"  
  107.         android:textColor="@color/main_color_white" />  
  108.   
  109. </LinearLayout>  

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

[java]  view plain  copy
  1. public class MainActivity extends Activity implements OnClickListener {  
  2.     public static final String TAG = MainActivity.class.getName();  
  3.     private ImageView iv_showCode;  
  4.     private EditText et_phoneCode;  
  5.     private EditText et_phoneNum;  
  6.     //产生的验证码  
  7.     private String realCode;  
  8.   
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.   
  14.         et_phoneCode = (EditText) findViewById(R.id.et_phoneCodes);  
  15.         Button but_toSetCode = (Button) findViewById(R.id.but_forgetpass_toSetCodes);  
  16.         but_toSetCode.setOnClickListener(this);  
  17.         iv_showCode = (ImageView) findViewById(R.id.iv_showCode);  
  18.         //将验证码用图片的形式显示出来  
  19.         iv_showCode.setImageBitmap(Code.getInstance().createBitmap());  
  20.         realCode = Code.getInstance().getCode().toLowerCase();  
  21.         iv_showCode.setOnClickListener(this);  
  22.     }  
  23.   
  24.     @Override  
  25.     public void onClick(View v) {  
  26.         switch (v.getId()) {  
  27.             case R.id.iv_showCode:  
  28.                 iv_showCode.setImageBitmap(Code.getInstance().createBitmap());  
  29.                 realCode = Code.getInstance().getCode().toLowerCase();  
  30.                 Log.v(TAG,"realCode"+realCode);  
  31.                 break;  
  32.             case R.id.but_forgetpass_toSetCodes:  
  33.                 String phoneCode = et_phoneCode.getText().toString().toLowerCase();  
  34.                 String msg = "生成的验证码:"+realCode+"输入的验证码:"+phoneCode;  
  35.                 Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();  
  36.   
  37.                 if (phoneCode.equals(realCode)) {  
  38.                     Toast.makeText(MainActivity.this, phoneCode + "验证码正确", Toast.LENGTH_SHORT).show();  
  39.                 } else {  
  40.                     Toast.makeText(MainActivity.this, phoneCode + "验证码错误", Toast.LENGTH_SHORT).show();  
  41.                 }  
  42.                 break;  
  43.         }  
  44.     }  
  45. }  

至此,基本功能已实现,如有疑问欢迎留言或加群讨论:196615382,如需源码,点击下载。。。




转载至:http://blog.csdn.net/wk843620202/article/details/50960904

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值