• 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);
  • 相关阅读:
    缓存知识总结
    Ribbon官网及参考文档
    【全网最详细】最全java面试题及答案(210道)
    python经典百题之交换数组元素
    vue3 404解决方法
    Mybatis入门实战
    数字博物馆如何设计搭建,一文了解数字博物馆解决方案
    数据挖掘实验:使用 Hadoop 实现 WordCount 应用
    Java on Azure Tooling 8月更新|以应用程序为中心的视图支持及 Azure 应用服务部署状态改进
    js JSON.stringify() 的简单了解之函数的转换
  • 原文地址:https://blog.csdn.net/m0_56750901/article/details/126541722