廢話不多說(shuō)了,下面一段代碼給大家分享android 生成栗子圖片驗(yàn)證碼功能,具體代碼如下所示;
成都創(chuàng)新互聯(lián)專注于企業(yè)網(wǎng)絡(luò)營(yíng)銷推廣、網(wǎng)站重做改版、濱海網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5場(chǎng)景定制、成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站制作、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為濱海等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
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; public class Autjcode { private static Autjcode bmpCode; private int width = 100, height = 60; private int base_padding_left = 15, range_padding_left = 5, base_padding_top = 25, range_padding_top = 30; private int codeLength = 4, line_number = 2, font_size = 25; private String code; private int padding_left, padding_top; private static final char[] CHARS = { '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', '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', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; /** * 獲得實(shí)例 * * @return */ public static Autjcode getInstance() { if (bmpCode == null) bmpCode = new Autjcode(); return bmpCode; } private Random random = new Random(); /** * 創(chuàng)建位圖 * * @return */ public Bitmap createBitmap() { padding_left = 0; // ARGB_8888表示為32位的ARGB位圖 Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas c = new Canvas(bp); code = createCode(); c.drawColor(Color.WHITE); Paint paint = new Paint(); paint.setTextSize(font_size); paint.setFakeBoldText(true); 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.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(); } /** * 劃線 * * @param canvas * @param paint */ 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); } 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); } private void randomTextStyle(Paint paint) { int color = randomColor(); paint.setColor(color); paint.setFakeBoldText(random.nextBoolean()); float skewX = random.nextInt(11) / 10; skewX = random.nextBoolean() ? skewX : -skewX; paint.setTextSkewX(skewX); } private void randomPadding() { padding_left += base_padding_left + random.nextInt(range_padding_left); padding_top = base_padding_top + random.nextInt(range_padding_top); } }
使用方法:
初始化:
registerAuthimg = (ImageView) findViewById(R.id.registerAuthimg); registerAuthimg.setImageBitmap(Autjcode.getInstance().createBitmap());
驗(yàn)證碼刷新:
registerAuthimg.setImageBitmap(Autjcode.getInstance().createBitmap());
獲取當(dāng)前顯示的文字驗(yàn)證碼:
String Autecodeimg = Autjcode.getInstance().getCode().toUpperCase();
總結(jié)
以上所述是小編給大家介紹的Android栗子の圖片驗(yàn)證碼生成實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!
當(dāng)前名稱:Android栗子の圖片驗(yàn)證碼生成實(shí)例代碼
分享URL:http://jinyejixie.com/article44/ipjcee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作、小程序開發(fā)、網(wǎng)站策劃、、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)