如何获取图像文件并将其转换为栅格然后访问其数据?

时间:2011-10-24 19:05:27

标签: java image image-processing raster

如何拍摄图像文件并将其转换为栅格,然后逐个像素地访问其数据(RBG值)?

5 个答案:

答案 0 :(得分:2)

BufferedImage img = ImageIO.read(new File("lol"));
int rgb = img.getRGB(x, y);

Color c = new Color(rgb);

现在你可以使用Color.getRed(),getGreen(),getBlue()和getAlpha()来获取不同的值

答案 1 :(得分: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 class color accessor methods;但是,正如我所演示的那样,通过位操作直接提取颜色可能会快得多,因为它避免了重复构造函数调用的开销。

答案 2 :(得分:1)

使用ImageIO.readBufferedImage的形式阅读图片文件,然后使用其中一种getData方法获取图片的Raster。在其中,您将找到获取像素数据的方法。

答案 3 :(得分:1)

使用栅格.getData方法完成将图像转换为栅格后,请勿使用rgb值

答案 4 :(得分:1)

使用此:

Image img.getRGB(x, y);

Color c = new Color(rgb);