java
// 创建图片对象
BufferedImage bi = new BufferedImage(100, 30, BufferedImage.TYPE_INT_RGB);
// 画图(画笔)
Graphics g = bi.getGraphics();
// 形状颜色
g.setColor(Color.PINK);
// 图片形状(长方体)
g.fillRect(0,0,100,30);
// 画笔颜色
g.setColor(Color.WHITE);
// 随机组合(数字&字母)
int i1 = (int)(Math.random()*20);
// 给图片填字
g.drawString(i1+"",20,15);
// 随机组合(数字&字母)
int i2 = (int)(Math.random()*20);
// 给图片填字
g.drawString(i2+"",40,20);
// 随机组合(数字&字母)
int i3 = (int)(Math.random()*20);
// 给图片填字
g.drawString(i3+"",60,15);
// 随机组合(数字&字母)
int i4 = (int)(Math.random()*20);
// 给图片填字
g.drawString(i4+"",80,20);
// 线条颜色
g.setColor(Color.YELLOW);
// 画线
g.drawLine(0,0,100,30);
ImageIO.write(bi,"jpg",resp.getOutputStream());
html
<body>
<input id="captcha" placeholder="验证码" type="text"/>
<img src=""/>
<button>注册</button>
<script charset="utf-8" src="jquery-3.6.0.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("img").attr("src", "../captcha?date=" + new Date());
});
$("img").on("click", function () {
$("img").attr("src", "../captcha?date=" + new Date());
});
</script>
</body>