android生成验证码bitmap

不多说了,直接上代码,项目中用到的,未做优化,还有很多参数未设置。

[java] view plaincopy

1.import java.util.Random;
2.
3.import android.graphics.Bitmap;
4.import android.graphics.Canvas;
5.import android.graphics.Color;
6.import android.graphics.Paint;
7.import android.graphics.Bitmap.Config;
8.
9.public class BPUtil {
10.
11.    private static final char[] CHARS = {
12.        ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘,
13.        ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘,
14.        ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘,
15.        ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘,
16.        ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘
17.    };
18.
19.    private static BPUtil bpUtil;
20.
21.    public static BPUtil getInstance() {
22.        if(bpUtil == null)
23.            bpUtil = new BPUtil();
24.        return bpUtil;
25.    }
26.
27.//  width="60" height="30"
28.//          base_padding_left="5"
29.//          range_padding_left="10"
30.//          base_padding_top="15"
31.//          range_padding_top="10"
32.//          codeLength="4"
33.//          line_number="3"
34.//          font_size="20"
35.
36.    //default settings
37.    private static final int DEFAULT_CODE_LENGTH = 4;
38.    private static final int DEFAULT_FONT_SIZE = 20;
39.    private static final int DEFAULT_LINE_NUMBER = 3;
40.    private static final int BASE_PADDING_LEFT = 5, RANGE_PADDING_LEFT = 10, BASE_PADDING_TOP = 15, RANGE_PADDING_TOP = 10;
41.    private static final int DEFAULT_WIDTH = 60, DEFAULT_HEIGHT = 30;
42.
43.    //settings decided by the layout xml
44.    //canvas width and height
45.    private int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT;
46.
47.    //random word space and pading_top
48.    private int base_padding_left = BASE_PADDING_LEFT, range_padding_left = RANGE_PADDING_LEFT,
49.            base_padding_top = BASE_PADDING_TOP, range_padding_top = RANGE_PADDING_TOP;
50.
51.    //number of chars, lines; font size
52.    private int codeLength = DEFAULT_CODE_LENGTH, line_number = DEFAULT_LINE_NUMBER, font_size = DEFAULT_FONT_SIZE;
53.
54.    //variables
55.    private String code;
56.    private int padding_left, padding_top;
57.    private Random random = new Random();
58.
59.    public Bitmap createBitmap() {
60.        padding_left = 0;
61.
62.        Bitmap bp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
63.        Canvas c = new Canvas(bp);
64.
65.        code = createCode();
66.
67.        c.drawColor(Color.WHITE);
68.        Paint paint = new Paint();
69.        paint.setTextSize(font_size);
70.
71.        for (int i = 0; i < code.length(); i++) {
72.            randomTextStyle(paint);
73.            randomPadding();
74.            c.drawText(code.charAt(i) + "", padding_left, padding_top, paint);
75.        }
76.
77.        for (int i = 0; i < line_number; i++) {
78.            drawLine(c, paint);
79.        }
80.
81.        c.save( Canvas.ALL_SAVE_FLAG );//保存
82.        c.restore();//
83.        return bp;
84.    }
85.
86.    public String getCode() {
87.        return code;
88.    }
89.
90.    private String createCode() {
91.        StringBuilder buffer = new StringBuilder();
92.        for (int i = 0; i < codeLength; i++) {
93.            buffer.append(CHARS[random.nextInt(CHARS.length)]);
94.        }
95.        return buffer.toString();
96.    }
97.
98.    private void drawLine(Canvas canvas, Paint paint) {
99.        int color = randomColor();
100.        int startX = random.nextInt(width);
101.        int startY = random.nextInt(height);
102.        int stopX = random.nextInt(width);
103.        int stopY = random.nextInt(height);
104.        paint.setStrokeWidth(1);
105.        paint.setColor(color);
106.        canvas.drawLine(startX, startY, stopX, stopY, paint);
107.    }
108.
109.    private int randomColor() {
110.        return randomColor(1);
111.    }
112.
113.    private int randomColor(int rate) {
114.        int red = random.nextInt(256) / rate;
115.        int green = random.nextInt(256) / rate;
116.        int blue = random.nextInt(256) / rate;
117.        return Color.rgb(red, green, blue);
118.    }
119.
120.    private void randomTextStyle(Paint paint) {
121.        int color = randomColor();
122.        paint.setColor(color);
123.        paint.setFakeBoldText(random.nextBoolean());  //true为粗体,false为非粗体
124.        float skewX = random.nextInt(11) / 10;
125.        skewX = random.nextBoolean() ? skewX : -skewX;
126.        paint.setTextSkewX(skewX); //float类型参数,负数表示右斜,整数左斜
127.//      paint.setUnderlineText(true); //true为下划线,false为非下划线
128.//      paint.setStrikeThruText(true); //true为删除线,false为非删除线
129.    }
130.
131.    private void randomPadding() {
132.        padding_left += base_padding_left + random.nextInt(range_padding_left);
133.        padding_top = base_padding_top + random.nextInt(range_padding_top);
134.    }
135.}  

