使用Java编辑jpeg EXIF数据 [英] Editing jpeg EXIF data with Java

查看:306
本文介绍了使用Java编辑jpeg EXIF数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编辑jpg文件的属性,例如:评论,标题,拍摄日期,相机制作者等。





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



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



谢谢!



注意:我也试过JHeader(






要更改评论标记,您可以执行以下操作

  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 


I want to edit jpg files' properties like: comments, title, date taken, camera maker, etc.

I have found libraries to read these data. But I need a free library with examples to edit them.

I'm aware of apache's imaging (sanselan). But I was not able to edit data with it. If you have previously used it yourself, I'd accept that as an answer only if you provide an example code other than the one in their website. Because even when I use their example I was not able to edit any property other than GPS data. After i run the code, file-properties-details still have the same values.

Thanks !

Note: I also tried JHeader (https://sourceforge.net/projects/jheader/) but using it as a process with -cl option still did not changed properties list.

解决方案

Apache commons Imaging works for me.

I have extended the sample provided here

So obviously my client code looks like this

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

and the extended method in 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);
    }
}

Please pay attention only to line where I add additional tag

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

as a result EXIF tag was properly added


To change the comments tag you can do the following

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

the full list of available constants is in the package:

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

这篇关于使用Java编辑jpeg EXIF数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