我有多个透明BufferedImage
实例,我想将它们叠加在一起(又名 Photoshop 图层)并烘焙到一个BufferedImage
输出中。我该怎么做呢?
问问题
9157 次
3 回答
10
我会说最好的选择是获取缓冲图像,并创建一个额外的图像,以便有一个要附加的对象。然后只需使用 Graphics.drawImage() 将它们放在一起。
所以沿着这些思路:
BufferedImage a = ImageIO.read(new File(filePath, "a.png"));
BufferedImage b = ImageIO.read(new File(filePath, "b.png"));
BufferedImage c = new BufferedImage(a.getWidth(), a.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = c.getGraphics();
g.drawImage(a, 0, 0, null);
g.drawImage(b, 0, 0, null);
于 2012-06-22T00:05:36.790 回答
3
假设第一个 BufferedImage 命名为 bi1,第二个命名为 bi2,而您想要将它们分层的图像是目标。
BufferedImage target=new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
Graphics2D targetGraphics=target.createImage();
targetGraphics.drawImage(bi1,0,0,null);//draws the first image onto it
int[] pixels2=((DataBufferInt) bi2.getRaster().getDataBuffer()).getData();
int[] pixelsTgt=((DataBufferInt) target.getRaster().getDataBuffer()).getData();
for(int a=0;a<pixels2.length;a++)
{
pixelsTgt[a]+=pixels2[a];//this adds the pixels together
}
确保所有三个 BufferedImage 对象都是 TYPE_INT_ARGB 以便打开 alpha。如果两者加在一起大于最大整数,这可能无法为您提供您想要的确切结果,因此您可能需要添加一些东西来帮助解决这个问题。像素使用位移来添加颜色,其整数排序为 AARRGGBB。
于 2012-06-22T00:10:40.977 回答
2
还要考虑AlphaComposite
可用于图形上下文的模式,此处讨论。
于 2012-06-22T01:13:13.353 回答