mirror of
https://github.com/dromara/tianai-captcha.git
synced 2026-08-31 02:46:10 +08:00
U 添加 背景图为 png 设置指定转换类型为 jpg 导致的图片变色警告日志
This commit is contained in:
@@ -2,10 +2,12 @@ package cloud.tianai.captcha.generator;
|
|||||||
|
|
||||||
import cloud.tianai.captcha.generator.common.model.dto.GenerateParam;
|
import cloud.tianai.captcha.generator.common.model.dto.GenerateParam;
|
||||||
import cloud.tianai.captcha.generator.common.model.dto.ImageCaptchaInfo;
|
import cloud.tianai.captcha.generator.common.model.dto.ImageCaptchaInfo;
|
||||||
|
import cloud.tianai.captcha.generator.common.util.CaptchaImageUtils;
|
||||||
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
@@ -20,6 +22,7 @@ import java.util.Map;
|
|||||||
* @date 2022/4/22 16:30
|
* @date 2022/4/22 16:30
|
||||||
* @Description 抽象的验证码生成器
|
* @Description 抽象的验证码生成器
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGenerator {
|
public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGenerator {
|
||||||
public static String DEFAULT_BG_IMAGE_TYPE = "jpeg";
|
public static String DEFAULT_BG_IMAGE_TYPE = "jpeg";
|
||||||
public static String DEFAULT_SLIDER_IMAGE_TYPE = "png";
|
public static String DEFAULT_SLIDER_IMAGE_TYPE = "png";
|
||||||
@@ -58,6 +61,11 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
|
|||||||
*/
|
*/
|
||||||
@SneakyThrows(IOException.class)
|
@SneakyThrows(IOException.class)
|
||||||
public String transform(BufferedImage bufferedImage, String formatType) {
|
public String transform(BufferedImage bufferedImage, String formatType) {
|
||||||
|
// 这里判断处理一下,加一些警告日志
|
||||||
|
String result = beforeTransform(bufferedImage, formatType);
|
||||||
|
if (result != null) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
ImageIO.write(bufferedImage, formatType, byteArrayOutputStream);
|
ImageIO.write(bufferedImage, formatType, byteArrayOutputStream);
|
||||||
//转换成字节码
|
//转换成字节码
|
||||||
@@ -66,6 +74,23 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
|
|||||||
return "data:image/" + formatType + ";base64,".concat(base64);
|
return "data:image/" + formatType + ";base64,".concat(base64);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String beforeTransform(BufferedImage bufferedImage, String formatType) {
|
||||||
|
int type = bufferedImage.getType();
|
||||||
|
if (BufferedImage.TYPE_4BYTE_ABGR == type) {
|
||||||
|
// png , 如果转换的是jpg的话
|
||||||
|
if (CaptchaImageUtils.isJpeg(formatType)) {
|
||||||
|
// bufferedImage为 png, 但是转换的图片为 jpg
|
||||||
|
if (log.isWarnEnabled()) {
|
||||||
|
log.warn("图片验证码转换警告, 原图为 png格式时,指定转换的图片为jpg格式时可能会导致转换异常,如果转换的图片为出现错误,请设置指定转换的类型与原图的类型一致");
|
||||||
|
} else {
|
||||||
|
System.err.println("图片验证码转换警告, 原图为 png格式时,指定转换的图片为jpg格式时可能会导致转换异常,如果转换的图片为出现错误,请设置指定转换的类型与原图的类型一致");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 其它的暂时不考虑
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
protected InputStream getTemplateFile(Map<String, Resource> templateImages, String imageName) {
|
protected InputStream getTemplateFile(Map<String, Resource> templateImages, String imageName) {
|
||||||
Resource resource = templateImages.get(imageName);
|
Resource resource = templateImages.get(imageName);
|
||||||
if (resource == null) {
|
if (resource == null) {
|
||||||
|
|||||||
@@ -463,5 +463,24 @@ public class CaptchaImageUtils {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后缀是否是jpg
|
||||||
|
*
|
||||||
|
* @param type type
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static boolean isJpeg(String type) {
|
||||||
|
return "jpg".equalsIgnoreCase(type) || "jpeg".equalsIgnoreCase(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 后缀是否是 png
|
||||||
|
*
|
||||||
|
* @param type type
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public static boolean isPng(String type) {
|
||||||
|
return "png".equalsIgnoreCase(type);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package example;
|
||||||
|
|
||||||
|
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
|
||||||
|
import cloud.tianai.captcha.generator.common.constant.SliderCaptchaConstant;
|
||||||
|
import cloud.tianai.captcha.generator.common.model.dto.GenerateParam;
|
||||||
|
import cloud.tianai.captcha.generator.common.model.dto.ImageCaptchaInfo;
|
||||||
|
import cloud.tianai.captcha.generator.impl.MultiImageCaptchaGenerator;
|
||||||
|
import cloud.tianai.captcha.resource.ImageCaptchaResourceManager;
|
||||||
|
import cloud.tianai.captcha.resource.ResourceStore;
|
||||||
|
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
||||||
|
import cloud.tianai.captcha.resource.impl.DefaultImageCaptchaResourceManager;
|
||||||
|
import cloud.tianai.captcha.resource.impl.provider.ClassPathResourceProvider;
|
||||||
|
import cloud.tianai.captcha.resource.impl.provider.FileResourceProvider;
|
||||||
|
import cloud.tianai.captcha.validator.ImageCaptchaValidator;
|
||||||
|
import cloud.tianai.captcha.validator.impl.BasicCaptchaTrackValidator;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator.DEFAULT_SLIDER_IMAGE_RESOURCE_PATH;
|
||||||
|
import static cloud.tianai.captcha.generator.impl.StandardSliderImageCaptchaGenerator.DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH;
|
||||||
|
|
||||||
|
public class CaptchaGenTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
ImageCaptchaResourceManager imageCaptchaResourceManager = new DefaultImageCaptchaResourceManager();
|
||||||
|
ResourceStore resourceStore = imageCaptchaResourceManager.getResourceStore();
|
||||||
|
// 添加一些系统的资源文件
|
||||||
|
resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource(FileResourceProvider.NAME, "C:\\Users\\Thinkpad\\Desktop\\111\\test.png"));
|
||||||
|
|
||||||
|
// 添加一些系统的 模板文件
|
||||||
|
Map<String, Resource> template1 = new HashMap<>(4);
|
||||||
|
template1.put(SliderCaptchaConstant.TEMPLATE_ACTIVE_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/1/active.png")));
|
||||||
|
template1.put(SliderCaptchaConstant.TEMPLATE_FIXED_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/1/fixed.png")));
|
||||||
|
template1.put(SliderCaptchaConstant.TEMPLATE_MATRIX_IMAGE_NAME, new Resource(ClassPathResourceProvider.NAME, DEFAULT_SLIDER_IMAGE_TEMPLATE_PATH.concat("/1/matrix.png")));
|
||||||
|
resourceStore.addTemplate(CaptchaTypeConstant.SLIDER, template1);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
MultiImageCaptchaGenerator imageCaptchaGenerator = new MultiImageCaptchaGenerator(imageCaptchaResourceManager, false);
|
||||||
|
GenerateParam generateParam = GenerateParam.builder()
|
||||||
|
.type(CaptchaTypeConstant.SLIDER)
|
||||||
|
.backgroundFormatName("jpg")
|
||||||
|
.sliderFormatName("png")
|
||||||
|
.obfuscate(false)
|
||||||
|
.build();
|
||||||
|
ImageCaptchaInfo imageCaptchaInfo = imageCaptchaGenerator.generateCaptchaImage(generateParam);
|
||||||
|
System.out.println(imageCaptchaInfo.getBackgroundImage());
|
||||||
|
// System.out.println(imageCaptchaInfo.getSliderImage());
|
||||||
|
|
||||||
|
// 负责计算一些数据存到缓存中,用于校验使用
|
||||||
|
// ImageCaptchaValidator负责校验用户滑动滑块是否正确和生成滑块的一些校验数据; 比如滑块到凹槽的百分比值
|
||||||
|
ImageCaptchaValidator imageCaptchaValidator = new BasicCaptchaTrackValidator();
|
||||||
|
// 这个map数据应该存到缓存中,校验的时候需要用到该数据
|
||||||
|
Map<String, Object> map = imageCaptchaValidator.generateImageCaptchaValidData(imageCaptchaInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
package example;
|
||||||
|
|
||||||
|
import cloud.tianai.captcha.generator.common.util.CaptchaImageUtils;
|
||||||
|
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
|
||||||
|
public class DemoTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws FileNotFoundException {
|
||||||
|
BufferedImage bufferedImage = CaptchaImageUtils.wrapFile2BufferedImage(new FileInputStream("E:\\projects\\tianai-captcha\\src\\main\\resources\\META-INF\\cut-image\\resource\\1.jpg"));
|
||||||
|
System.out.println(bufferedImage.getType());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user