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

调用:

    /**
     * 生成验证码
     */
    private void createConfirmCode(){
        mImageView.setImageBitmap(ConfirmCode.getInstance()
                .createBitmap());
        String Code = ConfirmCode.getInstance().getCode();

    }
时间: 2024-10-09 10:22:59

android 生成验证码图片的相关文章

javaweb学习总结(九)—— 通过Servlet生成验证码图片

一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 1 package gacl.response.study; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.awt.Graphics; 5 import java.awt.Graphics2D; 6 import java.awt.image.Buff

用python生成验证码图片

除了配置好的python环境外,还需要配有python中的PIL库,这是python中专门用来处理图片的库.用传统的pip install 方法或者下载源码 python setup.py install 方法安装该库,很可能会报错(视运行环境不同).可以采用以下方法: 1.下载安装包URL:http://www.pythonware.com/products/pil/index.htm,要下载支持全平台的. 2.解压缩: tar –zxv –f Imaging-1.1.7.tar.gz 3.进

Python Show-Me-the-Code 第 0010 题 生成验证码图片

第 0010 题:使用 Python 生成类似于下图中的字母验证码图片 阅读资料 思路:先随机生成验证码,然后用Python的PIL库画出这个激活码的图片,具体点就是创建画布,加验证码的字上去,增加噪点进行干扰,再进行模糊处理,接着保存到名字为验证码的图片中. 0010.生成验证码图片.py #!/usr/bin/env python #coding: utf-8 import Image, ImageDraw, ImageFont, ImageFilter import string, ran

用动态网页技术PHP生成验证码图片的源代码

文件a.php <? //checkNum.php session_start(); function random($len) { $srcstr="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; mt_srand(); $strs=""; for($i=0;$i<$len;$i++){ $strs.=$srcstr[mt_rand(0,35)]; } return strtoupper($strs); } $str=ra

转: 通过Servlet生成验证码图片

孤傲苍狼 只为成功找方法,不为失败找借口! javaweb学习总结(九)—— 通过Servlet生成验证码图片 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 1 package gacl.response.study; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.awt.Graphics; 5

java生成验证码图片

public class AuthImg extends HttpServlet { /** * */ private static final long serialVersionUID = 4975974534946437434L; // 设置图形验证码字符串的字体和大小 private Font mFont = new Font("微软雅黑", Font.ITALIC, 18); private Random random = new Random(); public void

JavaWeb---总结(九)通过Servlet生成验证码图片

一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片  1 package gacl.response.study;  2 import java.awt.Color;  3 import java.awt.Font;  4 import java.awt.Graphics;  5 import java.awt.Graphics2D;  6 import java.awt.imag

通过Servlet生成验证码图片

首先声明:本文转载自:@孤傲苍狼 https://home.cnblogs.com/u/xdp-gacl/ 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 1 package gacl.response.study; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.awt.Graphics; 5 im

通过Servlet生成验证码图片(转)

原文地址:http://www.cnblogs.com/xdp-gacl/p/3798190.html 一.BufferedImage类介绍 生成验证码图片主要用到了一个BufferedImage类,如下: 创建一个DrawImage Servlet,用来生成验证码图片 1 package gacl.response.study; 2 import java.awt.Color; 3 import java.awt.Font; 4 import java.awt.Graphics; 5 impo