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,31 +195,43 @@ public class ImageCombined { ...@@ -192,31 +195,43 @@ 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;
BufferedImage combinedImage = new BufferedImage(width, finalHeight, BufferedImage.TYPE_INT_RGB); try {
BufferedImage combinedImage = new BufferedImage(width, finalHeight, BufferedImage.TYPE_INT_RGB);
BufferedImage resizedImage = Thumbnails.of(image) if (showStyle == 1) {
.size(width, height).asBufferedImage(); qrWidth = width * QR_WIDTH_REFERENCE / 750;
}
Graphics2D g = combinedImage.createGraphics(); resizedQRCode = Thumbnails.of(qrCode).size(qrWidth, qrWidth).asBufferedImage();
g.setColor(Color.WHITE); resizedImage = Thumbnails.of(image).size(width, height).asBufferedImage();
g.drawImage(resizedImage, 0, 1, null);
if (showStyle == 1) { Graphics2D g = combinedImage.createGraphics();
qrWidth = width * QR_WIDTH_REFERENCE / 750; g.setColor(Color.WHITE);
} g.drawImage(resizedImage, 0, 1, null);
BufferedImage resizedQRCode = Thumbnails.of(qrCode).size(qrWidth, qrWidth).asBufferedImage();
if (showStyle == 1) {
// 不需要留白时,QR码距离底部20px
g.drawImage(resizedQRCode, width - qrWidth - 20, finalHeight - qrWidth - 20, null);
} else {
// 需要留白时
addWhiteSpace(g, width, finalHeight, resizedQRCode, qrWidth, lineOne, lineTwo);
}
if (showStyle == 1) { g.dispose();
// 不需要留白时,QR码距离底部20px logger.info("合成图片结束");
g.drawImage(resizedQRCode, width - qrWidth - 20, finalHeight - qrWidth - 20, null); return combinedImage;
} else { } catch (Exception e) {
// 需要留白时 logger.error("生成合成图像时发生异常:", e);
addWhiteSpace(g, width, finalHeight, resizedQRCode, qrWidth, lineOne, lineTwo); return null;
} finally {
if (resizedQRCode != null) {
resizedQRCode.flush();
}
if (resizedImage != null) {
resizedImage.flush();
}
} }
g.dispose();
logger.info("合成图片结束");
return combinedImage;
} }
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