如何用java生成随机验证码

 1.VerifyCode 类:

   1 package com.HRuinger.enity;

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

 2.ImageTest类

  1 package com.HRuinger.enity;

 2 
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import java.awt.Graphics2D;
 6 import java.awt.image.BufferedImage;
 7 import java.io.FileNotFoundException;
 8 import java.io.FileOutputStream;
 9 import java.io.IOException;
10 
11 import javax.imageio.ImageIO;
12 
13 public class ImageTest {
14 
15   public void fun1() throws FileNotFoundException, IOException{
16         /*
17          * 1. 创建图片缓冲区
18          * 2. 设置其宽高
19          * 3. 得到这个图片的绘制环境(得到画笔)
20          * 4. 保存起来
21          */
22         BufferedImage bi = new BufferedImage(85, 35, BufferedImage.TYPE_INT_RGB);
23         Graphics2D g = (Graphics2D)bi.getGraphics();//得到绘制环境
24         g.setColor(Color.WHITE);//把环境设置为白色
25         g.fillRect(0, 0, 70, 35);//填充矩形!填充矩形,从0,0点开始,宽70,高35,即整个图片,即为图片设置背景色
26         g.setColor(Color.RED);//把环境设置为红色
27         g.drawString("Hello", 2,35-2);//向图片上写入字符串,其中2,2表示x,y轴的坐标
28         
29   }
30     public void fun2() throws FileNotFoundException, IOException{
31             VerifyCode vc = new VerifyCode();//创建VerifyCode类的对象
32             BufferedImage bi = vc.getImage();//调用getImge()方法获得一个BufferedImage对象
33             VerifyCode.output(bi, new FileOutputStream("WebRoot/img/yzm.jpg"));//调用静态方法output()方法将图片保存在文件输出流中
34             System.out.println(vc.getText());//在控制台上打印验证码的文本值
35     }
36 }
原文地址:https://www.cnblogs.com/HRuinger/p/5705878.html