只需創建一個具有透明度的新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