0

我正在尝试批量编辑 2000 多张带有标题和标签的 jpg 照片。我可以轻松获得 ImageDescription (tag) 和 Title 字段,但我找不到插入新标题和新标签的方法。

有没有关于如何使用 Apache Imaging Lib(或其他比 Apaches 更好的库)插入新标题和标签的示例?

这是我现在提取数据的方式:

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());

编辑:这是我目前在编辑“XPTitle”字段方面所获得的

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

4

1 回答 1

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

}
于 2022-01-05T16:04:40.533 回答