Java 将一个BuffereImage绘制到另一个BuffereImage正在更改RGBA值
我在尝试复制Java 将一个BuffereImage绘制到另一个BuffereImage正在更改RGBA值,java,awt,bufferedimage,graphics2d,Java,Awt,Bufferedimage,Graphics2d,我在尝试复制buffereImage对象时遇到问题 我正在使用drawImage(BuffereImage image,int x,int y,ImageObserver)方法在新图像上绘制原始图像,我正在设置BuffereImage。为每个图像键入\u int\u ARGB,但是,当我打印新图像颜色的值时,RGBA值略有不同 我需要复制原始图像,因为我有一个JPanel保存一个图像作为背景。在我的应用程序的其他部分中,我必须从面板获取图像,但我希望返回一份副本,以避免从其他地方修改图像 我如何
buffereImage
对象时遇到问题
我正在使用drawImage(BuffereImage image,int x,int y,ImageObserver)
方法在新图像上绘制原始图像,我正在设置BuffereImage。为每个图像键入\u int\u ARGB
,但是,当我打印新图像颜色的值时,RGBA值略有不同
我需要复制原始图像,因为我有一个JPanel
保存一个图像作为背景。在我的应用程序的其他部分中,我必须从面板获取图像,但我希望返回一份副本,以避免从其他地方修改图像
我如何解决这个问题
代码:
编辑
我谈到克隆图像是因为这是我的目标,但有了这个问题,我想了解为什么图像之间的rgba值不同
我已经尝试使用
BufferedImage.TYPE\u INT\u ARGB\u PRE
,但没有帮助。要创建图像的精确副本(鉴于它们都是同一类型),您可以稍微更改代码:
BufferedImage copy = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
copy.setData(image.getRaster()); // getRaster() is faster than getData(), as no copy is created
for (int x = 0; x < colors.length; x++) {
Color color = new Color(copy.getRGB(x, 0), true);
System.out.println(color.getRed() + "," + color.getGreen() + "," + color.getBlue() + "," + color.getAlpha());
}
原因是在完全透明像素上合成半透明像素时,目标处的原始透明像素将有助于最终结果,从而改变RGBA值
200,237,182,14
254,66,188,214
247,104,197,198
158,93,79,239
235,45,57,194
155,76,126,150
165,237,19,172
184,105,97,191
186,249,135,85
234,117,96,24
BufferedImage copy = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
copy.setData(image.getRaster()); // getRaster() is faster than getData(), as no copy is created
for (int x = 0; x < colors.length; x++) {
Color color = new Color(copy.getRGB(x, 0), true);
System.out.println(color.getRed() + "," + color.getGreen() + "," + color.getBlue() + "," + color.getAlpha());
}
BufferedImage copy = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = copy.createGraphics();
g2d.setComposite(AlphaComposite.Src); // Completely replace, default is SrcOver
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
for (int x = 0; x < colors.length; x++) {
Color color = new Color(copy.getRGB(x, 0), true);
System.out.println(color.getRed() + "," + color.getGreen() + "," + color.getBlue() + "," + color.getAlpha());
}