Java实现生成图形验证码功能

图形验证码是系统中比较常见的一个功能,网上也有不少现成的解决方案,比如https://gitee.com/whvse/EasyCaptcha。

EasyCaptcha其实已经很方便了,但是它会要在某路径下生成一张实体图片,并且没有返回base64串的实现,本文就是为了解决这两个问题,实现过程中借鉴了EasyCaptcha的部分算法。

设计允许指定验证码的宽、高和字体大小:

#验证码图片高
cw.captcha.width=70
#验证码图片宽
cw.captcha.height=36
#验证码字体大小
cw.captcha.font-size=20

使用Bean来映射这段配置:

@Component
@ConfigurationProperties(prefix = "cw.captcha")
public class CaptchaProperties {
   
   /**
     * 验证码宽
     */
    private int width;

    /**
     * 验证码高
     */
    private int height;

    /**
     * 字体大小
     */
    private int fontSize;

    public int getWidth() {
   
        return width;
    }

    public void setWidth(int width) {
   
        this.width = width;
    }

    public int getHeight() {
   
        return height;
    }

    public void setHeight(int height) {
   
        this.height = height;
    }

    public int getFontSize() {
   
        return fontSize;
    }

    public void setFontSize(int fontSize) {
   
        this.fontSize = fontSize;
    }
}

定义一个生成验证码的interface:

public interface CaptchaProcessor {
   
    void generate();
}

实现一下:

@Component
public class CaptchaProcessorImpl implements CaptchaProcessor {
   

    @Autowired
    CaptchaProperties captchaProperties;

    private static char[] CODE_SEQUENCE = {
   'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值