http://my.eoe.cn/1169143/archive/22006.html
来源:互联网 发布:易语言源码 编辑:程序博客网 时间:2024/06/07 05:43
Android验证码的生成
验证码是我们在程序开发中不可或缺的一个功能模块,这里我为大家推荐一个比较简洁的验证码图片生成方法(已注释)。
1.新建一个BPUtil验证码工具类,作用是生成验证码图片,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
package com.my.nask.captcha.utils;import java.util.Random;import com.my.captcha.R;import android.graphics.Bitmap;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Bitmap.Config;/** * 验证码工具类 */public class BPUtil { /** 生成验证码的字符数组集合 **/ 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 BPUtil bpUtil; public static BPUtil getInstance() { if (bpUtil == null) bpUtil = new BPUtil(); return bpUtil; } // width="60" height="30" // base_padding_left="5" // range_padding_left="10" // base_padding_top="15" // range_padding_top="10" // codeLength="4" // line_number="3" // font_size="20" // default settings private static final int DEFAULT_CODE_LENGTH = 4; private static final int DEFAULT_FONT_SIZE = 20; private static final int DEFAULT_LINE_NUMBER = 3; private static final int BASE_PADDING_LEFT = 5, RANGE_PADDING_LEFT = 10, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 10; private static final int DEFAULT_WIDTH = 60, DEFAULT_HEIGHT = 30; // 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(); /** * 新建一个Bitmap对象 */ public Bitmap createBitmap() { padding_left = 0; Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas c = new Canvas(bp); code = createCode(); // 验证码图片背景色 c.drawColor(R.color.whiteColor); 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 bpUtil.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); }}
2.调用方法生成验证码图片,执行代码如下:
// 生成验证码并显示
1
img.setImageBitmap(BPUtil.getInstance().createBitmap());
就这样简单的两步,我们就可以完成验证码图片的生成了,具体需求大家可根据实际情况改写
0 0
- http://my.eoe.cn/1169143/archive/22006.html
- Android开发规范详解(原地址 http://my.eoe.cn/yyz168/archive/5551.html)
- 程序员在面试中遇到的10大算法类型!(http://news.eoe.cn/17989.html)
- Spring DataSource Demo(http://blog.cnbb.com.cn/shiningsb/archive/2007/02/06/41623.html)
- 《JAVA与模式》之观察者模式--http://www.cnblogs.com/java-my-life/archive/2012/05/16/2502279.html
- Ubuntu 14.04 source list: http://cn.archive.ubuntu.com
- 简单实用的铁道部12306.cn网站自动化登录(转http://www.cnblogs.com/cnshangsha/archive/2012/01/12/12306cnautologin.html)
- http://www.xrss.cn/Info/5682.Html
- http://www.seasar.org/cn/index.html
- http://www.gpsty.cn/webtoy.html
- http://blog.pfan.cn/sword2008/29437.html
- http://www.linuxgraphics.cn/home/index.html
- http://coolshell.cn/articles/5815.html
- http://coolshell.cn/articles/5426.html
- http://liancheng.info/llvm-tutorial-cn/html/#
- http://coolshell.cn/articles/5426.html
- http://www.loveq.cn/profile-wang091633.html
- http://www.loveq.cn/profile-wang091633.html
- c和fortran 混编 编译备忘
- IOS NSStringFromCGRect
- 哦IDG哈皮搜到过哈哈十多个hi哦
- poj1068
- 9静态路由
- http://my.eoe.cn/1169143/archive/22006.html
- 腾讯互联QQ登录第三方Android应用
- java接口学习例子(转载)
- 计算图片缩小比例
- 学Android有前途吗?
- 随机生成和为S的N个数
- Eclipse 在开发中使用到的快捷键 提高开发效率
- JAVA源码注释---hashmap源码中文注释
- 手机里通讯录被清除了如何找回