简体   繁体   English

透明 PNG - BufferedImage 包含不存在的像素 - java

[英]Transparent PNG - BufferedImage contains not existing pixels - java

I have got BufferedImage and method getRGB(...) shows me pixel in position 456, 1959, but I cannot see any color in this position in any image editor.我有 BufferedImage 和方法 getRGB(...) 显示 position 456、1959 中的像素,但我在任何图像编辑器中都看不到 position 中的任何颜色。 I have got two images with transparency.我有两张透明的图像。 ImageOne was created in my java application. ImageOne 是在我的 java 应用程序中创建的。 ImageTwo is ImageOne (ImageOne was open in Affinity Photo and saved as ImageTwo without any change). ImageTwo 是 ImageOne(ImageOne 在 Affinity Photo 中打开并保存为 ImageTwo,没有任何更改)。

If I open ImageOne or ImageTwo in any image editor I can see two same images.如果我在任何图像编辑器中打开 ImageOne 或 ImageTwo,我可以看到两个相同的图像。 Images to download:ImageOne + ImageTwo or HERE or HERE要下载的图像:ImageOne + ImageTwoHEREHERE

But in java application it seems, that images are different.但在 java 应用程序中,图像似乎不同。 This code print RGB color for pixel 456, 1959此代码打印像素 456、1959 的 RGB 颜色

    private static void testImage() {
    try {
        File fOne = new File("d:\\test\\ImageOne.png");
        File fTwo = new File("d:\\test\\ImageTwo.png");
        
        BufferedImage imageOne = ImageIO.read(fOne);
        BufferedImage imageTwo = ImageIO.read(fTwo);
        
        Color cOne = new Color(imageOne.getRGB(456, 1959));
        System.out.println("imageOne = " + cOne.getRGB() + " RGBA = " + cOne.getRed() + "," + cOne.getGreen() + "," + cOne.getBlue() + "," + cOne.getAlpha());
        
        Color cTwo = new Color(imageTwo.getRGB(456, 1959));
        System.out.println("imageTwo = " + cTwo.getRGB() + " RGBA = " + cTwo.getRed() + "," + cTwo.getGreen() + "," + cTwo.getBlue() + "," + cTwo.getAlpha());
        
    } catch (IOException e) {
        e.printStackTrace();
    }
}

The result is:结果是:

imageOne = -8355712 RGBA = 128,128,128,255
imageTwo = -16777216 RGBA = 0,0,0,255

Is it possible to clear BufferedImage from these "invisible" pixels?是否可以从这些“不可见”像素中清除 BufferedImage? Have you any idea why is pixel in ImageOne and why I can see it only in java?您知道为什么 ImageOne 中有像素,为什么我只能在 java 中看到它吗? Why is the pixel "invisible" for image editors?为什么图像编辑器的像素“不可见”? Thank you.谢谢你。

I thought this is totally legit.我认为这是完全合法的。 The alpha channel is 255, which means the ink is fully transparent. Alpha 通道为 255,这意味着墨水是完全透明的。

This assumption is wrong as per https://www.w3.org/TR/PNG-DataRep.html#DR.Alpha-channel : An alpha value of zero represents full transparency, and a value of (2^bitdepth)-1 represents a fully opaque pixel.根据https://www.w3.org/TR/PNG-DataRep.html#DR.Alpha-channel ,这个假设是错误的: alpha 值为零表示完全透明,值 (2^bitdepth)-1表示完全不透明的像素。

Which means I do not have an explanation why in graphics programs the picture should be looking the same - which is the symptom originally described.这意味着我没有解释为什么在图形程序中图片应该看起来一样 - 这是最初描述的症状。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM