首页 > 代码库 > Android开发中验证码的生成

Android开发中验证码的生成

最近在做电商金融类的项目,验证码的生成方法必不可少,先学习了一种,经过测试好用,从别处学习的代码,稍改动了一下可选择是否支持识别大小写。直接上代码。

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; // 验证码图标
 private String getCode = null; // 获取验证码的值
 private EditText vc_code; // 文本框的值

 @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); // 获取显示的验证码
  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(), "验证码输入正确", Toast.LENGTH_LONG).show();
    } else {
     Toast.makeText(getApplicationContext(), "验证码输入错误", 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:验证码生成类
 */
public class 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;// 验证码的长度 这里是4位
 private static final int DEFAULT_FONT_SIZE = 60;// 字体大小
 private static final int DEFAULT_LINE_NUMBER = 3;// 多少条干扰线
 private static final int BASE_PADDING_LEFT = 20; // 左边距
 private static final int RANGE_PADDING_LEFT = 35;// 左边距范围值
 private static final int BASE_PADDING_TOP = 42;// 上边距
 private static final int RANGE_PADDING_TOP = 15;// 上边距范围值
 private static final int DEFAULT_WIDTH = 200;// 默认宽度.图片的总宽
 private static final int DEFAULT_HEIGHT = 70;// 默认高度.图片的总高
 private final int DEFAULT_COLOR = 0xdf;// 默认背景颜色值

 // 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;// 保存生成的验证码
 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);// 保存
  c.restore();//
  return bp;
 }

 /**
  * @Description:是否区分大小写
  */
 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(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为非删除线
 }

 private void randomPadding() {
  padding_left += base_padding_left + random.nextInt(range_padding_left);
  padding_top = base_padding_top + random.nextInt(range_padding_top);
 }
}

布局文件
<?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="获取验证码" />
    <EditText
        android:id="@+id/myedit"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:hint="请输入验证码" />
    <Button
        android:id="@+id/verfiy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="验证输入的验证码是否正确" />
</LinearLayout>



Android开发中验证码的生成