Java 修改exif 信息,使用Java编辑jpeg EXIF数据

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

4e3c752ec070fbba1d3834515144d037.png

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

578e013a9af9ebb0e02b625c8beab2a5.png

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

db329beb8e76f0890ca5121c766f5807.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值