Commit 93de13ef by 王祖波

合成优化

parent e0f7d146
...@@ -126,6 +126,9 @@ public class ImageCombined { ...@@ -126,6 +126,9 @@ public class ImageCombined {
return null; return null;
} }
BufferedImage combinedImage = generateImage(largeImage, qrCodeImage, showStyle, lineOne, lineTwo); BufferedImage combinedImage = generateImage(largeImage, qrCodeImage, showStyle, lineOne, lineTwo);
if (combinedImage == null) {
return null;
}
// 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()) {
...@@ -173,7 +176,7 @@ public class ImageCombined { ...@@ -173,7 +176,7 @@ public class ImageCombined {
}); });
} }
private static BufferedImage generateImage(BufferedImage image, BufferedImage qrCode, int showStyle,String lineOne,String lineTwo) throws Exception { private static BufferedImage generateImage(BufferedImage image, BufferedImage qrCode, int showStyle,String lineOne,String lineTwo){
logger.info("合成图片开始"); logger.info("合成图片开始");
int width = image.getWidth(); int width = image.getWidth();
...@@ -192,19 +195,20 @@ public class ImageCombined { ...@@ -192,19 +195,20 @@ public class ImageCombined {
width = Math.max(width, MIN_WIDTH); width = Math.max(width, MIN_WIDTH);
} }
int finalHeight = height + (showStyle == 1 ? 0 : WHITE_SPACE_HEIGHT); int finalHeight = height + (showStyle == 1 ? 0 : WHITE_SPACE_HEIGHT);
BufferedImage resizedQRCode = null;
BufferedImage resizedImage = null;
try {
BufferedImage combinedImage = new BufferedImage(width, finalHeight, BufferedImage.TYPE_INT_RGB); BufferedImage combinedImage = new BufferedImage(width, finalHeight, BufferedImage.TYPE_INT_RGB);
if (showStyle == 1) {
BufferedImage resizedImage = Thumbnails.of(image) qrWidth = width * QR_WIDTH_REFERENCE / 750;
.size(width, height).asBufferedImage(); }
resizedQRCode = Thumbnails.of(qrCode).size(qrWidth, qrWidth).asBufferedImage();
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 // 不需要留白时,QR码距离底部20px
...@@ -217,6 +221,17 @@ public class ImageCombined { ...@@ -217,6 +221,17 @@ public class ImageCombined {
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