Java生成验证码小工具

无意中看到一个生成简易验证码的小工具类(保存学习):

工具类代码:

  1 import java.awt.BasicStroke;
  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.IOException;
  7 import java.io.OutputStream;
  8 import java.util.Random;
  9 
 10 import javax.imageio.ImageIO;
 11 
 12 public class VerifyCode {
 13     private int w = 70;
 14     private int h = 35;
 15      private Random r = new Random();
 16      // {"宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑", "楷体_GB2312"}
 17     private String[] fontNames  = {"宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑", "楷体_GB2312"};
 18     // 可选字符
 19     private String codes  = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
 20     // 背景色
 21     private Color bgColor  = new Color(255, 255, 255);
 22     // 验证码上的文本
 23     private String text ;
 24     
 25     // 生成随机的颜色
 26     private Color randomColor () {
 27         int red = r.nextInt(150);
 28         int green = r.nextInt(150);
 29         int blue = r.nextInt(150);
 30         return new Color(red, green, blue);
 31     }
 32     
 33     // 生成随机的字体
 34     private Font randomFont () {
 35         int index = r.nextInt(fontNames.length);
 36         String fontName = fontNames[index];//生成随机的字体名称
 37         int style = r.nextInt(4);//生成随机的样式, 0(无样式), 1(粗体), 2(斜体), 3(粗体+斜体)
 38         int size = r.nextInt(5) + 24; //生成随机字号, 24 ~ 28
 39         return new Font(fontName, style, size);
 40     }
 41     
 42     // 画干扰线
 43     private void drawLine (BufferedImage image) {
 44         int num  = 5;//一共画5条
 45         Graphics2D g2 = (Graphics2D)image.getGraphics();
 46         for(int i = 0; i < num; i++) {//生成两个点的坐标,即4个值
 47             int x1 = r.nextInt(w);
 48             int y1 = r.nextInt(h);
 49             int x2 = r.nextInt(w);
 50             int y2 = r.nextInt(h); 
 51             g2.setStroke(new BasicStroke(1.5F)); 
 52             g2.setColor(randomColor()); //随机生成干扰线颜色
 53             g2.drawLine(x1, y1, x2, y2);//画线
 54         }
 55     }
 56     
 57     // 随机生成一个字符
 58     private char randomChar () {
 59         int index = r.nextInt(codes.length());
 60         return codes.charAt(index);
 61     }
 62     
 63     // 创建BufferedImage
 64     private BufferedImage createImage () {
 65         BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 
 66         Graphics2D g2 = (Graphics2D)image.getGraphics(); 
 67         g2.setColor(this.bgColor);
 68         g2.fillRect(0, 0, w, h);
 69          return image;
 70     }
 71     
 72     // 调用这个方法得到验证码
 73     public BufferedImage getImage () {
 74         BufferedImage image = createImage();//创建图片缓冲区 
 75         Graphics2D g2 = (Graphics2D)image.getGraphics();//得到绘制环境
 76         StringBuilder sb = new StringBuilder();//用来装载生成的验证码文本
 77         // 向图片中画4个字符
 78         for(int i = 0; i < 4; i++)  {//循环四次,每次生成一个字符
 79             String s = randomChar() + "";//随机生成一个字母 
 80             sb.append(s); //把字母添加到sb中
 81             float x = i * 1.0F * w / 4; //设置当前字符的x轴坐标
 82             g2.setFont(randomFont()); //设置随机字体
 83             g2.setColor(randomColor()); //设置随机颜色
 84             g2.drawString(s, x, h-5); //画图
 85         }
 86         this.text = sb.toString(); //把生成的字符串赋给了this.text
 87         drawLine(image); //添加干扰线
 88         return image;        
 89     }
 90     
 91     // 返回验证码图片上的文本
 92     public String getText () {
 93         return text;
 94     }
 95     
 96     // 保存图片到指定的输出流
 97     public static void output (BufferedImage image, OutputStream out) 
 98                 throws IOException {
 99         ImageIO.write(image, "JPEG", out);
100     }
101 }
View Code

测试代码:

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import org.junit.Test;
public class Demo2 {
    @Test
    public void fun1() throws Exception {
        VerifyCode verifyCode = new VerifyCode();
        BufferedImage bi = verifyCode.getImage();
        System.out.println(verifyCode.getText());
        VerifyCode.output(bi, new FileOutputStream("F:/b.jpg"));
    }
}
View Code

运行结果:

posted @ 2016-10-22 16:05  凌晨。。。三点  阅读(1531)  评论(0编辑  收藏  举报