What is the fastest way to get the RGB value of each pixel of a BufferedImage
?
Right now I am getting the RGB values using two for
loops as shown in the code below, but it took too long to get those values as the nested loop runs a total of 479999 times for my image. If I use a 16-bit image this number would be even higher!
I need a faster way to get the pixel values.
Here is the code I am currently trying to work with:
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++);
}
}
it can even rise when i will use 16 bit image
- Why would the number of iterations depend on the bits per pixel? And what's the use case for that? Note that creatingColor
objects as well as printing to the console takes a while. If you want to access all 479999 pixels you can't get rid of a loop (you could merge them to one but that shouldn't make that big a difference). – UnsnarlSystem.out.println()
calls will make it substantially faster. You could possibly definec
outside of the loop, and even avoid instantiating aColor
if you needed to, but it's probably not necessary. – Sweet