簡體   English   中英

使用 Java 編輯 jpeg EXIF 數據

[英]Editing jpeg EXIF data with Java

我想編輯 jpg 文件的屬性,如:評論、標題、拍攝日期、相機制造商等。

在此處輸入圖像描述

我找到了可以讀取這些數據的圖書館。 但是我需要一個帶有示例的免費庫來編輯它們。

我知道 apache 的成像 (sanselan)。 但我無法用它編輯數據。 如果您以前自己使用過它,只有在您提供示例代碼而不是他們網站上的代碼時,我才會接受它作為答案。 因為即使我使用他們的示例,我也無法編輯 GPS 數據以外的任何屬性。 在我運行代碼后,文件屬性詳細信息仍然具有相同的值。

謝謝 !

注意:我還嘗試了 JHeader ( https://sourceforge.net/projects/jheader/ ),但將它用作帶有 -cl 選項的進程仍然沒有更改屬性列表。

Apache commons Imaging 對我有用。

我已經擴展了此處提供的示例

很明顯我的客戶端代碼看起來像這樣

public static void main(String[] args) throws ImageWriteException, ImageReadException, IOException {
    new WriteExifMetadataExample().changeExifMetadata(new File("somefilename.jpg"), new File("result_file.jpg"));
}

以及 WriteExifMetadataExample 中的擴展方法

public void changeExifMetadata(final File jpegImageFile, final File dst)
        throws IOException, ImageReadException, ImageWriteException {
    OutputStream os = null;
    boolean canThrow = false;
    try {
        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) {
                // TiffImageMetadata class is immutable (read-only).
                // TiffOutputSet class represents the Exif data to write.
                //
                // Usually, we want to update existing Exif metadata by
                // changing
                // the values of a few fields, or adding a field.
                // In these cases, it is easiest to use getOutputSet() to
                // start with a "copy" of the fields read from the image.
                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();
        }

        {
            // Example of how to add a field/tag to the output set.
            //
            // Note that you should first remove the field/tag if it already
            // exists in this directory, or you may end up with duplicate
            // tags. See above.
            //
            // Certain fields/tags are expected in certain Exif directories;
            // Others can occur in more than one directory (and often have a
            // different meaning in different directories).
            //
            // TagInfo constants often contain a description of what
            // directories are associated with a given tag.
            //
            final TiffOutputDirectory exifDirectory = outputSet
                    .getOrCreateExifDirectory();
            // make sure to remove old value if present (this method will
            // not fail if the tag does not exist).
            exifDirectory
                    .removeField(ExifTagConstants.EXIF_TAG_APERTURE_VALUE);
            exifDirectory.add(ExifTagConstants.EXIF_TAG_APERTURE_VALUE,
                    new RationalNumber(3, 10));
        }

        {
            // Example of how to add/update GPS info to output set.

            // New York City
            final double longitude = -74.0; // 74 degrees W (in Degrees East)
            final double latitude = 40 + 43 / 60.0; // 40 degrees N (in Degrees
            // North)

            outputSet.setGPSInDegrees(longitude, latitude);
        }



        final TiffOutputDirectory exifDirectory = outputSet
                .getOrCreateRootDirectory();
        exifDirectory
                .removeField(ExifTagConstants.EXIF_TAG_SOFTWARE);
        exifDirectory.add(ExifTagConstants.EXIF_TAG_SOFTWARE,
                "SomeKind");

        os = new FileOutputStream(dst);
        os = new BufferedOutputStream(os);

        new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os,
                outputSet);

        canThrow = true;
    } finally {
        IoUtils.closeQuietly(canThrow, os);
    }
}

請只注意我添加附加標簽的行

final TiffOutputDirectory exifDirectory = outputSet
                .getOrCreateRootDirectory();
        exifDirectory
                .removeField(ExifTagConstants.EXIF_TAG_SOFTWARE);
        exifDirectory.add(ExifTagConstants.EXIF_TAG_SOFTWARE,
                "SomeKind");

結果是正確添加了 EXIF 標簽

在此處輸入圖片說明


要更改評論標簽,您可以執行以下操作

        final TiffOutputDirectory exifDirectory = outputSet.getOrCreateRootDirectory();
        exifDirectory.removeField(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT);
        exifDirectory.add(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT, "SomeKind");

可用常量的完整列表在包中:

org.apache.commons.imaging.formats.tiff.constants

在此處輸入圖片說明

這樣的例子對你有用嗎?

我假設使用像 org.apache.commons.imaging.util.IoUtils 和 import org.apache.commons.imaging.Imaging 這樣的包在這里對你有很大幫助。

要更改評論標簽,您可以執行以下操作

        final TiffOutputDirectory exifDirectory = outputSet.getOrCreateRootDirectory();
        exifDirectory.removeField(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT);
        exifDirectory.add(MicrosoftTagConstants.EXIF_TAG_XPCOMMENT, "SomeKind");

可用常量的完整列表在包中:

org.apache.commons.imaging.formats.tiff.constants

在此處輸入圖片說明

您需要使用 Microsoft 標簽約束來編輯標簽

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM