2

I want to read a File to a BufferedImage as I want to resize my image. For the resize operation, I have to get a BufferedImage.

So, I used ImageIO.read(file), but it takes over 10000ms when file's size is over 5MB. This is my code:

...

public BufferedImage change(File file) {
    return ImageIO.read(file);
}

...

I don't understand why it takes so much time. And I want to know how to improve it's performance...

Harald K
  • 26,314
  • 7
  • 65
  • 111
kiseok
  • 29
  • 3
  • These posts may be related to your problem : https://stackoverflow.com/questions/44170127/imageio-read-it-is-very-slow-is-there-a-better-way/44170254 and https://stackoverflow.com/questions/40468494/why-imageio-read-sooo-slow – Junior Dussouillez May 31 '21 at 07:27
  • these solutions were not help for me... @JuniorDussouillez – kiseok May 31 '21 at 08:23
  • 1
    You could try loading into memory first. See https://stackoverflow.com/questions/62852645/efficient-way-to-read-an-image-file-using-java/62862626#62862626 – DuncG May 31 '21 at 08:28
  • Can you share the image (file) that causes this performance? Disabling disk caching may help a bit, but probably the problem is the way the image is encoded. – Harald K May 31 '21 at 11:01
  • 1
    https://unsplash.com/photos/a-QH9MAAVNI Here is link where i downloaded image to test over 5MB @HaraldK – kiseok Jun 01 '21 at 00:49
  • I solved this problem with @DuncG 's solution. I read a FIle to a ImageIcon. The ImageIcon can be changed to Image. As far as I concerned, BufferedImage extends Image. So, I read File to Image. – kiseok Jun 01 '21 at 00:55
  • 2
    ImageIcon uses [`Toolkit.createImage(URL)`](https://docs.oracle.com/javase/8/docs/api/java/awt/Toolkit.html#createImage-java.net.URL-) under the hood. The main difference to ImageIO is that this will load the image asynchronously. You don’t need to hide what’s going on but can use that method directly. – Holger Jun 01 '21 at 08:45
  • 1
    Thanks @kiesok. I finally found some time to test. On my computer, your image is read in ~5.6s, using standard ImageIO. And ~1.7s using my TwelveMonkeys JPEG ImageIO plugin. Sadly, using `Toolkit.getImage()` and properly converting to `BufferedImage` takes 360ms. Room for improvement... – Harald K Jun 03 '21 at 19:56

0 Answers0