Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read XMP face data from JPEG in Java

I have saved Picasa's face data inside my JPEG files (in XMP) and now I am trying to read that information in Java. So far I am failing and help would be much appreciated.

I am trying to use metadata-extractor library (though any other solution would also be fine). I can read the basic information (like the date, the image size etc.), but I am lost at extracting the additional data. This is what I get so far:

File file -- this is my JPEG file

Metadata metadata = JpegMetadataReader.readMetadata(file);
XmpDirectory xmpDirectory = metadata.getDirectory(XmpDirectory.class); 
XMPMeta xmpMeta = xmpDirectory.getXMPMeta();
System.out.println(xmpMeta.dumpObject()); 

Result:

ROOT NODE
    http://www.metadataworkinggroup.com/schemas/regions/ = "mwg-rs:" (0x80000000 : SCHEMA_NODE)
        mwg-rs:Regions  (0x100 : STRUCT)
            mwg-rs:AppliedToDimensions  (0x100 : STRUCT)
                stDim:h = "2793"
                stDim:unit = "pixel"
                stDim:w = "2047"
            mwg-rs:RegionList   (0x200 : ARRAY)
                [1] (0x100 : STRUCT)
                    mwg-rs:Area (0x100 : STRUCT)
                        stArea:h = "0.69531"
                        stArea:unit = "normalized"
                        stArea:w = "0.790425"
                        stArea:x = "0.491451"
                        stArea:y = "0.41783"
                    mwg-rs:Name = "abcde"
                    mwg-rs:Type = "Face"
    http://ns.adobe.com/xap/1.0/ = "xmp:"   (0x80000000 : SCHEMA_NODE)
        xmp:ModifyDate = "2014-04-06T19:43:24+01:00"

I do not understand how to get to these stArea:w, mwg-rs:Type = "Face", etc.

like image 290
texnic Avatar asked Mar 16 '26 16:03

texnic


1 Answers

As usual, right after posting I've found a solution. I'll list it below to have it here.

try {
    Metadata metadata = ImageMetadataReader.readMetadata(imageFile);
    XmpDirectory xmpDirectory = metadata.getDirectory(XmpDirectory.class);
    XMPMeta xmpMeta = xmpDirectory.getXMPMeta();
    XMPIterator itr = xmpMeta.iterator();
    while (itr.hasNext()) {
        XMPPropertyInfo pi = (XMPPropertyInfo) itr.next();
        if (pi != null && pi.getPath() != null) {
            if ((pi.getPath().endsWith("stArea:w")) || (pi.getPath().endsWith("mwg-rs:Name")) || (pi.getPath().endsWith("stArea:h")))
                System.out.println(pi.getValue().toString());
        }
    }
} catch (final NullPointerException npe) {
  // ignore
}

What I do not like here is that it iterates through all properties instead of just reading the required ones. Any better (faster) solution?

like image 93
texnic Avatar answered Mar 18 '26 05:03

texnic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!