简体   繁体   English

Java BufferedImage如何知道像素是否透明

[英]Java BufferedImage how to know if a pixel is transparent

I'm going to use the getRGB method of BufferedImage. 我将使用BufferedImage的getRGB方法。 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? 如何从getRGB返回的int中获取它?

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). 当然,img必须采用正确的格式TYPE_4BYTE_ABGR或某种支持alpha通道的格式,否则将始终是不透明的(即0xff)。

the correct shift to get alpha value in an int is with >>> due to sign bit. 由于符号位,在int中获得alpha值的正确移位是>>>。

example: int alpha1 = (pixel1 & 0xff000000) >>> 24; 示例:int alpha1 =(pixel1&0xff000000)>>> 24;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM