javagraphicsalphabufferedimage

How can i find out where a BufferedImage has Alpha in Java?


I've got a BuferredImage and a boolean[][] array. I want to set the array to true where the image is completely transparant.

Something like:

for(int x = 0; x < width; x++) {
    for(int y = 0; y < height; y++) {
        alphaArray[x][y] = bufferedImage.getAlpha(x, y) == 0;
    }
}

But the getAlpha(x, y) method does not exist, and I did not find anything else I can use. There is a getRGB(x, y) method, but I'm not sure if it contains the alpha value or how to extract it.

Can anyone help me? Thank you!


Solution

  • public static boolean isAlpha(BufferedImage image, int x, int y)
    {
        return image.getRGB(x, y) & 0xFF000000 == 0;
    }
    
    for(int x = 0; x < width; x++)
    {
        for(int y = 0; y < height; y++)
        {
            alphaArray[x][y] = isAlpha(bufferedImage, x, y);
        }
    }