Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java BufferedImage how to know if a pixel is transparent

I'm going to use the getRGB method of BufferedImage. I want to check the pixels of an image and see which of them have transparency (in general the pixels I will have that are transparent will be totaly transparent). How can I get it from the int that getRGB returns?

like image 779
Panos Avatar asked Jan 23 '12 20:01

Panos


People also ask

What is a transparent pixel?

Transparent pixelsThis image has binary transparency (some pixels fully transparent, other pixels fully opaque). It can be transparent against any background because it is monochrome.

How do I change the pixel color in Java?

Create a Color object bypassing the new RGB values as parameters. Get the pixel value from the color object using the getRGB() method of the Color class. Set the new pixel value to the image by passing the x and y positions along with the new pixel value to the setRGB() method.

What is pixel in Java?

java.lang.Object Pixel public class Pixel extends java.lang.Object. Class that references a pixel in a picture. A pixel has an x and y location in a picture. A pixel knows how to get and set the red, green, blue, and alpha values in the picture. A pixel also knows how to get and set the color using a Color object.


1 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 has to be in the correct format TYPE_4BYTE_ABGR or some format that supports alpha channels else if will always be opaque (ie 0xff).

like image 107
chubbsondubs Avatar answered Nov 15 '22 21:11

chubbsondubs