使用方法

[java] view plaincopy

1.ImageView imageView = (ImageView)findViewById(R.id.imageView);
2.imageView.setImageBitmap(BPUtil.getInstance().createBitmap());  

摘自:http://blog.csdn.net/whumr1/article/details/7092013
时间: 2025-01-11 12:33:34

android生成验证码bitmap的相关文章

android 生成验证码图片

(转自:http://blog.csdn.net/onlyonecoder/article/details/8231373) 1 package com.nobeg.util; 2 3 import java.util.Random; 4 5 import android.graphics.Bitmap; 6 import android.graphics.Canvas; 7 import android.graphics.Color; 8 import android.graphics.Pai

Android生成验证码

先放上这个小Demo的图片:点击图片可以更换新的验证码. 验证码是以图片的形式显示的,所以需要写一个自定义验证码控件 public class ValidateView extends View { /** * 点数 */ private int pointNum = 150;// 背景杂质 /** * 线段数 */ private int lineNum = 3;// 背景杂质 /** * 验证码字长 */ private int validateCodeLenght = 6;// 默认长度为

Android本地验证码的生成

android客户端生成本地验证码主要用来限制用户随意按请求按钮,其实该示例也是来对自定义view的练练手而已,先给出效果图吧 其中可定制: *干扰线数目 *干扰点数目 *背景颜色 *验证码字体大小及字数 相信以上可以满足一般的需要了吧,不够的话可自行添加,下面就来讲实现的步骤了 继承view,重写构造方法,并初始化所需参数 public class ValidationCode extends View { private Paint mTextPaint;//文字画笔 private Pai

Android锁定EditText内容和随机生成验证码

昨天写了个小Demo,实现了随机生成验证码,和锁定EditText两个小功能,先看一下效果图: 锁定EditText在我们不需要用户编辑EditText内容的时候可以用到,实现还是很简单的,一行代码: etLock.setEnabled(false); 随机生成验证码,主要是用了Random函数,以及将View转为Bitmap的逻辑,也没有难点,下面贴一下代码,供需要的朋友参考: Main.java package com.zms.textlock; import android.graphic

C#生成验证码

生成验证码的类: using System; using System.Collections.Generic; using System.Drawing; using System.Text; namespace Controllers.Core.Util { /// <summary> /// 验证码 /// </summary> public class VerifyCodeHelper : AdminBaseController { #region 变量 /// <s

asp.net一般处理程序(.ashx)动态生成验证码案例。

{使用一般处理程序动态生成验证码} 1.新建WebSite项目,添加一般处理程序命名为  yzm.ashx,添加如下代码: public void ProcessRequest(HttpContext context)    {   //将context.Response.ContentType = "text/plain";修改为context.Response.ContentType = "image/JPEG";        context.Response

artdialog 异步加载页面 生成验证码

artdialog  异步加载一个页面 需求:例如现在好多网站的登录或注册 都是点击弹出一个层出来 然后在上面登录.注册 这个登录可能在网站的每个页面都会有,但是我们又不能在每个页面都这一段html加载出来不显示,到需要用的时候,在给shou出来,这样做于情于理都说!不!!过!!!去!!!!!! 恰好以前接触过artdialog  不多说上代码,(注意思维,代码是死的方法是活,解决需求不一定非要这个方法 ) 1.页面html代码 1 <head runat="server">

Android客户端中Bitmap的下载过程和缓存机制

加载流程: if(内存命中){ 从内存中读取 }else{ create AsyncTasks,task中的多个Runnable是通过堆栈先进后出的方式来调度,而非队列式的先进先出,目的是最先加载用户最近划到或打开的图片. } AsyncTask: //do in background——该后台进程在用户scroll列表的时候会暂停,从而减小了列表划动时cpu的overhead,此方法也被ImageLoader和facebook的官方app所使用. if(磁盘缓存命中){ 从缓存中读取 }els

转 一个简单实用的 生成验证码的 代码

1 using System; 2 using System.Collections.Generic; 3 using System.Drawing; 4 using System.Drawing.Drawing2D; 5 using System.Drawing.Imaging; 6 using System.IO; 7 using System.Linq; 8 using System.Web; 9 10 11 public class ValidateCode 12 { 13 public