获取 BufferedImage
每个像素的 RGB 值的最快方法是什么?
现在我正在使用两个 for
循环获取 RGB 值,如下面的代码所示,但是获取这些值的时间太长,因为嵌套循环总共运行了 479999 次图片。如果我使用 16 位图像,这个数字会更高!
我需要一种更快的方法来获取像素值。
这是我目前正在尝试使用的代码:
BufferedImage bi=ImageIO.read(new File("C:\\images\\Sunset.jpg"));
int countloop=0;
for (int x = 0; x <bi.getWidth(); x++) {
for (int y = 0; y < bi.getHeight(); y++) {
Color c = new Color(bi.getRGB(x, y));
System.out.println("red=="+c.getRed()+" green=="+c.getGreen()+" blue=="+c.getBlue()+" countloop="+countloop++);
}
}
最佳答案
我不知道这是否有帮助,我还没有测试过,但你可以通过这种方式获得 rgb 值:
BufferedImage bi=ImageIO.read(new File("C:\\images\\Sunset.jpg"));
int[] pixel;
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
pixel = bi.getRaster().getPixel(x, y, new int[3]);
System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (bi.getWidth() * y + x));
}
}
如您所见,您不必在循环内初始化新的 Color。我还按照 onemasse 的建议反转了宽度/高度循环,以从我已有的数据中检索计数器。
关于java - 需要更快的方法来获取缓冲图像的每个像素的 RGB 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10088465/