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:
@@ -10,6 +10,7 @@ import lombok.Setter;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
@@ -66,9 +67,12 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
|
||||
return result;
|
||||
}
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
ImgWriter.write(bufferedImage, formatType, byteArrayOutputStream, -1);
|
||||
System.out.println("耗时:" + (System.currentTimeMillis() - currentTimeMillis));
|
||||
if (CaptchaImageUtils.isPng(formatType) || CaptchaImageUtils.isJpeg(formatType)) {
|
||||
// 如果是 jpg 或者 png图片的话 用hutool的生成
|
||||
ImgWriter.write(bufferedImage, formatType, byteArrayOutputStream, -1);
|
||||
}else {
|
||||
ImageIO.write(bufferedImage, formatType, byteArrayOutputStream);
|
||||
}
|
||||
//转换成字节码
|
||||
byte[] data = byteArrayOutputStream.toByteArray();
|
||||
String base64 = Base64.getEncoder().encodeToString(data);
|
||||
|
||||
@@ -8,9 +8,7 @@ import java.awt.*;
|
||||
import java.awt.geom.Area;
|
||||
import java.awt.geom.CubicCurve2D;
|
||||
import java.awt.geom.QuadCurve2D;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.awt.image.PixelGrabber;
|
||||
import java.awt.image.RenderedImage;
|
||||
import java.awt.image.*;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
@@ -564,4 +562,5 @@ public class CaptchaImageUtils {
|
||||
public static boolean isPng(String type) {
|
||||
return TYPE_PNG.equalsIgnoreCase(type);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user