mirror of
https://github.com/dromara/tianai-captcha.git
synced 2026-05-07 06:04:34 +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.ImageCaptchaInfo;
|
||||
import cloud.tianai.captcha.generator.common.util.CaptchaImageUtils;
|
||||
import cloud.tianai.captcha.resource.common.model.dto.Resource;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
@@ -20,6 +22,7 @@ import java.util.Map;
|
||||
* @date 2022/4/22 16:30
|
||||
* @Description 抽象的验证码生成器
|
||||
*/
|
||||
@Slf4j
|
||||
public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGenerator {
|
||||
public static String DEFAULT_BG_IMAGE_TYPE = "jpeg";
|
||||
public static String DEFAULT_SLIDER_IMAGE_TYPE = "png";
|
||||
@@ -58,6 +61,11 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
|
||||
*/
|
||||
@SneakyThrows(IOException.class)
|
||||
public String transform(BufferedImage bufferedImage, String formatType) {
|
||||
// 这里判断处理一下,加一些警告日志
|
||||
String result = beforeTransform(bufferedImage, formatType);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
ImageIO.write(bufferedImage, formatType, byteArrayOutputStream);
|
||||
//转换成字节码
|
||||
@@ -66,6 +74,23 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
|
||||
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) {
|
||||
Resource resource = templateImages.get(imageName);
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user