如何快速获取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++);
}
}
System.out.println()
调用将使其速度显著提高。你可能可以在循环外定义c
,甚至避免实例化Color
,但这可能并不必要。 - beerbajay