【问题标题】:Editing jpeg EXIF data with Java使用 Java 编辑 jpeg EXIF 数据
【发布时间】:2016-08-20 11:07:54
【问题描述】:

我想编辑 jpg 文件的属性,例如:cmets、标题、拍摄日期、相机制造商等。

我找到了可以读取这些数据的库。但是我需要一个带有示例的免费库来编辑它们。

我知道 apache 的成像 (sanselan)。但我无法用它编辑数据。如果您以前自己使用过它,那么只有在您提供示例代码而不是他们网站中的示例代码时,我才会接受它作为答案。因为即使我使用他们的示例,我也无法编辑 GPS 数据以外的任何属性。运行代码后,file-properties-details 仍然具有相同的值。

谢谢!

注意:我也尝试过 JHeader (https://sourceforge.net/projects/jheader/),但将其用作带有 -cl 选项的进程仍然没有更改属性列表。

【问题讨论】:

    标签: java image jpeg exif


    【解决方案1】:

    this work for you?这样的例子

    我假设在这里使用 org.apache.commons.imaging.util.IoUtils 和 import org.apache.commons.imaging.Imaging 之类的包对您有很大帮助。

    【讨论】:

    • 我猜@yurko 已经提到了同一个库。我会测试它并报告结果非常感谢。
    【解决方案2】:

    Apache commons Imaging 对我有用。

    我已经扩展了here提供的示例

    很明显我的客户端代码是这样的

    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 标记


    要更改 cmets 标签,您可以执行以下操作

            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
    

    【讨论】:

    • 以前我使用 Sanselan 库认为它在功能上与 Imaging 相同(我无法下载 Imaging 的 jar)。实际上我在使用 Sanselan 的例子时遇到了问题。我将使用您的代码尝试 Imaging by Maven 并报告结果。
    • 您的代码已成功编辑程序名称标签,但正如我所说,我需要更新标签,例如 cmets、标题、拍摄日期、相机制造商。我找不到解释 ExifTagConstants 映射到什么的文档。还有什么数据类型的值应该用于 add 方法。
    • MicrosoftTagConstants 像魔术一样工作。非常感谢。我也会搜索其他常量。
    【解决方案3】:

    要更改 cmets 标签,您可以执行以下操作

            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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-16
      • 2021-09-08
      • 2017-07-12
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      相关资源
      最近更新 更多