How to read pixel color in a java BufferedImage with transparency

蓝咒 提交于 2019-11-27 14:56:37

问题


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;

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?

Thanks


回答1:


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

The result is also a value ranging from 0 (completely transparent) to 255 (completely opaque).



来源:https://stackoverflow.com/questions/1599963/how-to-read-pixel-color-in-a-java-bufferedimage-with-transparency

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!