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 }