Commit 30a41d42 by 王祖波

优化imageIo读取

parent f09012d8
......@@ -9,11 +9,15 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
/**
......@@ -38,11 +42,11 @@ public class ImageCombined {
private static final int QR_WIDTH_REFERENCE = 136;
private static final String LARGE_IMAGE_URL = "https://jhdmyx-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-2e81265ed1bf4c4d8a623d6b04942767.jpeg?imageView2/2/w/1080/h/10800/format/jpg";
private static final String LARGE_IMAGE_URL = "https://wcy-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-0e89ce4dc0454998a16e5295a32e13ba.jpg?imageView2/2/w/1080/h/10800/format/jpg";
private static final String QR_CODE_URL = "https://gicinner-1251519181.cos.ap-shanghai.myqcloud.com/image/material_content-4ffc77073ca1476fb264bf1be9f11383.png";
private static final String STORE_NAME = "门店CCA";
private static final String GUIDE_NAME = "宇智222为您推荐";
private static final String OUTPUT_PATH = "/Users/wang/Downloads/output_image3.jpg";
private static final String OUTPUT_PATH = "/Users/wang/Downloads/output_image4.jpg";
static {
ImageIO.scanForPlugins(); // 初始化图像插件
......@@ -70,7 +74,7 @@ public class ImageCombined {
//压缩图片的参数
imageUrl = imageUrl + "?imageView2/2/w/1080/h/10800/format/jpg";
BufferedImage largeImage = loadImageWithCache(imageUrl);
BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeUrl));
BufferedImage qrCodeImage = loadImageWithReader(qrCodeUrl);
if (largeImage == null || qrCodeImage == null) {
return null;
}
......@@ -84,16 +88,33 @@ public class ImageCombined {
}
}
private static BufferedImage loadImageWithReader(String url) {
try {
URL imageUrl = new URL(url);
ImageInputStream input = ImageIO.createImageInputStream(imageUrl.openStream());
Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
if (readers.hasNext()) {
ImageReader reader = readers.next();
reader.setInput(input, true);
return reader.read(0);
}
} catch (IOException e) {
logger.error("读取异常: {}", url, e);
}
return null;
}
/**
* 从缓存中加载图像
*/
private static BufferedImage loadImageWithCache(String url) {
return imageCache.get(url, key -> {
try {
logger.info("加载图片buffered:{},key:{}",url,key);
return ImageIO.read(new URL(key));
logger.info("加载图片buffered:{},key:{}", url, key);
return loadImageWithReader(key); // 使用读取器方法
} catch (Exception e) {
logger.info("获取图片异常",e);
logger.error("获取图片异常", e);
return null;
}
});
......@@ -104,7 +125,7 @@ public class ImageCombined {
logger.info("合成图片开始");
int width = image.getWidth();
int height = image.getHeight();
int qrWidth = QR_WIDTH_REFERENCE;
if (showStyle == 1) {
if (height > MAX_HEIGHT_NO_WHITE_SPACE) {
width = width * MAX_HEIGHT_NO_WHITE_SPACE / height;
......@@ -124,10 +145,8 @@ public class ImageCombined {
Graphics2D g = combinedImage.createGraphics();
g.drawImage(resizedImage, 0, 0, null);
int qrWidth = width * QR_WIDTH_REFERENCE / 750;
if (width <= MIN_WIDTH) {
qrWidth = 136;
if (showStyle == 1) {
qrWidth = width * QR_WIDTH_REFERENCE / 750;
}
BufferedImage resizedQRCode = Thumbnails.of(qrCode).size(qrWidth, qrWidth).asBufferedImage();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment