当先锋百科网

首页 1 2 3 4 5 6 7

VerifyCode.java:

  1 package com.xjs.image;
  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 = 70;
 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) + 24; //生成随机字号, 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, "JPEG", out);
102     }
103 }

测试:

 1 import java.awt.image.BufferedImage;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 import org.junit.Test;
 7 
 8 public class Demo {
 9     @Test
10     public void fun1() throws FileNotFoundException, IOException{
11         VerifyCode vc=new VerifyCode();
12         BufferedImage bi=vc.getImage();//得到验证码图片,是一个BufferedImage类型的
13         VerifyCode.output(bi, new FileOutputStream("F://VerifyCode.jpg"));
14         
15         System.out.println(vc.getText());
16     }
17 }

运行后会在指定文件路径下生成一张图片,以及在控制台打印图片上的文本。

 

转载于:https://www.cnblogs.com/xjs1874704478/p/10849784.html