Android手机上生成随机验证码(详细注释)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cctvjiatao.authcode"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

package com.cctvjiatao.authcode;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
/**
 * @作者: jiatao
 * @修改时间:2016-4-13 上午7:17:40
 * @包名:com.cctvjiatao.authcode
 * @文件名:MainActivity.java
 * @版权声明:www.cctvjiatao.com
 * @功能: 实现客户端生成随机验证码
 */
public class MainActivity extends Activity {
	
	private final String TAG = getClass().getSimpleName();

	private ImageView iv_image; // 图标
	private Button btn_refresh, btn_ok; // 确定和刷新验证码
	private String codeStrGet = null; // 获取验证码的值
	private EditText edt_code; // 文本框的值

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		iv_image = (ImageView) findViewById(R.id.iv_image);
		iv_image.setImageBitmap(Code.getInstance().getBitmap());
		edt_code = (EditText) findViewById(R.id.edt_code);

		codeStrGet = Code.getInstance().getCodeToLower(); // 获取显示的验证码(已转为小写)
		Log.e(TAG, "获取到已转换的验证码:" + codeStrGet);
		
		btn_refresh = (Button) findViewById(R.id.btn_refresh);
		btn_refresh.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				iv_image.setImageBitmap(Code.getInstance().getBitmap());
				codeStrGet = Code.getInstance().getCodeToLower();
				Log.e(TAG, "刷新后,获取到已转换的验证码:" + codeStrGet);
			}
		});

		btn_ok = (Button) findViewById(R.id.btn_ok);
		btn_ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String codeStrInput = edt_code.getText().toString().trim();//获得输入框中的验证码
				Log.e(TAG, "输入的原生验证码:" + codeStrInput);
				if (codeStrInput == null || codeStrInput.equals("")) {
					Toast.makeText(MainActivity.this, "没有填写验证码", 2).show();
				} else if (!codeStrInput.toLowerCase().equals(codeStrGet)) {//将输入框中的验证码转换为小写,再和生成的二维码对比
					Toast.makeText(MainActivity.this, "验证码填写不正确", 2).show();
				} else {
					Toast.makeText(MainActivity.this, "操作成功", 2).show();
				}

			}
		});
	}
}

Code.java

package com.cctvjiatao.authcode;

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;
import android.util.Log;

/**
 * @作者: jiatao
 * @修改时间:2016-4-13 上午7:17:40
 * @包名:com.cctvjiatao.authcode
 * @文件名:Code.java
 * @版权声明:www.cctvjiatao.com
 * @功能: 验证码生成类
 */
public class Code {
	
	private final String TAG = getClass().getSimpleName();
	
	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 Code bpUtil;

	private Code() {
	};

	public static Code getInstance() {
		if (bpUtil == null)
			bpUtil = new Code();
		return bpUtil;
	}

	// 验证码相关参数默认值
	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;// 默认背景颜色值

	//声明变量,并赋值
	private int width = DEFAULT_WIDTH;// 默认宽度.图片的总宽
	private int height = DEFAULT_HEIGHT;// 默认高度.图片的总高

	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;// 上边距范围值

	private int codeLength = DEFAULT_CODE_LENGTH;// 验证码的长度 这里是4位
	private int line_number = DEFAULT_LINE_NUMBER;// 多少条干扰线
	private int font_size = DEFAULT_FONT_SIZE;// 字体大小

	private String codeStr;// 保存生成的验证码
	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);//把Bitmap图像绘制到画布上

		codeStr = createCode();//生成二维码
		Log.e(TAG, "原生验证码:" + codeStr);

		c.drawColor(Color.rgb(DEFAULT_COLOR, DEFAULT_COLOR, DEFAULT_COLOR));//填充画布颜色
		Paint paint = new Paint();//初始化画笔
		paint.setTextSize(font_size);//设置画笔大小,表现为字符大小

		//画验证码
		for (int i = 0; i < codeStr.length(); i++) {
			randomTextStyle(paint);
			randomPadding();
			c.drawText(codeStr.charAt(i) + "", padding_left, padding_top, paint);
		}

		//画干扰线
		for (int i = 0; i < line_number; i++) {
			drawCustomLine(c, paint);
		}

		c.save(Canvas.ALL_SAVE_FLAG);// 保存(基于saveFlags,可以保存当前模型和修剪到一个私有堆栈)
		c.restore();
		return bp;
	}

	/**
	 * 将验证码转换为小写
	 * 		这样只需再将输入框中的验证码也转换为小写,二者再做异同比较即可判断两个验证码是否一致了
	 * 注:codeStr是本类的私有(private)变量,所以外部类不能直接取得,只有通过getCodeToLower()这个公有(public)方法才可以取得
	 */
	public String getCodeToLower() {
		return codeStr.toLowerCase();
	}

	/**
	 * 获得生成的二维码图像
	 * 注:createBitmap是本类的私有(private)方法,所以外部类不能直接调用,只有通过getBitmap()这个公有(public)方法才可以调用
	 */
	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 drawCustomLine(Canvas canvas, Paint paint) {
		int color = randomColor(1);
		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 void randomTextStyle(Paint paint) {
		int color = randomColor(1);
		paint.setColor(color);
		paint.setFakeBoldText(random.nextBoolean()); // random.nextBoolean() 返回一个布尔伪随机均匀分布,true或false。 true为粗体,false为非粗体
		float skewX = random.nextInt(11) / 10;//random.nextInt(int n) 返回一个int伪随机数均匀分布在[0,n)。
		skewX = random.nextBoolean() ? skewX : -skewX; // float类型参数,负数表示右斜,整数左斜
		paint.setTextSkewX(skewX); 
		// paint.setUnderlineText(true); //true为下划线,false为非下划线
		// paint.setStrikeThruText(true); //true为删除线,false为非删除线
	}
	
	/**
	 * 生成随机颜色
	 */
	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 randomPadding() {
		padding_left += base_padding_left + random.nextInt(range_padding_left);
		padding_top = base_padding_top + random.nextInt(range_padding_top);
	}
}

activity_main.xml

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.cctvjiatao.authcode.MainActivity" >

    <LinearLayout
        android:id="@+id/ll_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ll_username"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:" />

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_code"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ll_pwd"
        android:layout_marginTop="4dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:orientation="horizontal" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="5dp"
                android:text="验 证 码:"
                android:textColor="#000000" />

            <EditText
                android:id="@+id/edt_code"
                android:layout_width="60dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:maxLength="4"
                android:paddingBottom="10dp"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:textColor="#000000"
                android:textSize="14sp" />
        </LinearLayout>

        <ImageView
            android:id="@+id/iv_image"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginBottom="10dp"
            android:layout_marginTop="5dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btn_refresh"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp"
            android:background="@android:color/transparent"
            android:text="刷新"
            android:textColor="#7f7f7f"
            android:textSize="12sp"
            android:textStyle="italic" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/ll_code"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_ok"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="确定" />
    </LinearLayout>

</RelativeLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值