mirror of
https://github.com/dromara/tianai-captcha.git
synced 2026-09-01 03:15:16 +08:00
U 解决 背景图为 png 设置指定转换类型为 jpg 导致的图片变色问题
This commit is contained in:
@@ -10,6 +10,7 @@ import lombok.Setter;
|
|||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -66,9 +67,12 @@ public abstract class AbstractImageCaptchaGenerator implements ImageCaptchaGener
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
long currentTimeMillis = System.currentTimeMillis();
|
if (CaptchaImageUtils.isPng(formatType) || CaptchaImageUtils.isJpeg(formatType)) {
|
||||||
ImgWriter.write(bufferedImage, formatType, byteArrayOutputStream, -1);
|
// 如果是 jpg 或者 png图片的话 用hutool的生成
|
||||||
System.out.println("耗时:" + (System.currentTimeMillis() - currentTimeMillis));
|
ImgWriter.write(bufferedImage, formatType, byteArrayOutputStream, -1);
|
||||||
|
}else {
|
||||||
|
ImageIO.write(bufferedImage, formatType, byteArrayOutputStream);
|
||||||
|
}
|
||||||
//转换成字节码
|
//转换成字节码
|
||||||
byte[] data = byteArrayOutputStream.toByteArray();
|
byte[] data = byteArrayOutputStream.toByteArray();
|
||||||
String base64 = Base64.getEncoder().encodeToString(data);
|
String base64 = Base64.getEncoder().encodeToString(data);
|
||||||
|
|||||||
@@ -8,9 +8,7 @@ import java.awt.*;
|
|||||||
import java.awt.geom.Area;
|
import java.awt.geom.Area;
|
||||||
import java.awt.geom.CubicCurve2D;
|
import java.awt.geom.CubicCurve2D;
|
||||||
import java.awt.geom.QuadCurve2D;
|
import java.awt.geom.QuadCurve2D;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.*;
|
||||||
import java.awt.image.PixelGrabber;
|
|
||||||
import java.awt.image.RenderedImage;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -564,4 +562,5 @@ public class CaptchaImageUtils {
|
|||||||
public static boolean isPng(String type) {
|
public static boolean isPng(String type) {
|
||||||
return TYPE_PNG.equalsIgnoreCase(type);
|
return TYPE_PNG.equalsIgnoreCase(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user