只需创建一个具有透明度的新BufferedImage,然后在其上绘制其他两个图像(具有完全或半透明)。 这是怎么看起来像:
示例代码(图像被称为 'image.png' 和 'overlay.png'):
File path = ... // base path of the images
// load source images
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));
// create the new image, canvas size is the max. of both image sizes
int w = Math.max(image.getWidth(), overlay.getWidth());
int h = Math.max(image.getHeight(), overlay.getHeight());
BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
// paint both images, preserving the alpha channels
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));
有没有办法有第二图像在第一个中心? – Ahmed 2013-01-14 19:03:09
不应该调用g.dispose();最后? – 2013-08-11 23:20:01
嗨,我遵循这种方法,并保存只有第二个图像,但不是第一个图像...任何帮助在这里将不胜感激.. – 2015-04-23 15:34:27