创建一个名为VerifyCode的类:
1 package com.sdbi.liuyouqi; 2 3 import javax.imageio.ImageIO; 4 import java.awt.Color; 5 import java.awt.Font; 6 import java.awt.Graphics2D; 7 import java.awt.*; 8 import java.awt.image.BufferedImage; 9 import java.io.IOException; 10 import java.io.OutputStream; 11 import java.util.Random; 12 13 public class VerifyCode { 14 private int width = 150; //宽度 15 private int height = 70; //高度 16 private Random r = new Random(); //获得一个随机数 17 private String[] fontNames = {"宋体", "黑体", "微软雅黑", "华文楷体", "楷体_GB2312"}; //定义所用的字体 18 private Color bgcolor = new Color(250, 250, 250); //生成验证码的背景色 19 private String Codes = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"; //定义验证码的随记字符 20 private String text; 21 22 //生成随机的颜色 23 private Color randomColor() { 24 int red = r.nextInt(150); 25 int green = r.nextInt(150); 26 int blue = r.nextInt(150); 27 return new Color(red, green, blue); 28 } 29 30 //生成随机字体 31 private Font randomFont() { 32 int index = r.nextInt(fontNames.length); 33 String fontName = fontNames[index]; //字体名称 34 int style = r.nextInt(4); //字体样式 35 int size = r.nextInt(5) + 50; //字体大小(50~54之间) 36 return new Font(fontName, style, size); 37 } 38 39 //画干扰线 40 private void drawLine(BufferedImage image) { 41 int num = 3; //干扰线数量 42 Graphics2D g2 = (Graphics2D) image.getGraphics(); //得到图片画笔 43 for (int i = 0; i < num; i++) { 44 int x1 = r.nextInt(width); //起点x坐标 45 int y1 = r.nextInt(height); //起点y坐标 46 int x2 = r.nextInt(width); //终点x坐标 47 int y2 = r.nextInt(height); //终点y坐标 48 g2.setStroke(new BasicStroke(1.5F)); //设置线条特征,1.5F为线的宽度 49 g2.setColor(Color.BLUE); //干扰线颜色 50 g2.drawLine(x1, y1, x2, y2); //画线 51 } 52 } 53 54 //得到codes的长度内的随机数,并使用charAt取得随机数位置上的codes中的字符 55 private char randomChar() { 56 int index = r.nextInt(Codes.length()); 57 return Codes.charAt(index); 58 } 59 60 //创建一张验证码的图片 61 public BufferedImage createImage() { 62 //得到一个图片缓冲区 63 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 64 Graphics2D g2 = (Graphics2D) image.getGraphics(); //得到图片画笔 65 g2.setColor(bgcolor); //给画笔设置颜色 66 g2.fillRect(0, 0, width, height); //使用画笔填充指定的矩形 67 g2.setColor(Color.RED); //给画笔设置颜色 68 g2.drawRect(0, 0, width - 1, height - 1); //使用画笔绘制指定的矩形 69 StringBuilder sb = new StringBuilder(); 70 //向图中画四个字符 71 for (int i = 0; i < 4; i++) { 72 String s = randomChar() + ""; 73 sb.append(s); 74 int x = i * width / 4; //计算字符x坐标位置 75 g2.setFont(randomFont()); //设置画笔字体 76 g2.setColor(randomColor()); //设置画笔颜色 77 g2.drawString(s, x, height - 20); //在图片上写字符 78 } 79 text = sb.toString(); 80 drawLine(image); //绘制干扰线 81 return image; 82 } 83 84 public void output(BufferedImage image, OutputStream output) throws IOException { 85 System.out.println("output"); 86 ImageIO.write(image, "jpeg", output); 87 } 88 89 public String getText() { 90 System.out.println("getText"); 91 return text; 92 } 93 }
创建一个名为VerifyCodeTest的测试类:
1 package com.sdbi.liuyouqi; 2 3 import java.awt.image.BufferedImage; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 7 public class VerifyCodeTest { 8 public static void main(String[] args) throws IOException { 9 VerifyCode code = new VerifyCode(); 10 BufferedImage image = code.createImage(); 11 code.output(image, new FileOutputStream("D:\\output\\image.jpg")); 12 System.out.println("验证码的真实文本是:"+code.getText()); //输出验证码文本 13 } 14 }
运行一次随机生成代码,如下所示:
控制台显示结果:
随记验证码图片:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· 单线程的Redis速度为什么快?
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码