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

[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

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

  1. android 生成验证码图片

    (转自:http://blog.csdn.net/onlyonecoder/article/details/8231373) package com.nobeg.util; import java.u ...

  2. Android功能模块化之生成验证码Bitmap

    Android生成验证码Bitmap,主要使用Canvas绘制,实现的步骤如下: 1.生成验证码.主要采用java的随机函数生成序号,然后对应获取预设的Char数组的值,生成长度固定的验证码: 2.C ...

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

    昨天写了个小Demo,实现了随机生成验证码,和锁定EditText两个小功能,先看一下效果图: 锁定EditText在我们不须要用户编辑EditText内容的时候能够用到,实现还是非常easy的,一行 ...

  4. 【转】DelphiXE10.2.3——跨平台生成验证码图片

    原文地址 Java.PHP.C#等很容易在网上找到生成验证码图片的代码,Delphi却寥寥无几,昨天花了一整天时间,做了个跨平台的验证码,可以用在C/S和B/S端,支持Windows.Linux.An ...

  5. 动态生成验证码———MVC版

    上面有篇博客也是写的验证码,但那个是适用于asp.net网站的. 今天想在MVC中实现验证码功能,弄了好久,最后还是看博友文章解决的,感谢那位博友. 首先引入生成验证码帮助类. ValidateCod ...

  6. 浅谈Android下的Bitmap之大Bitmap加载

    引言 我们常常提到的“Android程序优化”,通常指的是性能和内存的优化,即:更快的响应速度,更低的内存占用.Android程序的性能和内存问题,大部分都和图片紧密相关,而图片的加载在很多情况下很用 ...

  7. ASP.NET ashx实现无刷新页面生成验证码

    现在大部分网站登陆时都会要求输入验证码,在网上也看了一些范例,现在总结一下如何实现无刷新页面生成验证码. 效果图: 实现方式: 前台: <div> <span>Identify ...

  8. C#生成验证码

    生成验证码的类: using System; using System.Collections.Generic; using System.Drawing; using System.Text; na ...

  9. Asp.net mvc生成验证码

    1.生成验证码类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

随机推荐

  1. 用MFC如何高效地绘图

    显示图形如何避免闪烁,如何提高显示效率是问得比较多的问题.而且多数人认为MFC的绘图函数效率很低,总是想寻求其它的解决方案.     MFC的绘图效率的确不高但也不差,而且它的绘图函数使用非常简单,只 ...

  2. 构建微服务:Spring boot

    构建微服务:Spring boot 在上篇文章构建微服务:Spring boot 提高篇中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jp ...

  3. iOS:使用导航栏

    要求使用ARC // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. ...

  4. Java基础——异常机制

    [捕获异常] 硬件的错误.输入错误.物理限制等问题,都可能导致程序运行时的异常出现. 1.异常的分类层次 在java中,异常对象都是由Throwable类继承而来的,主要分为两大类: Error和Ex ...

  5. Hadoop 学习笔记 (九) hadoop2.2.0 生产环境部署 HDFS HA部署方法

    step1:将安装包hadoop-2.2.0.tar.gz存放到某一个目录下,并解压 step2:修改解压后的目录中的文件夹/etc/hadoop下的xml配置文件(如果文件不存在,则自己创建) 包括 ...

  6. BZOJ 2226 LCMSum

    Description Given \(n\), calculate the sum \(LCM(1,n) + LCM(2,n) + \cdots + LCM(n,n)\), where \(LCM( ...

  7. HTML Imports

    为什么需要导入? 先想想你在 web 上是如何加载不同类型的资源.对于 JS,我们有 <script src>.<link rel="stylesheet"> ...

  8. Delphi与Java中的日期互换

    在最近做的一个项目中用到了Java和Delphi,发现它们不能正确读取对方的日期类型,如在Java中写入一个值为“2007-12-1”的日期值,通过Delphi读取却不是这个值了.通过查阅资料,发现两 ...

  9. SQL*Net more data to client

    The server process is sending more data/messages to the client. The previous operation to the client ...

  10. -_-#【HTML】同一个标签页打开

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...