码迷,mamicode.com
首页 > 移动开发 > 详细

Android生成验证码

时间:2015-08-07 19:44:37      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:android   验证码   

先放上这个小Demo的图片:点击图片可以更换新的验证码。

技术分享

验证码是以图片的形式显示的,所以需要写一个自定义验证码控件

public class ValidateView extends View {

	/**
	 * 点数
	 */
	private int pointNum = 150;// 背景杂质
	/**
	 * 线段数
	 */
	private int lineNum = 3;// 背景杂质
	/**
	 * 验证码字长
	 */
	private int validateCodeLenght = 6;// 默认长度为6
	/**
	 * 验证码
	 */
	private String[] validateCode;
	private Paint mTempPaint = new Paint();
	private Context mContext;

	/**
	 * 验证码内容
	 */
	private static final String[] strContent = new String[] { "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" };

	public ValidateView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		mTempPaint.setAntiAlias(true);
		mTempPaint.setTextSize(50);
		mTempPaint.setStrokeWidth(3);
		validateCode = new String[validateCodeLenght];// 验证码
	}

	public String[] getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String[] validateCode) {
		this.validateCode = validateCode;
	}

	public void onDraw(Canvas canvas) {
		canvas.drawColor(Color.GREEN);

		// 绘制验证码
		final int height = getHeight();
		final int width = getWidth();
		int dx = 30;

		for (int i = 0; i < validateCodeLenght; i++) {
			canvas.drawText("" + validateCode[i], dx, getPositon(height), mTempPaint);
			dx += width / (validateCodeLenght + 1);
		}
		int[] line;

		for (int i = 0; i < lineNum; i++) {
			line = getLine(height, width);
			canvas.drawLine(line[0], line[1], line[2], line[3], mTempPaint);
		}
		// 绘制小圆点
		int[] point;
		for (int i = 0; i < pointNum; i++) {
			point = getPoint(height, width);
			canvas.drawCircle(point[0], point[1], 1, mTempPaint);
		}
	}

	/**
	 * 更新验证码显示
	 */
	public void invaliChenkNum() {
		invalidate();
	}

	/**
	 * 生成验证码,并显示在图片上
	 * 
	 * @return
	 */
	public String[] createAndSetValidateCode() {
		String[] tempValidateCode = new String[validateCodeLenght];
		for (int i = 0; i < validateCodeLenght; i++) {
			tempValidateCode[i] = strContent[(int) (Math.random() * strContent.length)];
		}
		validateCode = tempValidateCode;
		return tempValidateCode;
	}

	/**
	 * 生成验证码
	 * 
	 * @return
	 */
	public String[] createCheckNum() {
		String[] tempCheckNum = new String[validateCodeLenght];
		for (int i = 0; i < validateCodeLenght; i++) {
			tempCheckNum[i] = strContent[(int) (Math.random() * strContent.length)];
		}
		return tempCheckNum;
	}

	private int[] getLine(int height, int width) {
		int[] tempCheckNum = new int[validateCodeLenght];
		for (int i = 0; i < validateCodeLenght; i += 2) {
			tempCheckNum[i] = (int) (Math.random() * width);
			tempCheckNum[i + 1] = (int) (Math.random() * height);
		}
		return tempCheckNum;
	}

	private int[] getPoint(int height, int width) {
		int[] tempCheckNum = new int[validateCodeLenght];
		tempCheckNum[0] = (int) (Math.random() * width);
		tempCheckNum[1] = (int) (Math.random() * height);
		return tempCheckNum;
	}

	/**
	 * 验证是否正确,单元测试通过
	 * 
	 * @param userCheck
	 *            用户输入的验证码
	 * @param ValidateCode
	 *            自动生成的验证码
	 * @return
	 */
	public boolean validateCode(String userCheck, String[] code) {
		if (userCheck.length() != validateCodeLenght) {
			return false;
		}
		String checkString = "";
		for (int i = 0; i < validateCodeLenght; i++) {
			checkString += code[i];
		}
		if (userCheck.equals(checkString)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取验证码的纵坐标
	 * 
	 * @param height
	 * @return
	 */
	public int getPositon(int height) {
		int tempPositoin = (int) (Math.random() * height);
		if (tempPositoin < 25) {
			tempPositoin += 25;
		}
		return tempPositoin;
	}

	public int getPointNum() {
		return pointNum;
	}

	public void setPointNum(int pointNum) {
		this.pointNum = pointNum;
	}

	public int getLineNum() {
		return lineNum;
	}

	public void setLineNum(int lineNum) {
		this.lineNum = lineNum;
	}

	public int getValidateCodeLenght() {
		return validateCodeLenght;
	}

	public void setValidateCodeLenght(int validateCodeLenght) {
		this.validateCodeLenght = validateCodeLenght;
		validateCode = new String[validateCodeLenght];// 验证码
	}

}
简单的XML布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <com.example.validateviewdemo.ValidateView
        android:id="@+id/validateView"
        android:layout_width="180dp"
        android:layout_height="50dp" />

</LinearLayout>

实现简单的功能:

public class MainActivity extends Activity {

	ValidateView validateView;
	private static final int LENGHT = 6;//验证码的长度
	private String[] code = new String[LENGHT];//验证码

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		validateView = (ValidateView) findViewById(R.id.validateView);
		validateView.setValidateCodeLenght(LENGHT);
		code = validateView.createAndSetValidateCode();
		validateView.setOnClickListener(new OnClickListener() {
			// 点击图片,更换验证码
			@Override
			public void onClick(View arg0) {
				code = validateView.createAndSetValidateCode();
				validateView.invalidate();
			}
		});
	}
}
一般使用方式都是获取自动生成的验证码,再从输入框获取用户输入的验证码,相互比对就可以得出结果。


版权声明:本文为博主原创文章,未经博主允许不得转载。

Android生成验证码

标签:android   验证码   

原文地址:http://blog.csdn.net/u014375869/article/details/47343655

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!