commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Matej Jelovcan <zuti...@gmail.com>
Subject [sanselan] Ah, the special characters
Date Fri, 20 Jul 2012 20:56:30 GMT
Hey guys.

Damjan was already answering similar question, I thin (this is the mail
archive I found:
http://www.mail-archive.com/user@commons.apache.org/msg07447.html) but did
no good to me. This is my complete test class that I use to test this
thing...

package si;

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.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.TagInfo;
import org.apache.sanselan.formats.tiff.constants.TiffConstants;
import org.apache.sanselan.formats.tiff.constants.TiffFieldTypeConstants;
import org.apache.sanselan.formats.tiff.write.TiffOutputField;
import org.apache.sanselan.formats.tiff.write.TiffOutputSet;

public class SanselanTestCase
{
private static File TEMPORARY_FILE = null;
 private static String ENCODING = "UTF-8";
 private ExifRewriter exifRewriter = new ExifRewriter();
 public static void main(String[] args)
{
 File file = new File("d:\\image.jpg");
SanselanTestCase testCase = new SanselanTestCase();
 try
{
 String newValue = "Special chars: čšđžć";
testCase.updateMetaDataTags(file, TiffConstants.EXIF_TAG_XPTITLE, newValue);
 testCase.updateMetaDataTags(file, TiffConstants.EXIF_TAG_XPSUBJECT,
newValue);
testCase.updateMetaDataTags(file, TiffConstants.EXIF_TAG_XPCOMMENT,
newValue);
 }
catch(Exception ex)
{
 ex.printStackTrace();
}
 //Read data
testCase.getImageData(file, true);
}
 /**
 * Read meta-data from image file and save it to Metadata_Data object that
 * is then returned.
 *
 * @param sourceFile
 * @param printOut
 *            : if true, console will show what image meta data was found.
 * @return
 */
public void getImageData(File sourceFile, boolean printOut)
 {
IImageMetadata metadata = null;
 try
{
metadata = Sanselan.getMetadata(sourceFile);
 }
catch (Exception e)
{
 e.printStackTrace();
}
 if (metadata instanceof JpegImageMetadata)
{
JpegImageMetadata jpegMetadata = (JpegImageMetadata)metadata;
 getTagValue(jpegMetadata, TiffConstants.EXIF_TAG_XPTITLE, printOut);
 getTagValue(jpegMetadata, TiffConstants.EXIF_TAG_XPSUBJECT, printOut);
getTagValue(jpegMetadata, TiffConstants.EXIF_TAG_XPCOMMENT, printOut);
 }
}
 /**
 * Read ALL EXIF entries for specified IMAGE file.
 * @param sourceFile
 */
public static void printAllImageMetaDataValues(File sourceFile)
{
 IImageMetadata metadata = null;
 try
 {
metadata = Sanselan.getMetadata(sourceFile);
}
 catch (Exception e)
{
e.printStackTrace();
 }
 if (metadata instanceof JpegImageMetadata)
 {
JpegImageMetadata jpegMetadata = (JpegImageMetadata)metadata;
System.out.println("EXIF items -");
 @SuppressWarnings("rawtypes")
ArrayList items = jpegMetadata.getItems();
 for (int i = 0; i < items.size(); i++)
{
TiffImageMetadata.Item item = (TiffImageMetadata.Item)items.get(i);
 System.out.println("  " + item.getKeyword() + " = " + item.getText());
}
 }
}
 /**
 * Method returns value for specific meta-data tag. If tag info is not
 * found, null is returned.
 *
 * @param jpegMetadata
 * @param tagInfo
 * @param printOut
 *            : if true, results are printed in console window.
 * @return
 */
private String getTagValue(JpegImageMetadata jpegMetadata, TagInfo tagInfo,
boolean printOut)
 {
TiffField field = jpegMetadata.findEXIFValue(tagInfo);
if (field == null)
 {
if (printOut)
System.out.println("  " + tagInfo.name + ": " + "Not Found.");
 return null;
}
 else
{
if (printOut)
 System.out.println("  " + tagInfo.name + ": " +
field.getValueDescription());
 // return field.getValueDescription();
String result = field.getValueDescription();
 if (result.startsWith("'") && result.endsWith("'"))
result = result.substring(1, result.length() - 1);
 return result;
}
 }
 /**
 * Method inserts new meta-data that is supplied to the method to source
 * file.
 *
 * @param sourceFile
 * @param data
 */
public boolean updateMetaDataTags(File sourceFile, TagInfo tagInfo, String
value)
 {
File destinationFile = null;
IImageMetadata metadata = null;
 JpegImageMetadata jpegMetadata = null;
TiffImageMetadata exif = null;
OutputStream os = null;
 TiffOutputSet outputSet = new TiffOutputSet();
 // Establish metadata
 try{metadata = Sanselan.getMetadata(sourceFile);}
catch (Exception 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)
 {
updateEXIFTag(tagInfo, value, outputSet, SanselanTestCase.ENCODING);
 }
 // Create stream using temp file for destination
 try
{
destinationFile = File.createTempFile("temp-" + System.currentTimeMillis(),
".jpeg");
 os = new FileOutputStream(destinationFile);
os = new BufferedOutputStream(os);
 }
catch (IOException e){System.out.println("Error occured while saving EXIF
tags.");}
 // Write/update EXIF meta-data to output stream
try{exifRewriter.updateExifMetadataLossless(sourceFile, os, outputSet);}
 catch (Exception e){System.out.println("Error occured while saving EXIF
tags.");}
finally
 {
if (os != null)
{
 try{os.close();}
catch (IOException e){}
}
 }
 // Overwrite original file
 SanselanTestCase.TEMPORARY_FILE = new File(sourceFile.getAbsolutePath());
sourceFile.delete();
 boolean result = destinationFile.renameTo(SanselanTestCase.TEMPORARY_FILE);
 if (!result)
System.out.println("Not moved!");
else
 System.out.println("Image data for >" + sourceFile.getName() + "<
saved/updated!");
 SanselanTestCase.TEMPORARY_FILE = null;
//Delete temporary file
destinationFile.delete();
 return result;
}

private void updateEXIFTag(TagInfo tag, String value, TiffOutputSet
outputSet, String encoding)
{
 if( value == null )
value = "";
 TiffOutputField imageHistoryPre = outputSet.findField(tag);
if (imageHistoryPre != null)
 {
outputSet.removeField(tag);
}
 try
{
 /*
byte[] bytes = tag.encodeValue(TiffFieldTypeConstants.FIELD_TYPE_ASCII,
value, outputSet.byteOrder);
 TiffOutputField valueField = new TiffOutputField(tag, tag.dataTypes[0],
bytes.length, bytes);
*/
 byte[] bytes = value.getBytes(encoding);
TiffOutputField valueField = new TiffOutputField(tag,
TiffFieldTypeConstants.FIELD_TYPE_ASCII, bytes.length, bytes);
 outputSet.getOrCreateExifDirectory().add(valueField);
}
 catch (Exception e){e.printStackTrace();}
}
}

With it I can save and read custom user text from few XP fields. Though the
saved text is not shown in Windows Explorer and I have yet to check it
against EXIF tools it is a minor thing. Not important. What is kind of
important are the special characters. Now, At the top, there is a string
called "newValue". And at the end it has appended few special characters
used in my country (Damjan should also be familiar with them :D).

What for the gods sake should I do to store or read them correctly. I am
using sanselan 0.97. If someone can compile latest one from SVN and update
the above code... that will work just fine also. I am just out of ideas as
from here on :D

Thank you for your input and thank you Damjan for even providing Sanselan.
Even with its quirks, it is the best.
Cheers/Pozdrav,
MJ

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message