用Java读取/编辑/写入jpg IPTC元数据 [英] Read /Edit / Write jpg IPTC metadata in Java

查看:86
本文介绍了用Java读取/编辑/写入jpg IPTC元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apache Commons,但这对我来说还不够,因为它是一种非常古老的技术.因此,我找到了 iCafe ,它看起来更好,但是下面出现了错误.知道我在做什么错吗?

I am using Apache Commons but it is not enough for me because it is so old technology. So ,i found iCafe and it seems better but I am having the error below. Any idea what i am doing wrong?

      private static List<IPTCDataSet> createIPTCDataSet() {
          List<IPTCDataSet> iptcs = new ArrayList<IPTCDataSet>();
          iptcs.add(new IPTCDataSet(IPTCApplicationTag.COPYRIGHT_NOTICE, "Copyright 2014-2016, yuwen_66@yahoo.com"));
          iptcs.add(new IPTCDataSet(IPTCApplicationTag.CATEGORY, "ICAFE"));
          iptcs.add(new IPTCDataSet(IPTCApplicationTag.KEY_WORDS, "Welcome 'icafe' user!"));

          return iptcs;
      }

      private static IPTC createIPTC() {
        IPTC iptc = new IPTC();
        iptc.addDataSets(createIPTCDataSet());
        return iptc;
      }

      public static void main(String[] args) throws  IOException {

        FileInputStream fin = new FileInputStream("C:/Users/rajab/Desktop/test/ibo.jpeg");
        FileOutputStream fout = new FileOutputStream("C:/Users/rajab/Desktop/test/ibo/ibo.jpeg");

        List<Metadata> metaList = new ArrayList<Metadata>();
        //metaList.add(populateExif(TiffExif.class));
        metaList.add(createIPTC());
        metaList.add(new Comments(Arrays.asList("Comment1", "Comment2")));

        Metadata.insertMetadata(metaList, fin, fout);
    }
}

和我的例外

运行:线程主"中的异常java.lang.NoClassDefFoundError:org/slf4j/LoggerFactory位于com.icafe4j.image.meta.Metadata.(未知来源)在vectorcleaner.Metadata1.populateExif(Metadata1.java:41)在vectorcleaner.Metadata1.main(Metadata1.java:127)引起原因:java.lang.ClassNotFoundException:org.slf4j.LoggerFactory在java.net.URLClassLoader.findClass(URLClassLoader.java:381)在java.lang.ClassLoader.loadClass(ClassLoader.java:424)在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:349)在java.lang.ClassLoader.loadClass(ClassLoader.java:357)...另外3个C:\ Users \ rajab \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml:53:返回的Java:1失败(总时间:0秒)

run: Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory at com.icafe4j.image.meta.Metadata.(Unknown Source) at vectorcleaner.Metadata1.populateExif(Metadata1.java:41) at vectorcleaner.Metadata1.main(Metadata1.java:127) Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 3 more C:\Users\rajab\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

推荐答案

不确定您到底想要什么,但这是ICAFE可以使用IPTC元数据做什么:

Not sure what exactly you want but here is what ICAFE can do with IPTC metadata:

  • 从图像中读取IPTC.
  • 将IPTC插入图像或更新现有图像的IPTC.
  • 从图像中删除IPTC.

要阅读IPTC,请举一个例子:

For reading IPTC, here is an example:

    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    import java.util.Iterator;

    import com.icafe4j.image.meta.Metadata;
    import com.icafe4j.image.meta.MetadataEntry;
    import com.icafe4j.image.meta.MetadataType;
    import com.icafe4j.image.meta.iptc.IPTC;

    public class ExtractIPTC {

        public static void main(String[] args) throws IOException {
            Map<MetadataType, Metadata> metadataMap = Metadata.readMetadata(args[0]);
            IPTC iptc = (IPTC)metadataMap.get(MetadataType.IPTC);

            if(iptc != null) {
                Iterator<MetadataEntry> iterator = iptc.iterator();

                while(iterator.hasNext()) {
                    MetadataEntry item = iterator.next();
                    printMetadata(item, "", "     ");
                }
            }   
        }
        private void printMetadata(MetadataEntry entry, String indent, String increment) {
            logger.info(indent + entry.getKey() (StringUtils.isNullOrEmpty(entry.getValue())? "" : ": " + entry.getValue()));
            if(entry.isMetadataEntryGroup()) {
                 indent += increment;
                 Collection<MetadataEntry> entries = entry.getMetadataEntries();
                 for(MetadataEntry e : entries) {
                    printMetadata(e, indent, increment);
                 }          
            }
        }   
    }

对于插入/更新IPTC,这是一个示例:

For insert/update IPTC, here is an example:

    import java.io.IOException;
    import java.util.List;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;

    import com.icafe4j.image.meta.Metadata;
    import com.icafe4j.image.meta.MetadataEntry;
    import com.icafe4j.image.meta.MetadataType;
    import com.icafe4j.image.meta.iptc.IPTCDataSet;
    import com.icafe4j.image.meta.iptc.IPTCApplicationTag;

    public class InsertIPTC {

        public static void main(String[] args) throws IOException {
           FileInputStream fin = new FileInputStream("C:/Users/rajab/Desktop/test/ibo.jpeg");
           FileOutputStream fout = new FileOutputStream("C:/Users/rajab/Desktop/test/ibo/ibo.jpeg");

           Metadata.insertIPTC(fin, fout, createIPTCDataSet(), true);
        }
        private static List<IPTCDataSet> createIPTCDataSet() {
            List<IPTCDataSet> iptcs = new ArrayList<IPTCDataSet>();
            iptcs.add(new IPTCDataSet(IPTCApplicationTag.COPYRIGHT_NOTICE, "Copyright 2014-2016, yuwen_66@yahoo.com"));
            iptcs.add(new IPTCDataSet(IPTCApplicationTag.OBJECT_NAME, "ICAFE"));
            iptcs.add(new IPTCDataSet(IPTCApplicationTag.KEY_WORDS, "Welcome 'icafe' user!"));

            return iptcs;
        }
    }

上面的示例使用Metadata.insertIPTC代替Metadata.insertMetadata,因为我们还需要一个布尔参数.如果设置为true,它将保留现有的IPTC数据,并仅更新我们想要的数据.一些IPTC条目允许多个值.在这种情况下,我们仅添加/添加新的.对于其他唯一条目,它们将被新条目替换.

The above example uses Metadata.insertIPTC instead of Metadata.insertMetadata because we need one more boolean parameter. If set to true, it will keep the existing IPTC data and only update the those we want. Some of the IPTC entries allow multiple values. In that case, we only append/add the new ones. For other unique entries, they will be replaced by the new ones.

好像您要添加关键字和标题.在您的问题中,您已经显示了用于插入关键字的代码,并且为了插入标题,请使用上面示例中的OBJECT_NAME.

Looks like you want to add key words and title. In your question, you already showed code to insert key words and in order to insert title, use OBJECT_NAME which can be found in the example above.

注意:您也可以添加多个关键字.有些软件只能处理一个关键词记录.在这种情况下,您可以将所有关键字放在一个用分号分隔的记录中,而不用插入多个条目.

Note: you can add multiple key words as well. Some softwares can only handle one key words record. In that case, you can put all the key words in one record separated by semi-colon instead of insert multiple entries.

这篇关于用Java读取/编辑/写入jpg IPTC元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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