11

我正在读取 BufferedImage 中的像素颜色,如下所示:

.....
InputStream is = new BufferedInputStream(conn.getInputStream());
BufferedImage image = ImageIO.read(is);

int color = image.getRGB(x, y);

int  red = (colour & 0x00ff0000) >> 16;
int  green = (colour & 0x0000ff00) >> 8;
int  blue = colour & 0x000000ff;

现在这工作正常,除了具有透明度的png。我发现如果 x,y 指的是没有颜色的透明像素,我仍然会读取一种颜色,通常与图像中其他地方使用的颜色相同。

如何检测像素实际上是透明的而不是彩色的?

谢谢

4

1 回答 1

17
int alpha = (colour>>24) & 0xff;

结果也是一个范围从 0(完全透明)到 255(完全不透明)的值。

于 2009-10-21T10:38:56.857 回答