Dear Ladies and Gentleman,

I want to write the EXIF field "XPComment" to a JPG file, but I did not
find any working examples so I tried to create my own code, but I did
not succeed.
My program is able to write the "User Comment" field but not the
"XPComment" field. (If you change EXIF_TAG_XPCOMMENT to
EXIF_TAG_USERCOMMENT)
I do not understand what is wrong, so please help me. I attached my code
and the output I got to this mail.


Thank you for your help,

Moritz Lichter
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 {
        
        /**
     * 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());
                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 addXPComment(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 xpCommentPre = outputSet
                    .findField(TiffConstants.EXIF_TAG_XPCOMMENT);
            if (xpCommentPre != null) {
                System.out.println("REMOVE");
                outputSet.removeField(TiffConstants.EXIF_TAG_XPCOMMENT);
            }                    
            // add field 
            try {  
                String fieldData = "Hallo"; 
                  TiffOutputField xpComment = new TiffOutputField(
                                  TiffConstants.EXIF_TAG_XPCOMMENT, 
                          TiffFieldTypeConstants.FIELD_TYPE_BYTE, 
                          fieldData.length(), 
                          fieldData.getBytes());           
                  TiffOutputDirectory exifDirectory = 
outputSet.getOrCreateExifDirectory();
                  exifDirectory.add(xpComment);
                 
                
               
            } 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"));
                addXPComment(new File ("Adler (2).jpg"));
                readMetaData(new File ("Hallo.jpg"));
        }

}
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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to