依赖jar包

引入包 版本
jdk 1.8
spring boot 2.7.2
aviator 5.3.1
spring-boot-starter-web 2.7.2

使用

添加依赖

<dependency>
  <groupId>cn.allbs</groupId>
  <artifactId>allbs-captcha</artifactId>
  <version>1.1.8</version>
</dependency>
implementation 'cn.allbs:allbs-captcha:1.1.8'
implementation("cn.allbs:allbs-captcha:1.1.8")

构建CaptchaDetails

CaptchaDetails captchaInfo = CaptchaInfo.builder().build();

属性说明

属性 说明
len 验证码的字符个数 默认4
width 验证码图片的宽度 默认130
height 验证码图片默认的宽度 默认38
font 验证码默认的字体Arial 加粗 32px 系统提供10种默认子图可通过 FontEnum.rFont(fontType, 32f)的方式创建
color 验证码默认的背景色 默认为白色
type 验证码的类型 默认为数字字符混合
interfereCount 干扰个数 默认15
imageType 图片的类型 默认为gif 图片类型定义在ImageEnum中 1为png,2为jpg,3为gif

fontType 对应情况

部分字体会导致小写字母变为大写

fontType 字体名称 字体 效果
0 actionj FontEnum.FONT_1
1 epilog FontEnum.FONT_2
2 fresnel FontEnum.FONT_3
3 headache FontEnum.FONT_4
4 lexo FontEnum.FONT_5
5 prefix FontEnum.FONT_6
6 progbot FontEnum.FONT_7
7 ransom FontEnum.FONT_8
8 robot FontEnum.FONT_9
9 scandal FontEnum.FONT_10

传入构建的CaptchaDetails和HttpServletResponse

CaptchaUtil.captcha(captchaInfo, response)
    
# 或者
CaptchaDetails captchaInfo = CaptchaInfo.builder()
                .size(4, 130, 38)
                .build();
AbstractCaptcha captcha = CaptchaUtil.captcha(captchaInfo);
captcha.create();
captcha.write(response.getOutputStream());

# 中文字符构建方式
CaptchaDetails captchaInfo = ChineseCaptchaInfo.builder()
                .size(4, 130, 38)
                .build();
AbstractCaptcha captcha = CaptchaUtil.captcha(captchaInfo);
captcha.create();
captcha.write(response.getOutputStream());

使用示例

纯数字

CaptchaDetails captchaInfo = CaptchaInfo.builder()
                .size(4, 130, 38)
                .type(CaptchaEnum.ONLY_NUMBER.getType())
                .font(FontEnum.rFont(1, 32f))
                .build();
AbstractCaptcha captcha = CaptchaUtil.captcha(captchaInfo);
captcha.create();
captcha.getImage();
captcha.write(response.getOutputStream());
# 此次验证码内容
captcha.getText()

数字字母混合

CaptchaDetails captchaInfo = CaptchaInfo.builder()
                .size(4, 130, 38)
                .type(CaptchaEnum.NUMBER_CHAR_MIXTURE.getType())
                .font(FontEnum.rFont(10, 32f))
                .build();
String code = CaptchaUtil.captcha(captchaInfo, response);

纯字母

CaptchaDetails captchaInfo = CaptchaInfo.builder()
                .size(4, 130, 38)
                .type(CaptchaEnum.ONLY_CHAR.getType())
                .font(FontEnum.rFont(2, 32f))
                .build();
String code = CaptchaUtil.captcha(captchaInfo, response);

纯大写字母

CaptchaDetails captchaInfo = CaptchaInfo.builder()
                .size(4, 130, 38)
                .type(CaptchaEnum.ONLY_UPPER_CHAR.getType())
                .font(FontEnum.rFont(3, 32f))
                .build();
String code = CaptchaUtil.captcha(captchaInfo, response);

纯小写字母并输出到文件

CaptchaDetails captchaInfo = CaptchaInfo.builder()
                .font(new Font("微软雅黑", Font.BOLD, 24))
                .size(5, 130, 38)
                .type(CaptchaEnum.ONLY_LOWER_CHAR.getType())
                .imageType(ImageEnum.JPG.getType())
                .color(new Color(234, 254, 222))
                .build();
AbstractCaptcha captcha = CaptchaUtil.captcha(captchaInfo);
        captcha.create();
File file = new File("D://lower.jpg");
if (!file.exists()) {
    file.createNewFile();
}
captcha.write(file);

纯小写字母

数字计算(默认两个数字计算,修改size中的len可增加参与计算数字)

CaptchaDetails captchaInfo = CaptchaInfo.builder()
                .font(FontEnum.rFont(4, 32f))
                .size(2, 130, 38)
                .type(CaptchaEnum.NUMBER_COUNT.getType())
                .imageType(ImageEnum.GIF.getType())
                .color(Color.CYAN)
                .build();
AbstractCaptcha captcha = CaptchaUtil.captcha(captchaInfo);
captcha.create();
captcha.write(response.getOutputStream());
System.out.println(captcha.getText());

中文验证码

CaptchaDetails captchaInfo = ChineseCaptchaInfo.builder()
                .font(new Font("黑体", Font.BOLD, 24))
                .imageType(ImageEnum.GIF.getType())
                .color(Color.CYAN)
                .build();
AbstractCaptcha captcha = CaptchaUtil.captcha(captchaInfo);
captcha.create();
captcha.write(response.getOutputStream());

汉字验证码