Java BufferedImage how to find out if a pixel is transparent

I am going to use the getRGB method for BufferedImage. I want to check the pixels of the image and see which ones have transparency (in general, the pixels that will be transparent will be completely transparent). How can I get it from an int that returns getRGB?

+8
source share
2 answers
BufferedImage img = .... public boolean isTransparent( int x, int y ) { int pixel = img.getRGB(x,y); if( (pixel>>24) == 0x00 ) { return true; } return false; } 

Of course, img must be in the correct TYPE_4BYTE_ABGR format or in some format that supports alpha channels, otherwise it will always be opaque (i.e. 0xff).

+18
source

correct shift to get alpha value in int with β†’> due to sign bit.

Example: int alpha1 = (pixel1 and 0xff000000) β†’> 24;

0
source

Source: https://habr.com/ru/post/906701/


All Articles