如何在具有透明度的 java BufferedImage 中读取像素颜色 [英] How to read pixel color in a java BufferedImage with transparency
本文介绍了如何在具有透明度的 java BufferedImage 中读取像素颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在按如下方式读取 BufferedImage 中的像素颜色:
I am reading pixel color in a BufferedImage as follows:
.....
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 指的是一个没有颜色的透明像素,我仍然会读取一种颜色,通常与图像中其他地方使用的颜色相同.
Now this works fine except for png's with transparency. I find that if x,y refer to a transparent pixel with no color, i still read a color, generally the same color as used elsewhere in the image.
如何检测像素实际上是透明的而不是彩色的?
How do I detect that the pixel is actually transparent and not colored?
谢谢
推荐答案
int alpha = (colour>>24) & 0xff;
结果也是一个从 0(完全透明)到 255(完全不透明)的值.
The result is also a value ranging from 0 (completely transparent) to 255 (completely opaque).
这篇关于如何在具有透明度的 java BufferedImage 中读取像素颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文