Search code examples
javapngjpeggifjavax.imageio

Reading jpeg or gif metadata with javax.imageio


I want to modify the metadata of some types of images (png, jpeg or gif) and I found a a code that works very well for PNG images on this topic, provided by haraldK. When I try to run it on a jpg image though, it throws this error : javax.imageio.IIOException: JFIF APP0 must be first marker after SOI. The error is thrown when arriving on the line IIOImage image = reader.readAll(0, null);

What can I do to get this working ?

Thanks in advance for your answer.


Solution

  • The problem you face is the JPEG standard did not define a file format. Several file formats appeared. E.g. JFIF. EXIF. SPIFF. These formats represent metadata in different ways. Apparently the library you are trying to use only supports the JFIF file format. Apparently your library only supports the JFIF format while you have a file in a different format (likely EXIF).

    So you need a library that supports your file format or you need to modify the library you have to work with whatever file format you have. That could be a fairly substantial change.