Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IMAGING-319: Correct length calculation in EXIF rewriter #203

Closed
wants to merge 1 commit into from

Conversation

gwlucastrig
Copy link
Contributor

I added comments to the JIRA ticket on this item at IMAGING-319

Unfortunately, it will be hard for me to create a JUnit test to specifically test this change without actually adding the user's original file (which is large) to our code distribution.

The following text shows the problem.
The users' test program read the data and then re-wrote it using the Commons Imaging library. In the code below, his original File variable is named "source" and the rewritten version is named "result".

ImageMetadata metadata2 = Imaging.getMetadata(source);  // or result
JpegImageMetadata jpegMetadata2 = (JpegImageMetadata) metadata2;
TiffImageMetadata exif2 = jpegMetadata2.getExif();
String s2 = exif2.toString();
System.out.println("\n" + s2 + "\n");

The printout for the original EXIF block (with many elements removed) comes up as:
Exif:
ExifVersion: 48, 50, 51, 50
DateTimeOriginal: '2021:10:28 13:47:07'
DateTimeDigitized: '2021:10:28 13:47:07'
Unknown Tag (0x9010): '-05:00'
Unknown Tag (0x9011): '-05:00'
Unknown Tag (0x9012): '-05:00'

Unknown tag 0x9010 is fine. But if you perform the same thing on this result, it comes up with the first character in Tag 0x9010 as a comma rather than a negative sign as shown below:

Exif: 
    Unknown Tag (0x9010): , 05:00
    Unknown Tag (0x9011): '-05:00'
    Unknown Tag (0x9012): '-05:00'

The attached fix repairs the problem and causes the EXIF tag to be printed correctly.

Here's the full code for the test program:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.ImageWriteException;
import org.apache.commons.imaging.Imaging;
import org.apache.commons.imaging.common.ImageMetadata;
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
import org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter;
import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;

 
public class CommonsIssue319 {

  public static void main(String[] args) throws ImageReadException, IOException, ImageWriteException {
    File source = new File("iPhone12-geotag.JPG");
    File result = new File("editted-iPhone12-geotag.JPG");
    final ImageMetadata metadata = Imaging.getMetadata(source);
    final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
    final TiffImageMetadata exif = jpegMetadata.getExif();
    String s = exif.toString();
    System.out.println("\n" + s + "\n");
    TiffOutputSet outputSet = exif.getOutputSet();
    BufferedOutputStream bufferedOutputStream = new 
    BufferedOutputStream(new FileOutputStream(result));
    new ExifRewriter().updateExifMetadataLossless(source, 
    bufferedOutputStream, outputSet);
    bufferedOutputStream.flush();
    bufferedOutputStream.close();

    ImageMetadata metadata2 = Imaging.getMetadata(result);
    JpegImageMetadata jpegMetadata2 = (JpegImageMetadata) metadata2;
    TiffImageMetadata exif2 = jpegMetadata2.getExif();
    String s2 = exif2.toString();
    System.out.println("\n" + s2 + "\n");
  }

@kinow
Copy link
Member

kinow commented Mar 5, 2022

That was quite the detective work @gwlucastrig 👏 The change is really small, but I couldn't understand just reading the code. Going to have to re-read the JIRA and your description here 🤓 but it's looking good! Thanks Gary!

@StefanOltmann
Copy link
Contributor

The PR was closed, but fixes a still present bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants