简体   繁体   English

Java BufferedImage没有注册透明像素?

[英]Java BufferedImage Not Registering Transparent Pixels?

I have used the method ImageIO.read(File file); 我已经使用方法ImageIO.read(File file); to read a PNG image file. 读取PNG图像文件。 However, when I use the getRGB(int x, int y) method on it to extract the alpha it always returns 255 whether the pixel is transparent or not. 但是,当我在其上使用getRGB(int x, int y)方法提取Alpha时,无论像素是否透明,它总是返回255。 How do I remedy this inconvenience? 我该如何解决这种不便?

When converting packed int colors to Color objects, you need to tell it if it should calculate the alpha value or not. 将打包的int颜色转换为Color对象时,需要告诉它是否应该计算alpha值。

new Color(image.getRGB(x, y), true).getAlpha();

See Color(int, boolean) for more details 有关更多详细信息Color(int, boolean)请参见Color(int, boolean)

Just wanted to point out that using the method getRGB(x,y) is extremely inefficient. 只是想指出,使用方法getRGB(x,y)效率极低。 If you want to get the pixels of an image you could extract the colours from each individual pixel and then store the pixel in an int array. 如果要获取图像的像素,可以从每个像素提取颜色,然后将像素存储在一个int数组中。 Credit also to mota for explaining why this is inefficient see his post . 也要感谢mota解释为什么这效率低下, 请参阅他的文章 Example below: 下面的例子:

/**===============================================================================================
     * Method that extracts pixel data from an image 
     * @return a 2d array representing the pixels of the image
    =================================================================================================*/
    public static int[][] getImageData(BufferedImage img) {
            int height = img.getHeight();
            int width = img.getWidth();
            final byte[] imgPixels = ((DataBufferByte) img.getRaster().getDataBuffer()).getData();
            final boolean is_Alpha_Present = img.getAlphaRaster() != null;
            int[][] imgArr = new int[height][width];

            if (is_Alpha_Present) {
                    final int pixelLength = 4; //number of bytes used to represent a pixel if alpha value present
                    for (int pixel = 0, row = 0, col = 0; pixel < imgPixels.length; pixel = pixel + pixelLength) {
                            int argb = 0;
                            argb += (((int) imgPixels[pixel] & 0xff) << 24); //getting the alpha for the pixel
                            argb += ((int) imgPixels[pixel + 1] & 0xff); //getting the blue colour
                            argb += (((int) imgPixels[pixel + 2] & 0xff) << 8); //getting the green colour
                            argb += (((int) imgPixels[pixel + 3] & 0xff) << 16); //getting the red colour
                            imgArr[row][col] = argb;
                            col++;
                            if (col == width) {
                                    col = 0;
                                    row++;
                            }
                    }
            }
            else {
                    final int pixelLength = 3;
                    for (int pixel = 0, row = 0, col = 0; pixel < imgPixels.length; pixel = pixel + pixelLength) {
                            int argb = 0;
                            argb += Integer.MIN_VALUE;
                            argb += ((int) imgPixels[pixel] & 0xff); //getting the blue colour
                            argb += (((int) imgPixels[pixel+1] & 0xff) << 8); //getting the green colour
                            argb += (((int) imgPixels[pixel+2] & 0xff) << 16); //getting the red colour
                            imgArr[row][col] = argb;
                            col++;
                            if (col == width) {
                                    col = 0;
                                    row++;
                            }       
                    }
            }
            return imgArr;
    }

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

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