Android產生驗證碼

來源:互聯網
上載者:User

標籤: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];//驗證碼@Overrideprotected 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() {// 點擊圖片,更換驗證碼@Overridepublic void onClick(View arg0) {code = validateView.createAndSetValidateCode();validateView.invalidate();}});}}
一般使用方式都是擷取自動產生的驗證碼,再從輸入框擷取使用者輸入的驗證碼,相互比對就可以得出結果。


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Android產生驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.