如何获取图像文件并将其转换为栅格,然后逐像素访问其数据(RBG 值)?
问问题
255 次
5 回答
2
BufferedImage img = ImageIO.read(new File("lol"));
int rgb = img.getRGB(x, y);
Color c = new Color(rgb);
现在您可以使用 Color.getRed()、getGreen()、getBlue() 和 getAlpha() 来获取不同的值
于 2011-10-24T19:07:17.183 回答
2
BufferedImage image = ImageIO.read(new File(myFilename));
int pixel = image.getRGB(0, 0); // Top left pixel.
// Access the color components, valued 0-255.
int alpha = (pixel >>> 24) & 0xff; // If applicable to image format.
int r = (pixel >>> 16) & 0xff;
int g = (pixel >>> 8) & 0xff;
int b = pixel & 0xff;
[编辑]请注意,@Sibbo 的答案是正确的,并且可以方便地使用Color
类颜色访问器方法;但是,正如我所展示的那样,直接通过位操作提取颜色可能会快得多,因为它避免了重复构造函数调用的开销。
于 2011-10-24T19:13:46.563 回答
1
用于ImageIO.read
以 . 形式读取图像文件BufferedImage
,然后使用其中一种getData
方法获取图像的Raster
. 在其中,您将找到获取像素数据的方法。
于 2011-10-24T19:08:37.800 回答
1
完成将图像转换为光栅后,不要使用 rgb 值,使用 rasters.getData
方法
于 2011-11-11T18:09:30.067 回答
1
用这个:
Image img.getRGB(x, y);
Color c = new Color(rgb);
于 2011-12-12T20:06:42.793 回答