0

I am trying to bulk edit over 2000 jpg photos with a title and tag. I can easily get both the ImageDescription (tag) and Title fields but I cannot find a way to insert a new title and new tags.

Are there any examples on how to insert new titles and tags using the Apache Imaging Lib (or another library that is better than Apaches?

Here is how I am extracting the data now:

JpegImageMetadata metadata = (JpegImageMetadata) Imaging.getMetadata(image);
TiffImageMetadata tiffMetadataItem = metadata.getExif();
TiffOutputSet tiffOutputSet = tiffMetadataItem.getOutputSet();

if (tiffOutputSet == null) {
    tiffOutputSet = new TiffOutputSet();
}

TiffOutputDirectory tiffOutputDirectory = tiffOutputSet.getOrCreateExifDirectory();

System.out.println(tiffOutputDirectory.getFields());

EDIT: This is as far as I have currently gotten in terms of editing the field 'XPTitle'

https://gist.github.com/TheMasteredPanda/08fe51447fc6de47293bf1b34758e692

  • Can you show the code that extracts the fields? – Sean Bright Apr 20 '17 at 17:07
  • [This example code](https://commons.apache.org/proper/commons-imaging/xref-test/org/apache/commons/imaging/examples/WriteExifMetadataExample.html) shows how to read/write EXIF metadata. Is that what you want? – Sean Bright Apr 20 '17 at 17:11
  • In the official docs or in the program, I am writing? If it is the program then here is the snippet of code that extracts the metadata from the photo file. https://gist.github.com/TheMasteredPanda/548a0fcc222d3b4a83eb586c3d7e7660 – Duke Jake Morgan Apr 20 '17 at 17:13
  • Look at the link from my previous comment. There is a method named `setExifGPSTag` that shows how to write the `TiffOutputSet` to a new file. – Sean Bright Apr 20 '17 at 17:17
  • Alright, I understand how to write to a new file. But my problem is I don't know how to write a new image title and add tags to the image. So where TITLETITLE~ and HELLO~ are is the fields I want to alter. https://gyazo.com/b6509feb342b10a213c80c805541866d Am I missing something blatantly obvious? – Duke Jake Morgan Apr 20 '17 at 17:53

1 Answers1

0

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;
    }

}