Reputation: 2662
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!
Upvotes: 7
Views: 4524
Reputation: 117569
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);
}
}
Upvotes: 7
Reputation: 4378
public boolean isAlpha(BufferedImage image, int x, int y) {
Color pixel = new Color(image.getRGB(x, y), true);
return pixel.getAlpha() > 0; //or "== 255" if you prefer
}
Upvotes: 1
Reputation: 51445
Try this:
Raster raster = bufferedImage.getAlphaRaster();
if (raster != null) {
int[] alphaPixel = new int[raster.getNumBands()];
for (int x = 0; x < raster.getWidth(); x++) {
for (int y = 0; y < raster.getHeight(); y++) {
raster.getPixel(x, y, alphaPixel);
alphaArray[x][y] = alphaPixel[0] == 0x00;
}
}
}
Upvotes: 2