Read /Edit / Write jpg IPTC metadata in Java

喜你入骨 提交于 2021-01-27 22:44:16

问题


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

and my EXCEPTION

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)


回答1:


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

  • Read IPTC from image.
  • Insert IPTC into image or update IPTC of existing image.
  • Delete IPTC from image.

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

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

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.

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.



来源:https://stackoverflow.com/questions/50669162/read-edit-write-jpg-iptc-metadata-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!