Uploaded image for project: 'Commons Imaging'
  1. Commons Imaging
  2. IMAGING-137

Can not set EXIF_TAG_EXIF_VERSION tag

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.0-alpha1
    • 1.0-alpha1
    • Format: TIFF
    • java 1.7.0_55 Commons-Imaging (build date is 3/25/2014)

    Description

      I am unable to set EXIF_TAG_EXIF_VERSION.

      I am using Maven with the following settings (the build date is 3/25/2014):
      <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-imaging</artifactId>
      <version>1.0-SNAPSHOT</version>
      <scope>compile</scope>
      </dependency>

      <repository>
      <id>apache.snapshots</id>
      <url>http://repository.apache.org/snapshots/</url>
      </repository>

      I get exceptions because it expects only 1 byte.

      If I debug and inspect the metadata for a JPG file with proper Exif data, it shows 4 bytes (48,50,50,48). This is consistent with this reference

      Here is my code. To run, change the file names and run.

      import java.io.BufferedOutputStream;
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.OutputStream;
      import java.util.List;
      import org.apache.commons.imaging.ImageReadException;
      import org.apache.commons.imaging.ImageWriteException;
      import org.apache.commons.imaging.Imaging;
      import org.apache.commons.imaging.common.IImageMetadata;
      import org.apache.commons.imaging.common.IImageMetadata.IImageMetadataItem;
      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.constants.ExifTagConstants;
      import org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory;
      import org.apache.commons.imaging.formats.tiff.write.TiffOutputSet;

      public class ExifExample
      {
      public static void logImageMetaData(final File jpgImageFile) throws ImageReadException, IOException
      {
      // Get all metadata stored in Exif format (i.e., from JPEG or TIFF).
      final IImageMetadata metadata = Imaging.getMetadata(jpgImageFile);

      if (metadata instanceof JpegImageMetadata)
      {
      final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

      final List<IImageMetadataItem> items = jpegMetadata.getItems();
      for (int i = 0; i < items.size(); i++)

      { final IImageMetadataItem item = items.get(i); System.out.println(" " + "item: " + item); }

      }
      }

      private static TiffOutputSet getMetadata(final File jpegImageFile) throws ImageReadException, IOException, ImageWriteException
      {
      TiffOutputSet outputSet = null;

      // metadata might be null if no metadata is found.
      final IImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
      final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
      if (null != jpegMetadata)
      {
      // Exif might be null if no Exif metadata is found.
      final TiffImageMetadata exif = jpegMetadata.getExif();

      if (null != exif)

      { /* TiffImageMetadata class is immutable (read-only). * TiffOutputSet class represents the Exif data to write. * * Usually, we want to update existing Exif metadata by changing * the values of a few fields, or adding a field. * * In these cases, it is easiest to use getOutputSet() to * start with a "copy" of the fields read from the image. */ outputSet = exif.getOutputSet(); }

      }
      return outputSet;
      }

      public static void writeExifData(File jpegImageFile, File destImageFile) throws IOException, ImageReadException, ImageWriteException
      {
      try (OutputStream os = new BufferedOutputStream(new FileOutputStream(destImageFile)))

      { TiffOutputSet outputSet = new TiffOutputSet(); // Create the Exif directory tags final TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory(); /* * TODO: How do you set ExifVersion * * Exception in thread "main" org.apache.commons.imaging.ImageWriteException: Tag expects 1 value(s), not 4 at org.apache.commons.imaging.formats.tiff.write.TiffOutputDirectory.add(TiffOutputDirectory.java:91) at com.ttinc.util.exif.ExifExample.writeExifData(ExifExample.java:81) at com.ttinc.util.exif.ExifExample.main(ExifExample.java:91) */ exifDirectory.add(ExifTagConstants.EXIF_TAG_EXIF_VERSION, "0221".getBytes()); new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet); }

      }

      public static void main(String args[]) throws Exception

      { File destinationImageFile = new File("C:\\temp\\DestImage.jpg"); writeExifData(new File("C:\\temp\\SourceImage.jpg"), destinationImageFile); logImageMetaData(destinationImageFile); }

      }

      Attachments

        Activity

          People

            Unassigned Unassigned
            troccola Ted Troccola
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: