Reputation: 153
I am trying to modify or add new tags with the iim4j library, but its documentation is poor. I've been searching for examples on the internet and didn't found anything so far. I only got the examples the library offers. There's one example about reading the Metadata and I can get the title, description and tags of the image (the ones I care). I know the library manage these info as DataSet and DataSetInfo so I tried to create new instances of these objects for the info I want to add but I have no results so far.
This is the code for reading the IPTC section of a jpeg file:
public static void dump(File file) throws Exception {
System.out.println("IPTC segment for " + file);
IIMFile iimFile = new IIMFile();
IIMReader reader = new IIMReader(new JPEGIIMInputStream(new FileIIMInputStream(file)), new IIMDataSetInfoFactory());
iimFile.readFrom(reader, 20);
for (Iterator i = 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 all the info I need and also I know that kind of objects I must use to add new info on another files. So i tried this one to add a new tag:
String tagToAdd="Tag to 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 this one to write a new file with a tag:
String tagToAdd="Tag to 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. I need some help with this library and its use. Thanks
Upvotes: 3
Views: 811
Reputation: 32719
From my own testing, it seems that IIMWriter
is only intended to be used to write the IPTC metadata to a file, and nothing else. Needless to say, that does not make for a valid JPEG file. What you want is to take an existing JPEG file, and generate a new file with the same data, except for the IPTC metadata that should be removed (if any) and replaced with the content of your IIMFile
.
That's exactly what JPEGUtil.insertIIMIntoJPEG
is for. What you need to do is something like this (where newFile
is a File
insatnce pointing to where you want to save the 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 you 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)
}
Upvotes: 1