Editing JPEG Exif and thumbnails
I'm trying to update a jpeg with GPS Exif data. So far, with help from posts here, I can successfully copy the Exif data complete with the new GPS information over to a new copy of the image. I read on this list that performing the edit in place wouldn't be possible and anyway I don't really want to go editing the metadata on the original so this is fine, in spite of the loss of quality to the image.
To do this I needed to use the Java SE Jpeg reader/writer because the imageio reader and writer (CLibJPEGImageReader/Writer) doesn't seem to allow editing the metadata (as far as I can see CLibJPEGMetadata has isReadOnly always set to true). Since I use the metadata from the original image as the metadata for the copy I need to be able to edit it (e.g. with setFromTree or mergeTree). I don't see any other way to obtain an editable copy. Any suggestions would be greatly appreciated.
The main problem I had in doing this was with the imageio TIFF reader and writer that I used for actually editing the Exif and building the new App1 node. I would get a class cast exception if I used a TIFFDirectory directly to store the GPS IFD. It seems that internally it assumes the type of the IFD will be TIFFIFD which is a sub-class of TiffDirectory (see line 461 in TIFFIFD - TIFFIFD subIFD = (TIFFIFD)f.getData();). TIFFIFD isn't documented in the standard API so I would take this to be a bug. At the least in the documentation because it isn't possible to work out how to create an IFD using that alone, but also presumably in the code too because it doesn't seem like the TIFFIFD class is intended to be exposed in the API.
The problem I have now is how to copy over the thumbnail data. For the three camera makes I've tried two store the thumbnail in the Exif on the App1 segment (the other has the error "ICC APP2 encountered without prior JFIF!" which is a different problem). The SE Jpeg reader doesn't find these because it only looks at the JFIF one. The imageio one does but then I can't then edit the metadata with this. I'd rather not read the image twice with both readers.
I am able to find the thumbnail using the TIFF reader when reading the Exif, it's the next image (compressed as a JPEG or OLD JPEG). Ive tried both giving this BufferedImage to the Tiff writer as a thumbnail when it's writing the new App1 node and to the Jpeg writer as a thumbnail when it's writing the new image but in both cases the final image doesn't have a thumbnail. Ideally I'd like the thumbnail to be stored in exactly the same place in the copy as the original (i.e. the Exif meta data) . Does anyone know how I might achieve this? I don't claim to be an accomplished programmer so I'm probably missing something obvious. Since it might be important I'm using a tool I'm using a tool ExifReader to check the meta data and thumbnail is there.