只需创建一个具有透明性的新BufferedImage,然后在其上绘制其他两个图像(具有全透明或半透明)。它将是这样的:
示例代码(图像分别称为“ image.png”和“ overlay.png”):
File path = ...
BufferedImage image = ImageIO.read(new File(path, "image.png"));
BufferedImage overlay = ImageIO.read(new File(path, "overlay.png"));
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);
Graphics g = combined.getGraphics();
g.drawImage(image, 0, 0, null);
g.drawImage(overlay, 0, 0, null);
g.dispose();
ImageIO.write(combined, "PNG", new File(path, "combined.png"));