我使用下面的代码合并两个图像。一个基本图像没有透明度,一个覆盖图像与透明度。 这些图像的文件大小分别是20kb和5kb。 一旦我合并了这两个图像,生成的文件大小> 100kb,因此至少是25kb大小的4倍。我预计文件大小小于25kb。合并两个图像 - > 4倍大小!如何减小文件大小?
public static void mergeTwoImages(BufferedImage base, BufferedImage overlay, String destPath, String imageName) {
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(base.getWidth(), overlay.getWidth());
int h = Math.max(base.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics2D g2 = combined.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawImage(base, 0, 0, null);
g2.drawImage(overlay, 0, 0, null);
g2.dispose();
// Save as new image
saveImage(combined, destPath + "/" + imageName + "_merged.png");
}
我的应用程序必须是具有非常好的性能,从而任何人都可以解释我为什么这个效应发生,我怎么能减少生成的文件大小?
非常感谢!编辑: 非常感谢您的回答。该saveImage代码:
public static void saveImage(BufferedImage src, String file) {
try {
File outputfile = new File(file);
ImageIO.write(src, "png", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
也许你应该张贴'saveImage()的代码'。这就是内部图像被转换为文件的地方。 – 2012-07-05 17:25:58
压缩算法对组合图像可能会比较困难,特别是在视觉上使图像复杂化的情况下。 – dlev 2012-07-05 17:29:56