Android實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼
本文實(shí)例為大家分享了Android驗(yàn)證碼的隨機(jī)生成代碼,供大家參考,具體內(nèi)容如下
Code.java
package com.example.myapp;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import java.util.Random;
public class Code {
/**
* 隨機(jī)數(shù)數(shù)組
* 去除了易混淆的 數(shù)字 0 和 字母 o O
* 數(shù)字 1 和 字母 i I l L
* 數(shù)字 6 和 字母 b
* 數(shù)字 9 和 字母 q
* 字母 c C 和 G
* 字母 t (經(jīng)常和隨機(jī)線混在一起看不清)
*/
private static final char[] CHARS = {
'2', '3', '4', '5', '7', '8',
'a', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm',
'n', 'p', 'r', 's', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'D', 'E', 'F', 'H', 'J', 'K', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
};
private static Code bmpCode;
public static Code getInstance() {
if(bmpCode == null)
bmpCode = new Code();
return bmpCode;
}
//default settings
//驗(yàn)證碼默認(rèn)隨機(jī)數(shù)的個(gè)數(shù)
private static final int DEFAULT_CODE_LENGTH = 4;
//默認(rèn)字體大小
private static final int DEFAULT_FONT_SIZE = 25;
//默認(rèn)線條的條數(shù)
private static final int DEFAULT_LINE_NUMBER = 5;
//padding值
private static final int BASE_PADDING_LEFT = 10, RANGE_PADDING_LEFT = 15, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 20;
//驗(yàn)證碼的默認(rèn)寬高
private static final int DEFAULT_WIDTH = 100, DEFAULT_HEIGHT = 40;
//settings decided by the layout xml
//canvas width and height
private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;
//random word space and pading_top
private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT,
base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;
//number of chars, lines; font size
private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;
//variables
private String code;
private int padding_left, padding_top;
private Random random = new Random();
//驗(yàn)證碼圖片
public Bitmap createBitmap() {
padding_left = 0;
Bitmap bp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bp);
code = createCode();
c.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(font_size);
//畫驗(yàn)證碼
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.save();//保存
c.restore();//
return bp;
}
public String getCode() {
return code;
}
//生成驗(yàn)證碼
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);
}
//生成隨機(jī)顏色
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);
}
//隨機(jī)生成文字樣式,顏色,粗細(xì),傾斜度
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類型參數(shù),負(fù)數(shù)表示右斜,整數(shù)左斜
//paint.setUnderlineText(true); //true為下劃線,false為非下劃線
//paint.setStrikeThruText(true); //true為刪除線,false為非刪除線
}
//隨機(jī)生成padding值
private void randomPadding() {
padding_left += base_padding_left + random.nextInt(range_padding_left);
padding_top = base_padding_top + random.nextInt(range_padding_top);
}
}
調(diào)用
//將驗(yàn)證碼用圖片的形式顯示出來 RegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap()); realCode = Code.getInstance().getCode().toLowerCase();
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)圓圈倒計(jì)時(shí)
- Android實(shí)現(xiàn)倒計(jì)時(shí)的方案梳理
- Android?使用flow實(shí)現(xiàn)倒計(jì)時(shí)的方式
- Android實(shí)現(xiàn)一個(gè)倒計(jì)時(shí)自定義控件
- Android自定義View實(shí)現(xiàn)隨機(jī)數(shù)驗(yàn)證碼
- Android自定義驗(yàn)證碼輸入框的方法實(shí)例
- Android實(shí)現(xiàn)短信驗(yàn)證碼輸入框
- Android滑動(dòng)拼圖驗(yàn)證碼控件使用方法詳解
- OpenHarmony實(shí)現(xiàn)類Android短信驗(yàn)證碼及倒計(jì)時(shí)流程詳解
相關(guān)文章
Android編程實(shí)現(xiàn)加載等待ProgressDialog的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)加載等待ProgressDialog的方法,實(shí)例分析了Android中加載等待類ProgressDialog的具體使用方法,需要的朋友可以參考下2015-12-12
詳解Android開發(fā)中Activity的四種launchMode
這篇文章主要介紹了Android開發(fā)中Activity的四種launchMode,launchMode主要用于控制多個(gè)Activity間的跳轉(zhuǎn),需要的朋友可以參考下2016-03-03
Android中實(shí)現(xiàn)長(zhǎng)按修改ListView對(duì)象的內(nèi)容
這篇文章主要給大家介紹了在Android中實(shí)現(xiàn)長(zhǎng)按修改ListView對(duì)象內(nèi)容的相關(guān)資料,文中給出了完整的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-02-02
Android CoordinatorLayout詳解及實(shí)例代碼
這篇文章主要介紹了Android CoordinatorLayout詳解及實(shí)例代碼的相關(guān)資料,CoordinatorLayout基本實(shí)現(xiàn)兩個(gè)功能: 作為頂層布局 和調(diào)度協(xié)調(diào)子布局,這里詳細(xì)介紹此部分知識(shí),需要的朋友可以參考下2016-12-12
android輕量級(jí)無侵入式管理數(shù)據(jù)庫自動(dòng)升級(jí)組件
這篇文章主要為大家介紹了android輕量級(jí)無侵入式管理數(shù)據(jù)庫自動(dòng)升級(jí)組件詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

