android 实现随机生成图片验证码
程序员文章站
2022-03-19 22:13:22
在app开发中,根据需求不同,有时我们需要随机生成图片验证码,点击可以随机切换,其实很简单,接下来我们就来看看把。
首先我们使用如下工具类,其实内部原理很简单,运用随机数随机将0-9数字和a-z字母...
在app开发中,根据需求不同,有时我们需要随机生成图片验证码,点击可以随机切换,其实很简单,接下来我们就来看看把。
首先我们使用如下工具类,其实内部原理很简单,运用随机数随机将0-9数字和a-z字母随机排列的方式进行组合,效果图如下:
接下来我们就直接上代码把:
package com.example.administrator.panada; import android.graphics.bitmap; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import java.util.random; public class codeutils { // 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 final char[] chars = { '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 codeutils mcodeutils; private int mpaddingleft, mpaddingtop; private stringbuilder mbuilder = new stringbuilder(); private random mrandom = new random(); //default settings // private static final int default_code_length = 6;//验证码的长度 这里是6位 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 = 40; //左边距 private static final int range_padding_left = 30;//左边距范围值 private static final int base_padding_top = 70;//上边距 private static final int range_padding_top = 15;//上边距范围值 private static final int default_width = 300;//默认宽度.图片的总宽 private static final int default_height = 100;//默认高度.图片的总高 private static final int default_color = 0xdf;//默认背景颜色值 private string code; public static codeutils getinstance() { if (mcodeutils == null) { mcodeutils = new codeutils(); } return mcodeutils; } //生成验证码图片 返回类型为bitmap 直接用imageview.setbitmap()即可 public bitmap createbitmap() { mpaddingleft = 0; //每次生成验证码图片时初始化 mpaddingtop = 0; bitmap bitmap = bitmap.createbitmap(default_width, default_height, bitmap.config.argb_8888); canvas canvas = new canvas(bitmap); code = createcode(); canvas.drawcolor(color.rgb(default_color, default_color, default_color)); paint paint = new paint(); paint.settextsize(default_font_size); for (int i = 0; i < code.length(); i++) { randomtextstyle(paint); randompadding(); canvas.drawtext(code.charat(i) + "", mpaddingleft, mpaddingtop, paint); } //干扰线 for (int i = 0; i < default_line_number; i++) { drawline(canvas, paint); } canvas.save(canvas.all_save_flag);//保存 canvas.restore(); return bitmap; } /** * 得到图片中的验证码字符串 * * @return */ public string getcode() { return code; } //生成验证码 public string createcode() { mbuilder.delete(0, mbuilder.length()); //使用之前首先清空内容 for (int i = 0; i < default_code_length; i++) { mbuilder.append(chars[mrandom.nextint(chars.length)]); } return mbuilder.tostring(); } //生成干扰线 private void drawline(canvas canvas, paint paint) { int color = randomcolor(); int startx = mrandom.nextint(default_width); int starty = mrandom.nextint(default_height); int stopx = mrandom.nextint(default_width); int stopy = mrandom.nextint(default_height); paint.setstrokewidth(1); paint.setcolor(color); canvas.drawline(startx, starty, stopx, stopy, paint); } //随机颜色 private int randomcolor() { mbuilder.delete(0, mbuilder.length()); //使用之前首先清空内容 string haxstring; for (int i = 0; i < 3; i++) { haxstring = integer.tohexstring(mrandom.nextint(0xff)); if (haxstring.length() == 1) { haxstring = "0" + haxstring; } mbuilder.append(haxstring); } return color.parsecolor("#" + mbuilder.tostring()); } //随机文本样式 private void randomtextstyle(paint paint) { int color = randomcolor(); paint.setcolor(color); paint.setfakeboldtext(mrandom.nextboolean()); //true为粗体,false为非粗体 float skewx = mrandom.nextint(11) / 10; skewx = mrandom.nextboolean() ? skewx : -skewx; paint.settextskewx(skewx); //float类型参数,负数表示右斜,整数左斜 // paint.setunderlinetext(true); //true为下划线,false为非下划线 // paint.setstrikethrutext(true); //true为删除线,false为非删除线 } //随机间距 private void randompadding() { mpaddingleft += base_padding_left + mrandom.nextint(range_padding_left); mpaddingtop = base_padding_top + mrandom.nextint(range_padding_top); } }
来看看布局代码,也就是一个imageview
来看mainactivity中的代码吧:
public class mainactivity extends appcompatactivity implements view.onclicklistener { private imageview img; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initview(); } private void initview() { img = (imageview) findviewbyid(r.id.img);
??????
img.setimagebitmap(codeutils.getinstance().createbitmap()); //我们在控件初始化时设置随机生成图片验证码,防止运行第一次出现图片白色情况img.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string code = codeutils.getinstance().getcode(); //获取图片验证码上的内容 img.setimagebitmap(codeutils.getinstance().createbitmap()); //随机生成图片验证码 } });代码很简单,有木有学会呢?