只需创建一个具有透明度的新 BufferedImage,然后在其上绘制其他两个图像(具有全透明度或半透明度)。这是它的外观:
示例代码(图像称为“图像.png”和“覆盖.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);
g.dispose();
// Save as new image
ImageIO.write(combined, "PNG", new File(path, "combined.png"));