• EasyCaptcha图形验证码工具


    介绍

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

    快速开始

    导入依赖

    1. <dependency>
    2. <groupId>com.github.whvcsegroupId>
    3. <artifactId>easy-captchaartifactId>
    4. <version>1.6.2version>
    5. dependency>

    静态验证码

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

    动态验证码

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

    中文验证码

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

    动态中文验证码

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

    算数验证码

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

    验证码字符类型

    类型描述
    TYPE_DEFAULT数字和字母混合
    TYPE_ONLY_NUMBER纯数字
    TYPE_ONLY_CHAR纯字母
    TYPE_ONLY_UPPER纯大写字母
    TYPE_ONLY_LOWER纯小写字母
    TYPE_NUM_AND_UPPER数字和大写字母
    1. SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
    2. 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

    1. SpecCaptcha captcha = new SpecCaptcha(130, 48, 5);
    2. // 设置内置字体
    3. captcha.setFont(Captcha.FONT_1);
    4. // 设置系统字体
    5. captcha.setFont(new Font("楷体", Font.PLAIN, 28));

     图片输出到文件

    1. FileOutputStream outputStream = new FileOutputStream(new File("C:/captcha.png"))
    2. SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 5);
    3. specCaptcha.out(outputStream);
  • 相关阅读:
    大学迷茫怎么办?
    【适合所有群体】云原生从入门到精通(GO、Docker、K8S、微服务)【只此一文,踏入山巅】(持续更新,预计2022年7月底完结)
    智能方案设计——智能跳绳方案
    JAVA中如何实现代码优化
    Flutter Image组件如何处理图片加载过程中的错误?
    微信小程序--微信开发者工具使用小技巧(3)
    DSP 开发例程: led_flash
    【后端】Ubuntu开放mysql端口访问;如何开放服务器mysql给其他ip使用;在Ubuntu/Linux环境下开放3306端口
    基于SpringBoot+MyBatis实现的个人博客系统(一)
    火星探测器背后的人工智能:从原理到实战的强化学习
  • 原文地址:https://blog.csdn.net/m0_56750901/article/details/126541722