首页 > 代码库 > java web 验证码生成

java web 验证码生成

先看看效果图:

一般需求应该够用了,有线条干扰线,斑点,字体小幅度的旋转。。

还没有与session挂钩上,所以只能用于简单的测试用例。

直接上代码,看代码注释。。。

  1 package com.act262.demo;  2   3 import java.awt.Color;  4 import java.awt.Font;  5 import java.awt.Graphics;  6 import java.awt.Graphics2D;  7 import java.awt.image.BufferedImage;  8 import java.io.IOException;  9 import java.io.OutputStream; 10 import java.util.Random; 11  12 import javax.imageio.ImageIO; 13 import javax.servlet.ServletException; 14 import javax.servlet.annotation.WebServlet; 15 import javax.servlet.http.HttpServlet; 16 import javax.servlet.http.HttpServletRequest; 17 import javax.servlet.http.HttpServletResponse; 18  19 import sun.java2d.loops.DrawLine; 20  21 /** 22  * 输出随机的验证码 23  */ 24 @WebServlet({ "/CheckCode", "/checkCode.do" }) 25 public class CheckCodeServlet extends HttpServlet { 26     private static final long serialVersionUID = 1L; 27  28     /* 宽度 */ 29     private final int WIDTH = 100; 30     /* 高度 */ 31     private final int HEIGHT = 20; 32     /* 生成验证码的个数 */ 33     private final int COUNT = 4; 34     /* 干扰线条数 */ 35     private final int LINE_ROW = 6; 36  37     /* 输出的基本码表,如果使用中文,则使用utf-8的码表,类似 \ue234 ,而且应该使用常用字,避免出现偏僻字 */ 38     private final char[] BASECODE = { ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, 39             ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, 40             ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘, ‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, 41             ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, 42             ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, 43             ‘8‘, ‘9‘ }; 44     /* 随机数发生器 */ 45     private Random random; 46     private BufferedImage image; 47  48     // 写出数据 49     private void write(OutputStream output) throws IOException { 50         random = new Random(); 51  52         image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); 53         Graphics graphics = image.getGraphics(); 54  55         setBackground(graphics); 56         drawBorder(graphics); 57         drawDot(graphics); 58         drawLine(graphics); 59         drawString(graphics); 60  61         ImageIO.write(image, "jpg", output); 62     } 63  64     //写字 65     private void drawString(Graphics graphics) { 66         StringBuffer sb = new StringBuffer(); 67         Font font = new Font("宋体", Font.BOLD, 18); 68         graphics.setFont(font); 69         graphics.setColor(Color.BLACK); 70  71         for (int i = 0; i < COUNT; i++) { 72             String ch = String 73                     .valueOf(BASECODE[random.nextInt(BASECODE.length)]); 74             sb.append(ch); 75  76             // 设置位置 77             int x = i * 20 + random.nextInt(12) + 10; 78             int y = random.nextInt(HEIGHT / 3) + 12; 79  80             // 旋转字体 81             double theta = Math.PI / 180 * random.nextInt(20); 82             // rotate(graphics, theta); 83  84             graphics.drawString(ch, x, y); 85  86             // 恢复。。 87             // rotate(graphics, -theta); 88         } 89  90         System.out.println("验证码:" + sb.toString()); 91     } 92  93     //旋转 94     private void rotate(Graphics graphics, double theta) { 95         ((Graphics2D) graphics).rotate(theta); 96     } 97  98     // 画随机线条 99     private void drawLine(Graphics graphics) {100         for (int i = 0; i < LINE_ROW; i++) {101             int x1 = random.nextInt(WIDTH);102             int y1 = random.nextInt(HEIGHT);103             int x2 = random.nextInt(WIDTH);104             int y2 = random.nextInt(HEIGHT);105             setRandomColor(graphics);106             graphics.drawLine(x1, y1, x2, y2);107         }108     }109 110     // 画斑点111     private void drawDot(Graphics graphics) {112         graphics.setColor(Color.red);113         for (int i = 0; i < WIDTH; i++) {114             int x = i;115             int y = random.nextInt(HEIGHT);116             int r = random.nextInt(2);117             // graphics.fillOval(x, y, r, r);118             graphics.drawOval(x, y, r, r);119         }120     }121 122     // 画边框123     private void drawBorder(Graphics graphics) {124         graphics.setColor(Color.BLACK);125         graphics.drawRect(1, 1, WIDTH - 2, HEIGHT - 2);126     }127 128     // 设置背景129     private void setBackground(Graphics graphics) {130         graphics.setColor(Color.WHITE);131         graphics.fillRect(0, 0, WIDTH, HEIGHT);// 填充背景色132     }133 134     // 设置随机的画笔颜色135     private void setRandomColor(Graphics g) {136         g.setColor(new Color(random.nextInt(255), random.nextInt(255), random137                 .nextInt(255)));138     }139 140     protected void doGet(HttpServletRequest request,141             HttpServletResponse response) throws ServletException, IOException {142 143         response.setContentType("image/jpeg");144         response.setHeader("Expires", "-1");145         response.setHeader("Cache-Control", "no-cache");146 147         // 写出数据148         write(response.getOutputStream());149 150     }151 152     protected void doPost(HttpServletRequest request,153             HttpServletResponse response) throws ServletException, IOException {154         // TODO Auto-generated method stub155     }156 157 }

 

 

不足的是,这个验证码数量与字体大小还有图片的大小之间的关系比较难搞,我是一点一点计算调试出来的,有些时候图片上的字体会跑位,这就是画图的难点了。如果修改了数量,字体大小,这些位置关系得重新计算才行。

java web 验证码生成