JavaWeb基础—VerifyCode源码

时间:2022-11-16 15:19:41
 1 package com.jiangbei.verifycodeutils;  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 
 16 private int w = 70;  17 
 18 private int h = 35;  19 
 20 private Random r = new Random();  21 
 22 // {"宋体", "华文楷体", "黑体", "华文新魏", "华文隶书", "微软雅黑", "楷体_GB2312"}
 23 
 24 private String[] fontNames = {"宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312"};  25 
 26 // 可选字符
 27 
 28 private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";  29 
 30 // 背景色
 31 
 32 private Color bgColor = new Color(255, 255, 255);  33 
 34 // 验证码上的文本
 35 
 36 private String text ;  37 
 38 // 生成随机的颜色
 39 
 40 private Color randomColor () {  41 
 42 int red = r.nextInt(150);  43 
 44 int green = r.nextInt(150);  45 
 46 int blue = r.nextInt(150);  47 
 48 return new Color(red, green, blue);  49 
 50 }  51 
 52 // 生成随机的字体
 53 
 54 private Font randomFont () {  55 
 56 int index = r.nextInt(fontNames.length);  57 
 58 String fontName = fontNames[index];//生成随机的字体名称
 59 
 60 int style = r.nextInt(4);//生成随机的样式, 0(无样式), 1(粗体), 2(斜体), 3(粗体+斜体)
 61 
 62 int size = r.nextInt(5) + 24; //生成随机字号, 24 ~ 28
 63 
 64 return new Font(fontName, style, size);  65 
 66 }  67 
 68 // 画干扰线
 69 
 70 private void drawLine (BufferedImage image) {  71 
 72 int num = 3;//一共画3条
 73 
 74 Graphics2D g2 = (Graphics2D)image.getGraphics();  75 
 76 for(int i = 0; i < num; i++) {//生成两个点的坐标,即4个值
 77 
 78 int x1 = r.nextInt(w);  79 
 80 int y1 = r.nextInt(h);  81 
 82 int x2 = r.nextInt(w);  83 
 84 int y2 = r.nextInt(h);  85 
 86 g2.setStroke(new BasicStroke(1.5F));  87 
 88 g2.setColor(Color.BLUE); //干扰线是蓝色
 89 
 90 g2.drawLine(x1, y1, x2, y2);//画线
 91 
 92 }  93 
 94 }  95 
 96 // 随机生成一个字符
 97 
 98 private char randomChar () {  99 
100 int index = r.nextInt(codes.length()); 101 
102 return codes.charAt(index); 103 
104 } 105 
106 // 创建BufferedImage
107 
108 private BufferedImage createImage () { 109 
110 BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); 111 
112 Graphics2D g2 = (Graphics2D)image.getGraphics(); 113 
114 g2.setColor(this.bgColor); 115 
116 g2.fillRect(0, 0, w, h); 117 
118 return image; 119 
120 } 121 
122 // 调用这个方法得到验证码
123 
124 public BufferedImage getImage () { 125 
126 BufferedImage image = createImage();//创建图片缓冲区
127 
128 Graphics2D g2 = (Graphics2D)image.getGraphics();//得到绘制环境
129 
130 StringBuilder sb = new StringBuilder();//用来装载生成的验证码文本 131 
132 // 向图片中画4个字符
133 
134 for(int i = 0; i < 4; i++) {//循环四次,每次生成一个字符
135 
136 String s = randomChar() + "";//随机生成一个字母
137 
138 sb.append(s); //把字母添加到sb中
139 
140 float x = i * 1.0F * w / 4; //设置当前字符的x轴坐标
141 
142 g2.setFont(randomFont()); //设置随机字体
143 
144 g2.setColor(randomColor()); //设置随机颜色
145 
146 g2.drawString(s, x, h-5); //画图
147 
148 } 149 
150 this.text = sb.toString(); //把生成的字符串赋给了this.text
151 
152 drawLine(image); //添加干扰线
153 
154 return image; 155 
156 } 157 
158 // 返回验证码图片上的文本
159 
160 public String getText () { 161 
162 return text; 163 
164 } 165 
166 // 保存图片到指定的输出流
167 
168 public static void output (BufferedImage image, OutputStream out) 169 
170 throws IOException { 171 
172 ImageIO.write(image, "JPEG", out); 173 
174 } 175 
176 }

 

简单运用:

  

 1 package com.day11.servlet;  2 
 3 import java.awt.image.BufferedImage;  4 import java.io.IOException;  5 
 6 import javax.servlet.ServletException;  7 import javax.servlet.http.HttpServlet;  8 import javax.servlet.http.HttpServletRequest;  9 import javax.servlet.http.HttpServletResponse; 10 
11 import com.jiangbei.verifycodeutils.VerifyCode; 12 /**
13  * 用于验证码案例 14  * @author jiangbei01 15  * 16  */
17 public class VerifyCodeServlet extends HttpServlet { 18 
19 
20 
21     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 22         /**
23  * 生成图片 24  * 保存图片上验证码文本到session域中 25  * 将图片响应给客户端 26          */
27         VerifyCode vc = new VerifyCode(); 28         BufferedImage image = vc.getImage(); 29         request.getSession().setAttribute("vcode", vc.getText()); 30         
31  VerifyCode.output(image, response.getOutputStream()); 32         
33     }