如何将元数据写入 .JPG? [英] How can I write metadata into a .JPG?

查看:36
本文介绍了如何将元数据写入 .JPG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此代码通过 apache commons.imaging(快照)获取这三个不同的元数据属性(Object Name"、ImageDescription"和Keywords").但是,我不知道如何写入此属性.有人知道正确的方法吗?提前致谢...

I'm using this code to get these three diferent metadata attributes ('Object Name', 'ImageDescription' and 'Keywords') using the apache commons.imaging (snapshot). However, I have no clue about how to write into this attributes. Does anybody know the proper way? Thanks in advance...

IImageMetadata metadata = null;
String name;

try {
   metadata = Imaging.getMetadata(new File(filename));
} catch (ImageReadException | IOException e) {
}

if (metadata instanceof JpegImageMetadata) {
   final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
   final List<IImageMetadataItem> items = jpegMetadata.getItems();

   for (int i = 0; i < items.size(); i++) {
      final IImageMetadataItem item = items.get(i);
      name = item.toString().substring(0, item.toString().indexOf(":"));
      switch (name) {
      case "Object Name" :
      case "ImageDescription" :
      case "Keywords" :
         System.out.println(item.toString());
         break;
      }
   }
}

推荐答案

格式apache.commons.imaging 的概览页面 表示不支持 IPTC 元数据写入,但支持 EXIF 元数据写入.为了编写 EXIF 元数据,我还用谷歌搜索并找到了一个 示例.所以你必须做的是一些事情:

The format overview page of apache.commons.imaging says that IPTC metadata writing is not supported but EXIF metadata writing is. For writing of EXIF metadata I also googled and found an example. So what you have to do is something along the lines:

final TiffImageMetadata exif = jpegMetadata.getExif();
TiffOutputSet outputSet = exif.getOutputSet();

然后添加或删除和添加(=更新)标签,最后:

then adding or removing and adding (=updating) tags and in the end:

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

使用 jpegImageFile 一个文件(输入)和一个输出文件的输出流.

with jpegImageFile a File (input) and os an OutputStream to the output file.

这篇关于如何将元数据写入 .JPG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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