Simple, concise Java examples that do useful things.

Friday, August 25, 2006

XMP metadata dumper

XMP is a metatdata format from Adobe. One way to add and edit XMP is with PixVue. This is a minimalist XMP extractor/dumper for the jpeg format.


import java.io.*;

public class XMPDumper {

public static void main(String[] args) throws IOException {
File f = new File("myimage.jpg");
String xmp = new XMPDumper().getXMP(f);
System.out.println(xmp);
}

public String getXMP(File f)
throws IOException {
FileInputStream is = new FileInputStream(f);
int i=0; String result=null;
while ((i=is.read()) != -1)
if (i==0xFF && is.read()==0xE1) {
result = checkNamespace(is);
if (result!=null) break;
}
return result;
}

public static final String namespace =
"http://ns.adobe.com/xap/1.0/";

private String checkNamespace(FileInputStream is)
throws IOException {
String s = getSegment(is);
return (s.startsWith(namespace)) ? s.substring(29) : null;
}

private String getSegment(InputStream is)
throws IOException {
DataInputStream dis = new DataInputStream(is);
byte[] buffer = new byte[dis.readShort()-2];
dis.read(buffer);
return new String(buffer);
}
}

No comments: