1. Number addition and subtraction tools
1.1 Effect
1.2 base64 package
<dependency>
<groupId>org.apache.axis</groupId>
<artifactId>axis</artifactId>
<version>1.4</version>
</dependency>
base image transfer address
base64 image online conversion tool - webmaster tools
1.3 Code
@Data
public class ImageCode {
//图形结果
public String code;
//图片
public ByteArrayInputStream image;
//宽
private int wight = 400;
//高
private int height = 100;
public static ImageCode getInstance() {
return new ImageCode();
}
public ImageCode() {
//图形缓冲区
BufferedImage image = new BufferedImage(wight, height, BufferedImage.TYPE_3BYTE_BGR);
//画笔
Graphics graphics = image.getGraphics();
//设置颜色
graphics.setColor(new Color(46, 173, 144));
//画矩形
graphics.fillRect(0, 0, wight, height);
//设置字体
graphics.setFont(new Font("宋体", Font.PLAIN, 30));
//设置随机数
Random random = new Random();
//算数验证码
int num1 = random.nextInt(20);
int num2 = random.nextInt(20);
//设置颜色
graphics.setColor(new Color(2, 0, 0));
//画图
int Y = 60;
graphics.drawString(String.valueOf(num1), wight / 6 * 0 + 50, Y);
graphics.drawString("+", wight / 6 * 1 + 50, Y);
graphics.drawString(String.valueOf(num2), wight / 6 * 2 + 50, Y);
graphics.drawString("=", wight / 6 * 3 + 50, Y);
graphics.drawString("?", wight / 6 * 4 + 50, Y);
//计算值
int result = num1 + num2;
this.code = result + "";
//收笔
graphics.dispose();
ByteArrayInputStream inputStream = null;
ByteOutputStream outputStream = new ByteOutputStream();
//赋值给ByteArrayInputStream
try {
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
ImageIO.write(image, "png", imageOutputStream);
inputStream = new ByteArrayInputStream(outputStream.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
//生成图片
this.image = inputStream;
}
public static void main(String[] args) throws IOException {
ImageCode imageCode = new ImageCode();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ByteArrayInputStream inputStream = imageCode.getImage();
byte[] bytes = new byte[1024];
while (inputStream.read(bytes) != -1) {
outputStream.write(bytes);
}
String base64 = Base64.encode(outputStream.toByteArray());
outputStream.flush();
outputStream.close();
inputStream.close();
System.out.println("计算结果:"+imageCode.getCode());
System.out.println("data:image/png;base64,"+base64);
}
}
2. Confusing tool production verification code
2.1 Guide package
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.18</version>
</dependency>
2.2 Effect
2.3 Code
@GetMapping("test1")
public void test1(HttpServletResponse response) throws IOException {
//定义图形验证码的长和宽
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 5);
System.out.println("获取图形验证码结果,进行缓存" + lineCaptcha.getCode());
System.out.println("转成base64 -> "+"data:image/png;base64,"+lineCaptcha.getImageBase64());
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
//验证码写到页面上
lineCaptcha.write(outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}
3. Happy-captchaProduction verification code
官网:https://gitee.com/ramostear/Happy-Captcha?_from=gitee_search
3.1 Effect
3.2 Guide package
<dependency>
<groupId>com.ramostear</groupId>
<artifactId>Happy-Captcha</artifactId>
<version>1.0.1</version>
</dependency>
3.3 Code
@GetMapping("test2")
public void test2(HttpServletResponse response, HttpServletRequest request) {
//生成验证码
HappyCaptcha.require(request, response)
.style(CaptchaStyle.ANIM) //设置样式
.build()
.finish();
}
@GetMapping("test3")
public String test3(String code, HttpServletRequest request) {
boolean flag = HappyCaptcha.verification(request,code,true);
if(flag){
//通过了当前验证以后,移除当前验证
HappyCaptcha.remove(request);
System.out.println("通过了验证");
return "success";
}
return "fail";
}
4. easy-captcha generates verification code
4.1 Effect
4.2 Guide package
<dependency>
<groupId>com.github.whvcse</groupId>
<artifactId>easy-captcha</artifactId>
<version>1.6.2</version>
</dependency>
4.3 Code
@GetMapping("test4")
public void test4(HttpServletResponse response, HttpServletRequest request) throws IOException {
// 普通验证码
SpecCaptcha specCaptcha = new SpecCaptcha();
System.out.println("验证码结果:"+specCaptcha.text());
com.wf.captcha.utils.CaptchaUtil.out(specCaptcha,request,response);
}
@GetMapping("test5")
public void test5(HttpServletResponse response, HttpServletRequest request) throws IOException {
// 生成算数验证码
ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha();
//设置3为计算长度
arithmeticCaptcha.setLen(3);
System.out.println("验证码结果:"+arithmeticCaptcha.text());
com.wf.captcha.utils.CaptchaUtil.out(arithmeticCaptcha,request,response);
}