android端实现验证码随机生成功能
程序员文章站
2024-02-08 09:57:10
本文实例为大家分享了android端生成随机验证码的具体代码,供大家参考,具体内容如下
package com.nobeg.util;
import j...
本文实例为大家分享了android端生成随机验证码的具体代码,供大家参考,具体内容如下
package com.nobeg.util; import java.util.random; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.bitmap.config; public class code { 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' }; private static code bmpcode; public static code getinstance() { if(bmpcode == null) bmpcode = new code(); return bmpcode; } //default settings private static final int default_code_length = 3; private static final int default_font_size = 25; private static final int default_line_number = 2; private static final int base_padding_left = 5, range_padding_left = 15, base_padding_top = 15, range_padding_top = 20; private static final int default_width = 60, 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(); //验证码图片 public bitmap createbitmap() { padding_left = 0; 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); 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; } //验证码 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); } 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()); //true为粗体,false为非粗体 float skewx = random.nextint(11) / 10; skewx = random.nextboolean() ? skewx : -skewx; paint.settextskewx(skewx); //float类型参数,负数表示右斜,整数左斜 // paint.setunderlinetext(true); //true为下划线,false为非下划线 // paint.setstrikethrutext(true); //true为删除线,false为非删除线 } private void randompadding() { padding_left += base_padding_left + random.nextint(range_padding_left); padding_top = base_padding_top + random.nextint(range_padding_top); } }
java代码:
yanzhengma_img = (imageview) findviewbyid(r.id.yanzhengma_img); yanzhengma_huan = (button) findviewbyid(r.id.yanzhengma_huan); yanzhengma_val = (edittext) findviewbyid(r.id.yanzhengma_val); yanzhengma_img.setimagebitmap(code.getinstance().createbitmap()); system.out.println("验证码:"+code.getinstance().getcode()); yanzhengma_huan.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { yanzhengma_img.setimagebitmap(code.getinstance().createbitmap()); system.out.println("验证码:"+code.getinstance().getcode()); } });
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。