Results 1 to 5 of 5
  1. #1
    lichtemo is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Question Writing Exif Metadata to JPEG

    Hello,

    I want to write Metadata to a JPEg image using the Exif 'XPComment' field.
    Because the standart Java libraries does not support writing metadata I tried using the JAI (Java Advanced Imaging) and Sanselan (Commons Sanselan - Commons ) but I was not able to find an example or something else which helps me to create a working code.
    Does anybody know how I can do this?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

  3. #3
    lichtemo is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default

    I know that and I tried to create some code which can write comments. But it fails writing the XPComment. It supports the UserCommet field (if you replace the used Variables).
    My code is:
    Java Code:
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.ArrayList;
     
    import org.apache.sanselan.ImageReadException;
    import org.apache.sanselan.ImageWriteException;
    import org.apache.sanselan.Sanselan;
    import org.apache.sanselan.common.IImageMetadata;
    import org.apache.sanselan.formats.jpeg.JpegImageMetadata;
    import org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter;
    import org.apache.sanselan.formats.tiff.TiffField;
    import org.apache.sanselan.formats.tiff.TiffImageMetadata;
    import org.apache.sanselan.formats.tiff.constants.ExifTagConstants;
    import org.apache.sanselan.formats.tiff.constants.TagInfo;
    import org.apache.sanselan.formats.tiff.constants.TiffConstants;
    import org.apache.sanselan.formats.tiff.constants.TiffFieldTypeConstants;
    import org.apache.sanselan.formats.tiff.write.TiffOutputDirectory;
    import org.apache.sanselan.formats.tiff.write.TiffOutputField;
    import org.apache.sanselan.formats.tiff.write.TiffOutputSet;
     
     
     
    public class Test2 {
        
        private static TiffField t;
        
        /**
         * Read metadata from image file and display it. 
         * @param file
         */
        public static void readMetaData(File file) {
            IImageMetadata metadata = null;
            try {
                metadata = Sanselan.getMetadata(file);
            } catch (ImageReadException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            if (metadata instanceof JpegImageMetadata) {
                JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
                System.out.println("\nFile: " + file.getPath());
                printTagValue(jpegMetadata,
                    TiffConstants.TIFF_TAG_XRESOLUTION);
                printTagValue(jpegMetadata,
                    TiffConstants.TIFF_TAG_DATE_TIME);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_CREATE_DATE);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_ISO);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_APERTURE_VALUE);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_BRIGHTNESS_VALUE);
     
                // simple interface to GPS data
                TiffImageMetadata exifMetadata = jpegMetadata.getExif();
                if (exifMetadata != null) {
                    try {
                        TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
                        if (null != gpsInfo) {
                            double longitude = gpsInfo.getLongitudeAsDegreesEast();
                            double latitude = gpsInfo.getLatitudeAsDegreesNorth();
                            System.out.println("    " +
                                "GPS Description: " + gpsInfo);
                            System.out.println("    " +
                                "GPS Longitude (Degrees East): " +
                                longitude);
                            System.out.println("    " +
                                "GPS Latitude (Degrees North): " +
                                latitude);
                        }
                    } catch (ImageReadException e) {
                        e.printStackTrace();
                    }
                }
     
                System.out.println("EXIF items -");
                ArrayList<?> items = jpegMetadata.getItems();
                for (int i = 0; i < items.size(); i++) {
                    Object item = items.get(i);
                    System.out.println(item.getClass());
                    System.out.println(((TiffImageMetadata.Item)item).getTiffField());
                    t =((TiffImageMetadata.Item)item).getTiffField();
                    System.out.println("    " + "item: " +
                        item);
                }
                System.out.println();
            }
        }
     
        private static void printTagValue(
            JpegImageMetadata jpegMetadata, TagInfo tagInfo) {
            TiffField field = jpegMetadata.findEXIFValue(tagInfo);
            if (field == null) {
                System.out.println(tagInfo.name + ": " +
                    "Not Found.");
            } else {
                System.out.println(tagInfo.name + ": " +
                    field.getValueDescription());
            }
        }
     
        
        /**
         * Example of adding an EXIF item to metadata, in this case using ImageHistory field. 
         * (I have no idea if this is an appropriate use of ImageHistory, or not, just picked
         * a field to update that looked like it wasn't commonly mucked with.)
         * @param file
         */
        public static void addImageHistoryTag(File file) {
            File dst = null;
            IImageMetadata metadata = null;
            JpegImageMetadata jpegMetadata = null;
            TiffImageMetadata exif = null;
            OutputStream os = null;
            TiffOutputSet outputSet = new TiffOutputSet();
     
            // establish metadata
            try {
                metadata = Sanselan.getMetadata(file);
            } catch (ImageReadException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            // establish jpegMedatadata
            if (metadata != null) {
                jpegMetadata = (JpegImageMetadata) metadata;
            }
     
            // establish exif
            if (jpegMetadata != null) {
                exif = jpegMetadata.getExif();
            }
     
            // establish outputSet
            if (exif != null) {
                try {
                    outputSet = exif.getOutputSet();
                } catch (ImageWriteException e) {
                    e.printStackTrace();
                }
            }
     
            if (outputSet != null) {         
                // check if field already EXISTS - if so remove         
                TiffOutputField imageHistoryPre = outputSet
                        .findField(TiffConstants.EXIF_TAG_XPCOMMENT);
                if (imageHistoryPre != null) {
                    System.out.println("REMOVE");
                    outputSet.removeField(TiffConstants.EXIF_TAG_XPCOMMENT);
                }                    
                // add field 
                try {  
                    String fieldData = "Hallo"; 
                      TiffOutputField imageHistory = new TiffOutputField(
                              TiffConstants.EXIF_TAG_XPCOMMENT, 
                              TiffFieldTypeConstants.FIELD_TYPE_BYTE, 
                              fieldData.length(), 
                              fieldData.getBytes());
    //            
                      TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();
                      exifDirectory.add(imageHistory);
                     
                    
                   
                } catch (ImageWriteException e) {
     
                    e.printStackTrace();
                }
            }
            
            try {
                dst = new File("Hallo.jpg");
                os = new FileOutputStream(dst);
                os = new BufferedOutputStream(os);
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            // write/update EXIF metadata to output stream
            try {
                new ExifRewriter().updateExifMetadataLossless(file,
                    os, outputSet);
            } catch (ImageReadException e) {
                e.printStackTrace();
            } catch (ImageWriteException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                    }
                }
            }
            
    //        file.delete();
    //      dst.renameTo(file);        
        }
     
     
        
        public static void main(String[] args) {
            readMetaData(new File ("Adler (2).jpg"));
            addImageHistoryTag(new File ("Adler (2).jpg"));
            readMetaData(new File ("Hallo.jpg"));
        }
     
    }
    I get the following output:
    File: Adler (2).jpg
    XResolution: 1
    Date Time: Not Found.
    Date Time Original: Not Found.
    Create Date: Not Found.
    ISO: Not Found.
    Shutter Speed Value: Not Found.
    Aperture Value: Not Found.
    Brightness Value: Not Found.
    EXIF items -
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    282 (0x11a: XResolution): 1 (1 Rational)
    item: XResolution: 1
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    283 (0x11b: YResolution): 1 (1 Rational)
    item: YResolution: 1
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    296 (0x128: Resolution Unit): 1 (1 Short)
    item: Resolution Unit: 1
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    531 (0x213: YCbCr Positioning): 1 (1 Short)
    item: YCbCr Positioning: 1
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    40092 (0x9c9c: XPComment): 104, 0, 103, 0, 102, 0, 0, 0 (8 Byte)
    item: XPComment: 104, 0, 103, 0, 102, 0, 0, 0

    REMOVE

    File: Hallo.jpg
    XResolution: 1
    Date Time: Not Found.
    Date Time Original: Not Found.
    Create Date: Not Found.
    ISO: Not Found.
    Shutter Speed Value: Not Found.
    Aperture Value: Not Found.
    Brightness Value: Not Found.
    EXIF items -
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    282 (0x11a: XResolution): 1 (1 Rational)
    item: XResolution: 1
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    283 (0x11b: YResolution): 1 (1 Rational)
    item: YResolution: 1
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    296 (0x128: Resolution Unit): 1 (1 Short)
    item: Resolution Unit: 1
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    531 (0x213: YCbCr Positioning): 1 (1 Short)
    item: YCbCr Positioning: 1
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    34665 (0x8769: Exif Offset): 74 (1 Long)
    item: Exif Offset: 74
    class org.apache.sanselan.formats.tiff.TiffImageMetadata $Item
    40092 (0x9c9c: Unknown Tag): 72, 97, 108, 108, 111 (5 Byte)
    item: Unknown Tag (0x9c9c): 72, 97, 108, 108, 111
    You see that the knew image also includes a 0x9c9c tag, but this is why ever "Unknown" and windows also not show it.

  4. #4
    Adidmafioso is offline Member
    Join Date
    Apr 2014
    Posts
    1
    Rep Power
    0

    Default Re: Writing Exif Metadata to JPEG

    hey did you get solution for this.i am stuck in similar situation.
    Thanks in advance.



    Quote Originally Posted by lichtemo View Post
    I know that and I tried to create some code which can write comments. But it fails writing the XPComment. It supports the UserCommet field (if you replace the used Variables).
    My code is:
    Java Code:
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.ArrayList;
     
    import org.apache.sanselan.ImageReadException;
    import org.apache.sanselan.ImageWriteException;
    import org.apache.sanselan.Sanselan;
    import org.apache.sanselan.common.IImageMetadata;
    import org.apache.sanselan.formats.jpeg.JpegImageMetadata;
    import org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter;
    import org.apache.sanselan.formats.tiff.TiffField;
    import org.apache.sanselan.formats.tiff.TiffImageMetadata;
    import org.apache.sanselan.formats.tiff.constants.ExifTagConstants;
    import org.apache.sanselan.formats.tiff.constants.TagInfo;
    import org.apache.sanselan.formats.tiff.constants.TiffConstants;
    import org.apache.sanselan.formats.tiff.constants.TiffFieldTypeConstants;
    import org.apache.sanselan.formats.tiff.write.TiffOutputDirectory;
    import org.apache.sanselan.formats.tiff.write.TiffOutputField;
    import org.apache.sanselan.formats.tiff.write.TiffOutputSet;
     
     
     
    public class Test2 {
        
        private static TiffField t;
        
        /**
         * Read metadata from image file and display it. 
         * @param file
         */
        public static void readMetaData(File file) {
            IImageMetadata metadata = null;
            try {
                metadata = Sanselan.getMetadata(file);
            } catch (ImageReadException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            if (metadata instanceof JpegImageMetadata) {
                JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
                System.out.println("\nFile: " + file.getPath());
                printTagValue(jpegMetadata,
                    TiffConstants.TIFF_TAG_XRESOLUTION);
                printTagValue(jpegMetadata,
                    TiffConstants.TIFF_TAG_DATE_TIME);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_DATE_TIME_ORIGINAL);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_CREATE_DATE);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_ISO);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_SHUTTER_SPEED_VALUE);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_APERTURE_VALUE);
                printTagValue(jpegMetadata,
                    TiffConstants.EXIF_TAG_BRIGHTNESS_VALUE);
     
                // simple interface to GPS data
                TiffImageMetadata exifMetadata = jpegMetadata.getExif();
                if (exifMetadata != null) {
                    try {
                        TiffImageMetadata.GPSInfo gpsInfo = exifMetadata.getGPS();
                        if (null != gpsInfo) {
                            double longitude = gpsInfo.getLongitudeAsDegreesEast();
                            double latitude = gpsInfo.getLatitudeAsDegreesNorth();
                            System.out.println("    " +
                                "GPS Description: " + gpsInfo);
                            System.out.println("    " +
                                "GPS Longitude (Degrees East): " +
                                longitude);
                            System.out.println("    " +
                                "GPS Latitude (Degrees North): " +
                                latitude);
                        }
                    } catch (ImageReadException e) {
                        e.printStackTrace();
                    }
                }
     
                System.out.println("EXIF items -");
                ArrayList<?> items = jpegMetadata.getItems();
                for (int i = 0; i < items.size(); i++) {
                    Object item = items.get(i);
                    System.out.println(item.getClass());
                    System.out.println(((TiffImageMetadata.Item)item).getTiffField());
                    t =((TiffImageMetadata.Item)item).getTiffField();
                    System.out.println("    " + "item: " +
                        item);
                }
                System.out.println();
            }
        }
     
        private static void printTagValue(
            JpegImageMetadata jpegMetadata, TagInfo tagInfo) {
            TiffField field = jpegMetadata.findEXIFValue(tagInfo);
            if (field == null) {
                System.out.println(tagInfo.name + ": " +
                    "Not Found.");
            } else {
                System.out.println(tagInfo.name + ": " +
                    field.getValueDescription());
            }
        }
     
        
        /**
         * Example of adding an EXIF item to metadata, in this case using ImageHistory field. 
         * (I have no idea if this is an appropriate use of ImageHistory, or not, just picked
         * a field to update that looked like it wasn't commonly mucked with.)
         * @param file
         */
        public static void addImageHistoryTag(File file) {
            File dst = null;
            IImageMetadata metadata = null;
            JpegImageMetadata jpegMetadata = null;
            TiffImageMetadata exif = null;
            OutputStream os = null;
            TiffOutputSet outputSet = new TiffOutputSet();
     
            // establish metadata
            try {
                metadata = Sanselan.getMetadata(file);
            } catch (ImageReadException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            // establish jpegMedatadata
            if (metadata != null) {
                jpegMetadata = (JpegImageMetadata) metadata;
            }
     
            // establish exif
            if (jpegMetadata != null) {
                exif = jpegMetadata.getExif();
            }
     
            // establish outputSet
            if (exif != null) {
                try {
                    outputSet = exif.getOutputSet();
                } catch (ImageWriteException e) {
                    e.printStackTrace();
                }
            }
     
            if (outputSet != null) {         
                // check if field already EXISTS - if so remove         
                TiffOutputField imageHistoryPre = outputSet
                        .findField(TiffConstants.EXIF_TAG_XPCOMMENT);
                if (imageHistoryPre != null) {
                    System.out.println("REMOVE");
                    outputSet.removeField(TiffConstants.EXIF_TAG_XPCOMMENT);
                }                    
                // add field 
                try {  
                    String fieldData = "Hallo"; 
                      TiffOutputField imageHistory = new TiffOutputField(
                              TiffConstants.EXIF_TAG_XPCOMMENT, 
                              TiffFieldTypeConstants.FIELD_TYPE_BYTE, 
                              fieldData.length(), 
                              fieldData.getBytes());
    //            
                      TiffOutputDirectory exifDirectory = outputSet.getOrCreateExifDirectory();
                      exifDirectory.add(imageHistory);
                     
                    
                   
                } catch (ImageWriteException e) {
     
                    e.printStackTrace();
                }
            }
            
            try {
                dst = new File("Hallo.jpg");
                os = new FileOutputStream(dst);
                os = new BufferedOutputStream(os);
            } catch (IOException e) {
                e.printStackTrace();
            }
     
            // write/update EXIF metadata to output stream
            try {
                new ExifRewriter().updateExifMetadataLossless(file,
                    os, outputSet);
            } catch (ImageReadException e) {
                e.printStackTrace();
            } catch (ImageWriteException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                    }
                }
            }
            
    //        file.delete();
    //      dst.renameTo(file);        
        }
     
     
        
        public static void main(String[] args) {
            readMetaData(new File ("Adler (2).jpg"));
            addImageHistoryTag(new File ("Adler (2).jpg"));
            readMetaData(new File ("Hallo.jpg"));
        }
     
    }
    I get the following output:

    You see that the knew image also includes a 0x9c9c tag, but this is why ever "Unknown" and windows also not show it.

  5. #5
    lichtemo is offline Member
    Join Date
    Feb 2010
    Posts
    3
    Rep Power
    0

    Default Re: Writing Exif Metadata to JPEG

    Quote Originally Posted by Adidmafioso View Post
    hey did you get solution for this.i am stuck in similar situation.
    Thanks in advance.
    No, I found no solution. I was using this ExifTool by Phil Harvey, calling it is an external process from java.

Similar Threads

  1. This project needs to migrate WPT metadata
    By Arthur Gardner in forum Eclipse
    Replies: 5
    Last Post: 12-11-2009, 11:49 PM
  2. jpg metadata
    By dinosoep in forum Java 2D
    Replies: 1
    Last Post: 11-20-2009, 04:11 PM
  3. Replies: 0
    Last Post: 03-30-2008, 11:06 AM
  4. JDO - defining the persistence using metadata
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-17-2008, 08:50 AM
  5. Media Metadata
    By soulspirit in forum Advanced Java
    Replies: 0
    Last Post: 11-27-2007, 12:01 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •