Commit e0f7d146 by 王祖波

Revert "优化读取"

This reverts commit 590d1c73.
parent 10acc4bb
...@@ -97,8 +97,11 @@ public class ImageCombined { ...@@ -97,8 +97,11 @@ public class ImageCombined {
public static void main(String[] args) throws Exception{ public static void main(String[] args) throws Exception{
BufferedImage largeImage = loadImageWithCache(LARGE_IMAGE_URL); BufferedImage largeImage = loadImageWithCache(LARGE_IMAGE_URL);
BufferedImage qrCodeImage = ImageIO.read(new URL(QR_CODE_URL));
int showStyle = 0; int showStyle = 0;
BufferedImage combinedImage = generateImage(largeImage, QR_CODE_URL, showStyle,STORE_NAME, GUIDE_NAME);
BufferedImage combinedImage = generateImage(largeImage, qrCodeImage, showStyle,STORE_NAME, GUIDE_NAME);
ImageIO.write(combinedImage, "jpg", new File(OUTPUT_PATH)); ImageIO.write(combinedImage, "jpg", new File(OUTPUT_PATH));
} }
...@@ -114,22 +117,23 @@ public class ImageCombined { ...@@ -114,22 +117,23 @@ public class ImageCombined {
} }
//压缩图片的参数 //压缩图片的参数
imageUrl = imageUrl + "?imageView2/2/w/1080/h/10800/format/jpg"; imageUrl = imageUrl + "?imageView2/2/w/1080/h/10800/format/jpg";
logger.info("加载要合成的图片开始"); logger.info("加载要合成的图片开始1");
BufferedImage largeImage = loadImageWithCache(imageUrl); BufferedImage largeImage = loadImageWithCache(imageUrl);
logger.info("加载要合成的图片开始2");
BufferedImage qrCodeImage = loadImageWithReader(qrCodeUrl);
logger.info("加载要合成的图片结束"); logger.info("加载要合成的图片结束");
if (largeImage == null) { if (largeImage == null || qrCodeImage == null) {
return null;
}
BufferedImage combinedImage = generateImage(largeImage, qrCodeUrl, showStyle, lineOne, lineTwo);
if (combinedImage == null) {
return null; return null;
} }
BufferedImage combinedImage = generateImage(largeImage, qrCodeImage, showStyle, lineOne, lineTwo);
// ImageIO.write(combinedImage, "jpg", new File(OUTPUT_PATH)); // ImageIO.write(combinedImage, "jpg", new File(OUTPUT_PATH));
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImageIO.write(combinedImage, "jpg", baos); ImageIO.write(combinedImage, "jpg", baos);
// 返回字节数组 // 返回字节数组
return baos.toByteArray(); return baos.toByteArray();
}finally { }finally {
qrCodeImage.flush();
combinedImage.flush(); combinedImage.flush();
} }
} }
...@@ -169,8 +173,9 @@ public class ImageCombined { ...@@ -169,8 +173,9 @@ public class ImageCombined {
}); });
} }
private static BufferedImage generateImage(BufferedImage image, String qrCodeUrl, int showStyle,String lineOne,String lineTwo) { private static BufferedImage generateImage(BufferedImage image, BufferedImage qrCode, int showStyle,String lineOne,String lineTwo) throws Exception {
logger.info("合成图片开始");
int width = image.getWidth(); int width = image.getWidth();
int height = image.getHeight(); int height = image.getHeight();
int qrWidth = QR_WIDTH_REFERENCE; int qrWidth = QR_WIDTH_REFERENCE;
...@@ -188,48 +193,30 @@ public class ImageCombined { ...@@ -188,48 +193,30 @@ public class ImageCombined {
} }
int finalHeight = height + (showStyle == 1 ? 0 : WHITE_SPACE_HEIGHT); int finalHeight = height + (showStyle == 1 ? 0 : WHITE_SPACE_HEIGHT);
if (showStyle == 1) {
qrWidth = width * QR_WIDTH_REFERENCE / 750;
}
BufferedImage resizedQRCode = null;
BufferedImage resizedImage = null;
try {
logger.info("加载小程序码图片开始");
resizedQRCode = Thumbnails.of(new URL(qrCodeUrl)).size(qrWidth, qrWidth).asBufferedImage();
logger.info("加载小程序码图片结束");
if (resizedQRCode == null) {
return null;
}
logger.info("合成图片开始");
BufferedImage combinedImage = new BufferedImage(width, finalHeight, BufferedImage.TYPE_INT_RGB); BufferedImage combinedImage = new BufferedImage(width, finalHeight, BufferedImage.TYPE_INT_RGB);
resizedImage = Thumbnails.of(image).size(width, height).asBufferedImage();
BufferedImage resizedImage = Thumbnails.of(image)
.size(width, height).asBufferedImage();
Graphics2D g = combinedImage.createGraphics(); Graphics2D g = combinedImage.createGraphics();
g.setColor(Color.WHITE); g.setColor(Color.WHITE);
g.drawImage(resizedImage, 0, 1, null); g.drawImage(resizedImage, 0, 1, null);
if (showStyle == 1) {
qrWidth = width * QR_WIDTH_REFERENCE / 750;
}
BufferedImage resizedQRCode = Thumbnails.of(qrCode).size(qrWidth, qrWidth).asBufferedImage();
if (showStyle == 1) { if (showStyle == 1) {
// 不需要留白时,QR码距离底部20px
g.drawImage(resizedQRCode, width - qrWidth - 20, finalHeight - qrWidth - 20, null); g.drawImage(resizedQRCode, width - qrWidth - 20, finalHeight - qrWidth - 20, null);
} else { } else {
// 需要留白时
addWhiteSpace(g, width, finalHeight, resizedQRCode, qrWidth, lineOne, lineTwo); addWhiteSpace(g, width, finalHeight, resizedQRCode, qrWidth, lineOne, lineTwo);
} }
g.dispose(); g.dispose();
logger.info("合成图片结束"); logger.info("合成图片结束");
return combinedImage; return combinedImage;
} catch (Exception e) {
logger.error("生成合成图像时发生异常:", e);
return null;
} finally {
if (resizedQRCode != null) {
resizedQRCode.flush();
}
if (resizedImage != null) {
resizedImage.flush();
}
}
} }
private static void addWhiteSpace(Graphics2D g, int width, int finalHeight, BufferedImage qrCode, int qrWidth,String lineOne,String lineTwo) { private static void addWhiteSpace(Graphics2D g, int width, int finalHeight, BufferedImage qrCode, int qrWidth,String lineOne,String lineTwo) {
......
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