Chin
Chin

Reputation: 20675

Write EXIF data to TIFF file using Apache Commons Imaging

How can I write EXIF data to a TIFF image using Apache Commons Imaging?

This is what I tried:

File img = new File("pic.tif");
File dst = new File("out.tif");
try (FileOutputStream fos = new FileOutputStream(dst);
     OutputStream os = new BufferedOutputStream(fos)) {

    TiffOutputSet outputSet = null;

    final ImageMetadata metadata = Imaging.getMetadata(img);
    final TiffImageMetadata tiffMetadata = (TiffImageMetadata) metadata;
    outputSet = tiffMetadata.getOutputSet();

    if (null == outputSet) {
        outputSet = new TiffOutputSet();
    }

    // New York City
    final double longitude = -74.0;
    final double latitude = 40 + 43 / 60.0;
    outputSet.setGPSInDegrees(longitude, latitude);

    new ExifRewriter().updateExifMetadataLossless(img, os, outputSet);
}

but I got this error:

Exception in thread "main" org.apache.commons.imaging.ImageReadException: Not a Valid JPEG File: doesn't begin with 0xffd8
    at org.apache.commons.imaging.common.BinaryFunctions.readAndVerifyBytes(BinaryFunctions.java:134)
    at org.apache.commons.imaging.formats.jpeg.JpegUtils.traverseJFIF(JpegUtils.java:56)
    at org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter.analyzeJFIF(ExifRewriter.java:186)
    at org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter.updateExifMetadataLossless(ExifRewriter.java:376)
    at org.apache.commons.imaging.formats.jpeg.exif.ExifRewriter.updateExifMetadataLossless(ExifRewriter.java:298)
    at Test.main(Test.java:94)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

which seems to indicate that the ExifRewriter class does not support TIFF? But then which class should I use?

Upvotes: 4

Views: 3127

Answers (2)

jhenya-d
jhenya-d

Reputation: 399

This also may be helpful

            BufferedImage bufferedImage = Imaging.getBufferedImage(out.toByteArray()); 
            File imageFile = new File(outputFileName);
            final Map<String, Object> optionalParams = new HashMap<String, Object>();

            TiffOutputSet tiffExif = new TiffOutputSet();
            tiffExif.addRootDirectory().add(MicrosoftTagConstants.EXIF_TAG_XPTITLE, "Title");
            optionalParams.put("EXIF", tiffExif);
            Imaging.writeImage(bufferedImage, imageFile, ImageFormats.TIFF, optionalParams);

Upvotes: 0

Lake
Lake

Reputation: 4092

ExifRewriter is a tool of the org.apache.commons.imaging.formats.jpeg package, thus it does not work for the TIFF format.

In order to write the EXIF and tags to a TIFF file, you have to read it and then re-write it with the tags created for your OutputSet:

BufferedImage img = Imaging.getBufferedImage(f);
byte[] imageBytes = Imaging.writeImageToBytes(img, ImageFormats.TIFF, new HashMap<>());

File ex = new File(FileUtils.getBaseFileName(f) + "_exif." + FileUtils.getExtension(f));
try(FileOutputStream fos = new FileOutputStream(ex);
    OutputStream os = new BufferedOutputStream(fos)) {
    new TiffImageWriterLossless(imageBytes).write(os, outputSet);
}

You might then want to overwrite the original file with the exif-ed one:

Files.delete(Paths.get(f.toURI()));
Files.move(Paths.get(ex.toURI()), Paths.get(f.toURI()));

Upvotes: 3

Related Questions