Question

I was using ImageIO.read() to get some JPEG files from disk, but it is so slow... It takes 250ms to read each file, while Toolkit.getDefaultToolkit().getImage() takes less than 10ms (actually it is showing 0ms in my tests)!

Now I ask myself: why is ImageIO.read() so slow if Toolkit.getDefaultToolkit().getImage() is so fast?


This is super fast, but does not return a BufferedImage:

Image image = Toolkit.getDefaultToolkit().getImage(f.getAbsolutePath());

This is slow (~250ms to read a 2.3MB 3264x1840 JPEG file):

BufferedImage image = ImageIO.read(f);
Was it helpful?

Solution

Generally it's not.

ImageIO reads the image within the same thread context it was called from, Toolkit.getDefaultToolkit().getImage() uses a separate background Thread to load the image in and returns (almost) immediately.

This is why you have MediaTracker#waitForAll and MediaTracker#waitForID and ImageObserver. This all monitor the state of a image loading process which was created by Toolkit.getDefaultToolkit().getImage() so they known when the image loading has completed...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top