您的位置:首页 > 编程语言 > Java开发

如何用java生成随机验证码

2016-07-26 00:07 471 查看
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)
throws IOException {
ImageIO.write(image, "jpg", out);
}
}

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;

import javax.imageio.ImageIO;

public class ImageTest {

public void fun1() throws FileNotFoundException, IOException{
/*
* 1. 创建图片缓冲区
* 2. 设置其宽高
* 3. 得到这个图片的绘制环境(得到画笔)
* 4. 保存起来
*/
BufferedImage bi = new BufferedImage(85, 35, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D)bi.getGraphics();//得到绘制环境
g.setColor(Color.WHITE);//把环境设置为白色
g.fillRect(0, 0, 70, 35);//填充矩形!填充矩形,从0,0点开始,宽70,高35,即整个图片,即为图片设置背景色
g.setColor(Color.RED);//把环境设置为红色
g.drawString("Hello", 2,35-2);//向图片上写入字符串,其中2,2表示x,y轴的坐标

}
public void fun2() throws FileNotFoundException, IOException{
VerifyCode vc = new VerifyCode();//创建VerifyCode类的对象
BufferedImage bi = vc.getImage();//调用getImge()方法获得一个BufferedImage对象
VerifyCode.output(bi, new FileOutputStream("WebRoot/img/yzm.jpg"));//调用静态方法output()方法将图片保存在文件输出流中
System.out.println(vc.getText());//在控制台上打印验证码的文本值
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: