在 Java 中读取/编辑/写入 jpg IPTC 元数据

Read /Edit / Write jpg IPTC metadata in Java

我正在使用 Apache Commons,但它对我来说还不够,因为它是一项非常古老的技术。所以,我发现 iCafe 似乎更好,但我遇到以下错误。知道我做错了什么吗?

      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);
    }
}

和我的例外

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.2\executor-snippets\run.xml:53: Java returned: 1 BUILD FAILED (total time: 0 seconds)

不确定您到底想要什么,但 ICAFE 可以使用 IPTC 元数据执行以下操作:

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

阅读IPTC,这里有一个例子:

    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);
                 }          
            }
        }   
    }

对于insert/update IPTC,这里有一个例子:

    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 条目允许多个值。在那种情况下,我们只 append/add 新的。对于其他独特的条目,它们将被新条目替换。

您似乎想添加关键词和标题。在您的问题中,您已经显示了插入关键字的代码,为了插入标题,请使用 OBJECT_NAME ,可以在上面的示例中找到。

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