package top.octopusyan.utils;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;

import java.awt.image.BufferedImage;
import java.util.Properties;

/**
 * @author : octopus yan
 * @email : octopus_yan@foxmail.com
 * @description :  验证码图片生成
 * @create : 2022-4-3 23:34
 */
public class KaptchaUtil {
    public static volatile KaptchaUtil util;
    private final DefaultKaptcha defaultKaptcha;

    private KaptchaUtil(DefaultKaptcha kaptcha){
        this.defaultKaptcha = kaptcha;
    }

    public static KaptchaUtil getInstance() {
        Properties properties = new Properties();
        properties.put("kaptcha.border", "no");
        properties.put("kaptcha.textproducer.font.color", "black");
        properties.put("kaptcha.textproducer.char.space", "5");
        properties.put("kaptcha.textproducer.font.names", "Arial,Courier,cmr10,宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        defaultKaptcha.setConfig(config);
        if(util == null) util = new KaptchaUtil(defaultKaptcha);
        return util;
    }

    public BufferedImage getCaptcha(String code) {
        return util.defaultKaptcha.createImage(code);
    }

    public String createCode(){
        return util.defaultKaptcha.createText();
    }
}
