验证码生成器模版

 

 VCodeGenerator.java

  1 package util;
  2 import java.awt.Color;
  3 import java.awt.Font;
  4 import java.awt.Graphics2D;
  5 import java.awt.image.BufferedImage;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.OutputStream;
  9 
 10 import javax.imageio.ImageIO;
 11 public class VCodeGenerator {
 12     private int width=160;
 13     private int height=60;
 14     private int charCnt=4;
 15     private int disturbLineNum = 10;
 16 
 17     final private char[] chars = "2345678ABCDEFGHJKLMPQRSTUVWXYabcdefhkmnqrstuvwx"
 18         .toCharArray();
 19     private static String[] fontNames = new String[] { "Courier", "Arial",
 20         "Verdana", "Georgia", "Times", "Tahoma" };
 21     private static int[] fontStyle = new int[] { Font.PLAIN, Font.BOLD,Font.ITALIC, Font.BOLD | Font.ITALIC };
 22 
 23 
 24     private OutputStream os;
 25     
 26     public VCodeGenerator(OutputStream os) {//一参构造函数
 27         this.os = os;
 28     }
 29     public VCodeGenerator(int width, int height, int charCnt, OutputStream os) {//四参构造函数
 30         super();
 31         this.width = width;
 32         this.height = height;
 33         this.charCnt = charCnt;
 34         this.os = os;
 35     }
 36     
 37     public String drawCode() throws IOException {
 38         BufferedImage bi = new BufferedImage(this.width, this.height,
 39                 BufferedImage.TYPE_INT_RGB);
 40         Graphics2D g = bi.createGraphics();
 41         g.setColor(new Color(245, 245, 245));//背景色
 42         g.fillRect(0, 0, this.width, this.height);
 43 
 44         drawDisturbLine(g);
 45 
 46         BufferedImage[] bis = new BufferedImage[charCnt];
 47         char[] codes = generateCode();
 48         for (int i = 0; i < charCnt; i++) {
 49             bis[i] = generateBuffImg(codes[i]);
 50             g.drawImage(bis[i], null, (int) (this.height * 0.6) * i, 0);
 51         }
 52 
 53         g.dispose();
 54 
 55         ImageIO.write(bi, "gif", os);
 56         return new String(codes);
 57     }
 58     private BufferedImage generateBuffImg(char c) {
 59         String tmp = Character.toString(c);
 60         Color forecolor = getRandomColor();
 61         Color backcolor = new Color(255, 255, 255, 0);
 62         String fontName = getRandomFontName();
 63         int fontStyle = getRandomStyle();
 64         int fontSize = getRandomSize();
 65         int strX = (this.height - fontSize) / 2;
 66         int strY = (this.height - fontSize) / 2 + fontSize;
 67         double arch = getRandomArch();
 68 
 69         BufferedImage ret = new BufferedImage(this.height, this.height,
 70                 BufferedImage.TYPE_INT_ARGB);
 71         Graphics2D g = ret.createGraphics();
 72         g.setColor(backcolor);
 73         g.fillRect(0, 0, this.height, this.height);
 74 
 75         g.setColor(forecolor);
 76         g.setFont(new Font(fontName, fontStyle, fontSize));
 77         g.rotate(arch, this.height / 2, this.height / 2);
 78         g.drawString(tmp, strX, strY);
 79 
 80         g.dispose();
 81         return ret;
 82     }
 83 
 84     private double getRandomArch() {
 85         return ((int) (Math.random() * 1000) % 2 == 0 ? -1 : 1) * Math.random();
 86     }
 87 
 88     private Color getRandomColor() {
 89         int r = (int) (Math.random() * 10000) % 200;
 90         int g = (int) (Math.random() * 10000) % 200;
 91         int b = (int) (Math.random() * 10000) % 200;
 92         return new Color(r, g, b);
 93     }
 94 
 95     private String getRandomFontName() {
 96         int pos = (int) (Math.random() * 10000) % (fontNames.length);
 97         return fontNames[pos];
 98     }
 99 
100     private int getRandomStyle() {
101         int pos = (int) (Math.random() * 10000) % (fontStyle.length);
102         return fontStyle[pos];
103     }
104 
105     private int getRandomSize() {
106         int max = (int) (this.height * 0.9);
107         int min = (int) (this.height * 0.6);
108         return (int) (Math.random() * 10000) % (max - min + 1) + min;
109     }
110 
111     private char[] generateCode() {
112         char[] ret = new char[charCnt];
113         for (int i = 0; i < charCnt; i++) {
114             int letterPos = (int) (Math.random() * 10000) % (chars.length);
115             ret[i] = chars[letterPos];
116         }
117         return ret;
118     }
119     
120     private void drawDisturbLine(Graphics2D graphics) {
121         for (int i = 0; i < disturbLineNum; i++) {
122             graphics.setColor(getRandomColor());
123             int x = (int) (Math.random() * 10000) % (this.width + 1) + 1;
124             int x1 = (int) (Math.random() * 10000) % (this.width + 1) + 1;
125             int y = (int) (Math.random() * 10000) % (this.height + 1) + 1;
126             int y1 = (int) (Math.random() * 10000) % (this.height + 1) + 1;
127             graphics.drawLine(x, y, x1, y1);
128         }
129 
130     }
131 
132     public static void main(String[] args) throws IOException {
133         FileOutputStream fos = new FileOutputStream("d:/tmp.gif");
134         VCodeGenerator vg = new VCodeGenerator(80,30,4,fos);
135         String a=vg.drawCode();
136         System.out.print("\n"+a);
137 /* 新建一个servlet,代码如下:(验证码直接调用该servlet)
138  * OutputStream os=response.getOutputStream();
139         VCodeGenerator vg=new VCodeGenerator(os);
140         String vcode=vg.drawCode();
141         os.close();
142         request.getSession().setAttribute("vcode", vcode);
143         System.out.print("\n"+vcode);
144   *jsp中获取:
145           <tr> <td height="60">
146             <img src="servlet/getVcode?ts="+new Date().getTime() id="imgVcode"/>
147           </td> </tr>
148         <tr>
149           <td align="center" valign="middle" height="20" style="color:blue">若看不清,点图片换一张</td>
150         </tr>
151    *js中控制    
152     //绑定验证码图片框单击事件实现更换验证码
153     $("div.validateCodeDiv").click(function(event){
154         changeCode();
155         window.event.cancelBubble = true;//防止冒泡事件
156     });
157         function changeCode(){
158     $("#imgVcode").attr("src","servlet/getVcode?ts="+new Date().getTime() );    
159 }
160 */
161     }
162 }

 

posted @ 2012-11-14 10:41  墙头一颗草  Views(441)  Comments(0Edit  收藏  举报