Commit 93de13ef by 王祖波

合成优化

parent e0f7d146
......@@ -126,6 +126,9 @@ public class ImageCombined {
return null;
}
BufferedImage combinedImage = generateImage(largeImage, qrCodeImage, showStyle, lineOne, lineTwo);
if (combinedImage == null) {
return null;
}
// ImageIO.write(combinedImage, "jpg", new File(OUTPUT_PATH));
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
......@@ -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("合成图片开始");
int width = image.getWidth();
......@@ -192,31 +195,43 @@ public class ImageCombined {
width = Math.max(width, MIN_WIDTH);
}
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);
BufferedImage resizedImage = Thumbnails.of(image)
.size(width, height).asBufferedImage();
Graphics2D g = combinedImage.createGraphics();
g.setColor(Color.WHITE);
g.drawImage(resizedImage, 0, 1, null);
if (showStyle == 1) {
qrWidth = width * QR_WIDTH_REFERENCE / 750;
}
BufferedImage resizedQRCode = Thumbnails.of(qrCode).size(qrWidth, qrWidth).asBufferedImage();
try {
BufferedImage combinedImage = new BufferedImage(width, finalHeight, BufferedImage.TYPE_INT_RGB);
if (showStyle == 1) {
qrWidth = width * QR_WIDTH_REFERENCE / 750;
}
resizedQRCode = Thumbnails.of(qrCode).size(qrWidth, qrWidth).asBufferedImage();
resizedImage = Thumbnails.of(image).size(width, height).asBufferedImage();
Graphics2D g = combinedImage.createGraphics();
g.setColor(Color.WHITE);
g.drawImage(resizedImage, 0, 1, null);
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) {
// 不需要留白时,QR码距离底部20px
g.drawImage(resizedQRCode, width - qrWidth - 20, finalHeight - qrWidth - 20, null);
} else {
// 需要留白时
addWhiteSpace(g, width, finalHeight, resizedQRCode, qrWidth, lineOne, lineTwo);
g.dispose();
logger.info("合成图片结束");
return combinedImage;
} catch (Exception e) {
logger.error("生成合成图像时发生异常:", e);
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) {
......
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