Here is an example using Apache Imaging (building on some of the code samples), where we are updating the common Windows fields such as Title, Subject, Comment, Rating, Keywords. There is one issue that the title does not get updated if it already has a value - if it is blank, it gets written successfully. I know this is a bit late but I was trying to do the same thing - the complexity is understanding which tag constants to use, as there are so many of them! It is necessary to remove the field before adding (as a replacement).
public static boolean updateWindowsFields(final File jpegImageFile, final File dst)
throws IOException, ImageReadException, ImageWriteException {
try (FileOutputStream fos = new FileOutputStream(dst);
OutputStream os = new BufferedOutputStream(fos)) {
TiffOutputSet outputSet = null;
// note that metadata might be null if no metadata is found.
final ImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
if (null != jpegMetadata) {
// note that exif might be null if no Exif metadata is found.
final TiffImageMetadata exif = jpegMetadata.getExif();
if (null != exif) {
outputSet = exif.getOutputSet();
}
}
// if file does not contain any exif metadata, we create an empty
// set of exif metadata. Otherwise, we keep all of the other
// existing tags.
if (null == outputSet) {
outputSet = new TiffOutputSet();
}
final TiffOutputDirectory rootDir = outputSet.getOrCreateRootDirectory();
rootDir.removeField(MicrosoftTagConstants.EXIF_TAG_XPTITLE);
rootDir.add(MicrosoftTagConstants.EXIF_TAG_XPTITLE, "new title");
rootDir.removeField(MicrosoftTagConstants.EXIF_TAG_XPSUBJECT);
rootDir.add(MicrosoftTagConstants.EXIF_TAG_XPSUBJECT, "new subject");
//
rootDir.removeField(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT);
rootDir.add(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT, "new comment");
//
rootDir.removeField(MicrosoftTagConstants.EXIF_TAG_XPKEYWORDS);
rootDir.add(MicrosoftTagConstants.EXIF_TAG_XPKEYWORDS, "key1;key2");
//
rootDir.removeField(MicrosoftTagConstants.EXIF_TAG_RATING);
rootDir.add(MicrosoftTagConstants.EXIF_TAG_RATING, (short) 4);
new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
outputSet);
return true;
}
catch(Exception e)
{
return false;
}
}