介绍
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);