IndexOutOfBoundsException in ImageReader.getImageMetadata(0)
See original GitHub issueAs you know from my ongoing saga in #586 I want to scale an image, modify metadata, and write the image to a file using Java Image I/O.
For the time being for certain images I have updated the code to maintain the existing metadata. I just need to do this:
IIOMetadata imageMetadata = imageReader.getImageMetadata(0);
IIOImage iioImage = new IIOImage(newImage, null, imageMetadata);
Unbelievably it works well, and Java Image I/O seems to keep all the Exif, IPTC and XMP metadata!
Note that I am assuming that the image I am processing is the first image, image 0
. (This might not work on a JPEG image with no image metadata, but truly that is the last of my worries at the moment.)
Because of this approach I don’t even need to use Twelve Monkeys, so had taken it out altogether. I wanted to check one last thing, so I added back Twelve Monkeys 3.6.2 without changing any of my code, and I get this:
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248)
at java.base/java.util.Objects.checkIndex(Objects.java:372)
at java.base/java.util.ArrayList.get(ArrayList.java:458)
at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader.readSegments(JPEGImageReader.java:842)
at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader.initHeader(JPEGImageReader.java:693)
at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader.initHeader(JPEGImageReader.java:727)
at com.twelvemonkeys.imageio.plugins.jpeg.JPEGImageReader.getImageMetadata(JPEGImageReader.java:1229)
I have no idea why Twelve Monkeys thinks that there are no images when calling ImageReader.getImageMetadata(0)
. It was working earlier when I was ignoring metadata altogether (and adding the last true
argument in ImageReader.setInput(imageInputStream, true, true)
).
As I mentioned this works fine with pure Java Image I/O; it’s only when I add Twelve Monkeys as a dependency that this breaks.
Because I wound up not needing Twelve Monkeys at the moment (and I have to deal with breaking bugs in Apache Commons Imaging such as IMAGING-281; and in metadata-extractor such as https://github.com/drewnoakes/metadata-extractor/issues/270 ) unfortunately I don’t time to investigate this further. Sorry. I hope you understand.
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (6 by maintainers)
Top GitHub Comments
I have excellent news!
Indeed, that was the problem! Thank you so much! Even though it wasn’t causing me any problems at the moment (as I wasn’t using Twelve Monkeys), it’s very good to have the code more robust and to remove a blatant error.
Here is what the code looks like now. I’ve restructured the code a bit to prevent this sort of problem the future:
I now owe you several! Seriously, after the pandemic if I’m in the area I might just buy you one personally.
I’ll mark this ticket as closed. Feel free to add an “invalid” label or whatever to the bug report.
@haraldk I sent you the original image via email. I’d rather not post it on GitHub. (It’s nothing secret, just a large, full-size version of one of my photography works. I prefer to distribute only the scaled versions, which is part of why I’m going to all the trouble to scale them to begin with.)
Yesterday I generated an entire set of photos with a milestone version of my static site generation with image scaling project. It worked just fine. To reproduce this problem, I merely added the following as a dependency as per your project instructions:
Boom, it broke:
This is with commit
5525b5acc7aecfd1bba32824f1279cf28d0319dc
as indicated above.