利用JavaSE动态随机生成验证码图片
这部分内容属于JavaSE部分的知识,一些基础知识,并不难,就当回顾一下基础知识。此外,在jsp中写登录功能添加验证码时可以当作一个工具类直接用上。所有代码与注释如下:
VerifyCode.java
package cn.itcast.image;//包名自己改
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
/**
* 利用JavaSE动态生成验证码图片工具类
* @author WenHuagang
*
*/
public class VerifyCode {
private int width = 70;//图片宽
private int height = 35;//图片高
private Random random = new Random();//创建一个随机数生成器
private String[] fontNames = {"宋体","华文楷体","黑体","微软雅黑"};//字体数组
private String codes = "23456789abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXYZ";//可取的字符
private Color bgColor = new Color(255,255,255);//图片背景颜色为白色
private String text;//存放随机生成四个字符
//随机生成一个颜色
private Color getRandomColor() {
int red = random.nextInt(150);//随机生成一个小于150的数,防止字体颜色太清淡
int green = random.nextInt(150);
int blue = random.nextInt(150);
return new Color(red, green, blue);//RGB创建颜色对象
}
//随机生成字体样式
private Font getRandomFont() {
int index = random.nextInt(fontNames.length);//随机生成字体数组的下标
String fontName = fontNames[index];
int fontStyle = random.nextInt(4);//生成随机的样式, 0(无样式), 1(粗体), 2(斜体), 3(粗体+斜体)
int fontSize = random.nextInt(5) +24;//随机生成字号 24~28
return new Font(fontName, fontStyle, fontSize);
}
//画干扰线
private void drawLine(BufferedImage image) {
int num =3;//画3条
Graphics2D g2 = image.createGraphics();//得到绘制环境
for(int i = 0; i < num; i++) {
int x1 = random.nextInt(width);//随机生成两个点的x,y坐标
int y1 = random.nextInt(height);
int x2 = random.nextInt(width);
int y2 = random.nextInt(height);
g2.setStroke(new BasicStroke(1.5F));//设置画笔的粗细
g2.setColor(Color.BLUE);//干扰线为蓝色
g2.drawLine(x1, y1, x2, y2);//开始画线(两点一线)
}
}
//随机生成一个字符
private char getRandomChar() {
int index = random.nextInt(codes.length());
return codes.charAt(index);//根据随机数下标取字符串中相应的字符
}
//创建图片缓冲区
private BufferedImage createBufferedImage() {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, width, height);//填充矩形,填满,也就是设置图片背景
return image;
}
//外部调用,来获取一张随机生成的验证码图片
public BufferedImage getBufferedImage() {
BufferedImage image = createBufferedImage();
Graphics2D g2 = image.createGraphics();
StringBuilder sb = new StringBuilder();//用来装载生成的验证码文本
//一共生成4个字符
for(int i = 0; i < 4; i++) {
String s = getRandomChar() + "";
sb.append(s);
float x = i * 1.0F * width / 4;//获取每一个字符的x轴坐标
g2.setFont(getRandomFont());//设置随机生成的字体样式
g2.setColor(getRandomColor());//设置字体颜色
g2.drawString(s, x, height-5);//图片上写入字符串,设置每一个字符的坐标
}
this.text = sb.toString();//将封装的验证码文本赋给成员变量
drawLine(image);//为图片添加干扰线
return image;
}
//外部调用,来获取验证码文本
public String getText() {
return text;
}
//外部调用来保存图片
public static void getOutputStream(BufferedImage image, OutputStream out) throws IOException {
ImageIO.write(image, "JPEG", out);
}
}
测试类TestDemo.java
package cn.itcast.image;//包名自己改
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.junit.Test;
public class TestDemo {
@Test
public void test() throws FileNotFoundException, IOException {
VerifyCode vc = new VerifyCode();
BufferedImage image = vc.getBufferedImage();
System.out.println("随机生成的验证码为:" + vc.getText());//验证码文本输出到控制台
VerifyCode.getOutputStream(image, new FileOutputStream("E:/result.jpg"));//自己要创建一个字符输出流,路径为图片的保存位置
}
}
控制台结果:
生成的图片:
来源:CSDN
作者:AKA 404 Not Found
链接:https://blog.csdn.net/qq_45693287/article/details/104225860