java - Modifying or adding new tags to a jpg (Metadata) with iim4j -


i trying modify or add new tags iim4j library, documentation poor. i've been searching examples on internet , didn't found far. got examples library offers. there's 1 example reading metadata , can title, description , tags of image (the ones care). know library manage these info dataset , datasetinfo tried create new instances of these objects info want add have no results far.

this code reading iptc section of jpeg file:

    public static void dump(file file) throws exception {      system.out.println("iptc segment " + file);     iimfile iimfile = new iimfile();      iimreader reader = new iimreader(new jpegiiminputstream(new fileiiminputstream(file)), new iimdatasetinfofactory());     iimfile.readfrom(reader, 20);      (iterator = iimfile.getdatasets().iterator(); i.hasnext();) {         dataset ds = (dataset) i.next();                     object value = ds.getvalue();         if (value instanceof byte[]) {             value = "<bytes " + ((byte[]) value).length + ">";         }         datasetinfo info = ds.getinfo();         if (info instanceof defaultdatasetinfo)         {             system.out.println("-----------");             system.out.println("number : " + info.getdatasetnumber());             system.out.println("name :" + info.getname());             serializer serializador=info.getserializer();             if(serializador instanceof stringserializer)                 system.out.println("serializer :" + serializador.tostring());             system.out.println("repeat : " + info.isrepeatable());             system.out.println("----------");         }          system.out.println(info.tostring() + " " + info.getname() + ": " + value);      }     reader.close(); } 

i can obtain info need , know kind of objects must use add new info on files. tried 1 add new tag:

    string tagtoadd="tag add";     int size= tagtoadd.length();     defaultdatasetinfo valuetag=new defaultdatasetinfo(537,"keywords", new stringserializer(size+ ""),true);     defaultdataset dataset=new defaultdataset(valuetag, tagtoadd.getbytes());     iimfile.add(dataset); 

and tried 1 write new file tag:

    string tagtoadd="tag add";     int size= tagtoadd.length();     defaultdatasetinfo valuetag=new defaultdatasetinfo(537,"keywords", new stringserializer(size+ ""),true);     defaultdataset dataset=new defaultdataset(valuetag, tagtoadd.getbytes());      iimwriter escritor= new iimwriter(new defaultiimoutputstream(file));     escritor.write(dataset);     escritor.close(); 

tags weren't add. need library , use. thanks

from own testing, seems iimwriter intended used write iptc metadata file, , nothing else. needless say, not make valid jpeg file. want take existing jpeg file, , generate new file same data, except iptc metadata should removed (if any) , replaced content of iimfile.

that's jpegutil.insertiimintojpeg for. need (where newfile file insatnce pointing want save modified jpeg):

// 1. read original iim iimreader reader = new iimreader(new jpegiiminputstream(new fileiiminputstream(file)), new iimdatasetinfofactory()); iimfile.readfrom(reader, 20);  // 2. add dataset dataset dataset = ... // whatever want iimfile.add(dataset);  // 3. create new copy of jpeg file new iim try (   inpustream in = new bufferedinputstream(new fileinputstream(file));   outputstream out = new bufferedoutputstream(new fileoutputstream(newfile)) ) {   jpegutil.insertiimintojpeg(out, iimfile, in) } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -