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

Java BuffereImage如何知道像素是否透明,java,transparency,bufferedimage,Java,Transparency,Bufferedimage,我将使用BuffereImage的getRGB方法。我想检查一幅图像的像素,看看它们中哪些是透明的(通常我将拥有的透明像素将是完全透明的)。如何从getRGB返回的int中获取它 BufferedImage img = .... public boolean isTransparent( int x, int y ) { int pixel = img.getRGB(x,y); if( (pixel>>24) == 0x00 ) { return true;

我将使用BuffereImage的getRGB方法。我想检查一幅图像的像素,看看它们中哪些是透明的(通常我将拥有的透明像素将是完全透明的)。如何从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;
}

当然,img必须采用正确的格式类型\u 4BYTE\u ABGR或支持alpha通道的某种格式,否则if将始终是不透明的(即0xff)。

由于符号位的原因,获取int中alpha值的正确移位是>>>

例如:
int alpha1=(像素1&0xff000000)>>>24

int transparency=((img.getRGB(x,y)&0xff000000)>>24)第一个字节是alpha值。非常简单易用!非常感谢。