All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.springframework.boxes.cookie.starter.captcha.BoxesCaptchaHandler Maven / Gradle / Ivy

There is a newer version: 2.0.6
Show newest version
package com.springframework.boxes.cookie.starter.captcha;

import com.google.code.kaptcha.Producer;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.util.FastByteArrayOutputStream;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public abstract class BoxesCaptchaHandler {

    /**
     * 生成验证码文本
     * @return
     */
    public String createCaptchaVal(){
        return getProducer().createText().toUpperCase();
    }

    /**
     * 生成验证码图片
     * @param captchaVal 验证码内容
     * @return 返回结果
     */
    public Resource createImage(String captchaVal){
        BufferedImage bufferedImage = getProducer().createImage(captchaVal);
        try (FastByteArrayOutputStream out = new FastByteArrayOutputStream()) {
            ImageIO.write(bufferedImage, "JPEG", out);
            return new ByteArrayResource(out.toByteArray());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public abstract Producer getProducer();

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy