Java BufferedImage getRGB方法和Color类 [英] Java BufferedImage getRGB method and Color class
问题描述
getRGB(x,y)方法中的x和y参数代表什么?数值返回值代表什么?
What do the x and y parameters in the getRGB(x, y) method represent? What does the numerical return value represent?
如何识别显示在DrawingPanel上的BufferedImage中的特定像素的颜色?理想情况下,我想在图像中找到一个单词,并在该单词下划线或勾勒出一个相关的超链接。如何将这些数值转换成人类可以理解的颜色,如'red''green'或'blue'?
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'?
是的,我知道在Color类中, 0-255代表透明到不透明。但这是什么意味着实际的颜色?我很高兴澄清,如果这没有意义。感谢您阅读。
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.
推荐答案
x和y坐标代表像素位置<从左到右,y从上到下(都从0开始)。所以(0,0)是左上角的像素,(width-1,height-1)是右下角的像素。
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.
返回的整数值有R ,G,B和(潜在)Alpha值打包成单个int。记住,一个int包含32位,所以其中8位分配给每个值。
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.
但是,如果你不好,一个整数,最简单的做法是将整数值传递给Color 的构造函数:
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.
这篇关于Java BufferedImage getRGB方法和Color类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!