Return-Path: Delivered-To: apmail-commons-user-archive@www.apache.org Received: (qmail 22557 invoked from network); 28 Jan 2010 16:07:20 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 28 Jan 2010 16:07:20 -0000 Received: (qmail 2728 invoked by uid 500); 28 Jan 2010 16:07:19 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 2616 invoked by uid 500); 28 Jan 2010 16:07:18 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 2606 invoked by uid 99); 28 Jan 2010 16:07:18 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Jan 2010 16:07:18 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of lichtemo@googlemail.com designates 209.85.220.217 as permitted sender) Received: from [209.85.220.217] (HELO mail-fx0-f217.google.com) (209.85.220.217) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Jan 2010 16:07:08 +0000 Received: by fxm9 with SMTP id 9so876188fxm.10 for ; Thu, 28 Jan 2010 08:06:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:content-type; bh=cjLUqmipJHLGIMnf8cxJeIWdrAHpDCvJs9yRf2xgAhU=; b=gZKsWbh4UrkBhGmUlkECkHE5vxcxCIkTzKetj8b8OBRECWa2ZKmhDyN8qm4RbJKs6N Re2A5KQPQ3k8tEwDyoIF8wmHM2Ks5DTA3MlFpqoFJvPDhwAzPSapMP9TLwTyTylU0AWF tnvyd3ZYWZNawnULZhAxTca4CSCIREKvgUyKw= DomainKey-Signature: a=rsa-sha1; c=nofws; d=googlemail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type; b=KYG8PP2VlHPL0oq48njsqlOfNPAU1Buzph10ILsQPWgg6GDygnBLVY612BUDLflRKG Rtw+0lQI1oSLgraSYMszELLvms8zz7rlmW3ufhtcO6E5zCiVd+97zFam7g1NNX7t82Lq JfBEqmsofkBG9gA2XaUd+RhIaQzUOPBJZC0vQ= Received: by 10.223.77.75 with SMTP id f11mr1841531fak.65.1264694807639; Thu, 28 Jan 2010 08:06:47 -0800 (PST) Received: from ?192.168.178.27? (dslb-094-218-001-020.pools.arcor-ip.net [94.218.1.20]) by mx.google.com with ESMTPS id 9sm1665495fks.22.2010.01.28.08.06.45 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 28 Jan 2010 08:06:46 -0800 (PST) Message-ID: <4B61B65D.8030004@googlemail.com> Date: Thu, 28 Jan 2010 17:07:57 +0100 From: Moritz Lichter User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.1.7) Gecko/20100111 Lightning/1.0b1 Thunderbird/3.0.1 MIME-Version: 1.0 To: user@commons.apache.org Subject: [SANSELAN] Write EXIF Content-Type: multipart/mixed; boundary="------------090504070804010904020203" X-Virus-Checked: Checked by ClamAV on apache.org --------------090504070804010904020203 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------090504070804010904020203 Content-Type: text/plain; name="Test2.java" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="Test2.java" 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")); } } --------------090504070804010904020203 Content-Type: text/plain; name="output.txt" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="output.txt" 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 --------------090504070804010904020203 Content-Type: text/plain; charset=us-ascii --------------------------------------------------------------------- To unsubscribe, e-mail: user-unsubscribe@commons.apache.org For additional commands, e-mail: user-help@commons.apache.org --------------090504070804010904020203--