java bufferedimage颜色,Java BufferedImage getRGB方法和Color类

What do the x and y parameters in the getRGB(x, y) method represent? What does the numerical return value represent?

How do I identify the color of specific pixels within a BufferedImage that is displayed on a DrawingPanel? Ideally I'd like to 'find' a word within an image, and underline or outline this word to create a relevant hyperlink. How can I translate these numerical values into human understandable colors like 'red' 'green' or 'blue'?

Yes, I understand that in the Color class an alpha value of 0-255 represents transparent to opaque. But what does this imply for the actual color? I'm happy to clarify if this doesn't make sense. Thank you for reading.

解决方案

The x and y coordinates represent the pixel location, x going from left to right and y going from top to bottom (both starting at 0). So (0, 0) is the upper left pixel, and (width-1, height-1) is the bottom right pixel.

The integer value returned has the R, G, B and (potentially) Alpha values packed into a single int. Remember that an int contains 32 bits, so 8 of those bits are allocated to each of the values.

But, if you're not to good with shifting bits around inside an integer, the easiest thing to do is pass the integer value to the constructor of Color:

int rgba = image.getRGB(coordinates);

Color col = new Color(rgba, true);

int r = col.getRed();

int g = col.getGreen();

...

Obviously, recognising where a word is from raw pixel values is a whole nother, quite complex, problem.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值