EasyCaptcha图形验证码工具

介绍

Java图形验证码工具,支持gif(动图)、中文、算术等类型,可用于Java Web、JavaSE等项目。

快速开始

导入依赖

 <dependency>
     <groupId>com.github.whvcse</groupId>
     <artifactId>easy-captcha</artifactId>
     <version>1.6.2</version>
 </dependency>

静态验证码

 @GetMapping("/captcha123")
 public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
     
     // 设置请求头为输出图片类型
     response.setContentType("image/gif");
     response.setHeader("Pragma", "No-cache");
     response.setHeader("Cache-Control", "no-cache");
     response.setDateHeader("Expires", 0);
 ​
     // 三个参数分别为宽、高、位数
     SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
     // 设置字体(有默认字体,可以不用设置)
     specCaptcha.setFont(new Font("Verdana", Font.PLAIN, 32));
     // 设置类型,字母数字混合
     specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
     // 获取验证码内容
     String code = specCaptcha.text().toLowerCase()
     // 输出图片流
     specCaptcha.out(response.getOutputStream());
 }

动态验证码

 @RequestMapping("/captcha123")
 public void captcha2(HttpServletRequest request, HttpServletResponse response) throws Exception {
     // 设置请求头为输出图片类型
     response.setContentType("image/gif");
     response.setHeader("Pragma", "No-cache");
     response.setHeader("Cache-Control", "no-cache");
     response.setDateHeader("Expires", 0);
     // 三个参数分别为宽、高、位数
     GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
     // 获取验证码
     String code = gifCaptcha.text().toLowerCase();
     // 输出图片流
     gifCaptcha.out(response.getOutputStream());
 }

中文验证码

@RequestMapping("/captcha123")
public void captcha2(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 设置请求头为输出图片类型
    response.setContentType("image/gif");
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 三个参数分别为宽、高、位数
    ChineseCaptcha chineseCaptchaAbstract = new ChineseCaptcha(130,28,4);
    // 获取验证码
    String code = chineseCaptchaAbstract.text().toLowerCase();
    // 输出图片流
    chineseCaptchaAbstract.out(response.getOutputStream());
}

动态中文验证码

@RequestMapping("/captcha123")
public void captcha2(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 设置请求头为输出图片类型
    response.setContentType("image/gif");
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 三个参数分别为宽、高、位数
    ChineseGifCaptcha chineseGifCaptchaAbstract = new ChineseGifCaptcha(130,28,4);
    // 获取验证码
    String code = chineseGifCaptchaAbstract.text().toLowerCase();
    // 输出图片流
    chineseGifCaptchaAbstract.out(response.getOutputStream());
}

算数验证码

@RequestMapping("/captcha123")
public void captcha2(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 设置请求头为输出图片类型
    response.setContentType("image/gif");
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 三个参数分别为宽、高、位数
    ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(130 , 28 , 4);
    // 获取验证码
    String code = arithmeticCaptcha.text().toLowerCase();
    // 输出图片流
    arithmeticCaptcha.out(response.getOutputStream());
}

验证码字符类型

类型描述
TYPE_DEFAULT数字和字母混合
TYPE_ONLY_NUMBER纯数字
TYPE_ONLY_CHAR纯字母
TYPE_ONLY_UPPER纯大写字母
TYPE_ONLY_LOWER纯小写字母
TYPE_NUM_AND_UPPER数字和大写字母
SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
captcha.setCharType(Captcha.TYPE_ONLY_NUMBER);

字体设置

字体效果
Captcha.FONT_1

Captcha.FONT_2

Captcha.FONT_3

Captcha.FONT_4

Captcha.FONT_5

Captcha.FONT_6

Captcha.FONT_7

Captcha.FONT_8

Captcha.FONT_9

Captcha.FONT_10

SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);

// 设置内置字体
captcha.setFont(Captcha.FONT_1); 

// 设置系统字体
captcha.setFont(new Font("楷体", Font.PLAIN, 28)); 

 图片输出到文件

FileOutputStream outputStream = new FileOutputStream(new File("C:/captcha.png"))
SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
specCaptcha.out(outputStream);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙域、白泽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值