Question

I have used the method ImageIO.read(File file); to read a PNG image file. 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. How do I remedy this inconvenience?

Was it helpful?

Solution

When converting packed int colors to Color objects, you need to tell it if it should calculate the alpha value or not.

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

See Color(int, boolean) for more details

OTHER TIPS

Just wanted to point out that using the method getRGB(x,y) is extremely inefficient. 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. Credit also to mota for explaining why this is inefficient see his post . 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;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top