Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android生成驗證碼

Android生成驗證碼

編輯:關於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布局:

 

 



    


實現簡單的功能:

 

 

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();
			}
		});
	}
}
一般使用方式都是獲取自動生成的驗證碼,再從輸入框獲取用戶輸入的驗證碼,相互比對就可以得出結果。


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved