Adding custom namespace to metadata (BitmapMetadat

2019-06-26 03:52发布

问题:

Here's a quick question: how to correctly add a custom namespace to XMP using BitmapMetadata?

Let's say I want the namespace to look like this: xmlns:MyNamespace="http://test"

There's no clear way how to add the namespace in the BitmapMetadata, so I tried this:

//I retrieve the image frame (Frame[0]), then:
var metadata = (BitmapMetadata) frame.Metadata.Clone();

//Covering all bases
metadata.SetQuery("/app1/ifd/PaddingSchema:Padding", 4096);
metadata.SetQuery("/app1/ifd/exif/PaddingSchema:Padding", 4096);
metadata.SetQuery("/xmp/PaddingSchema:Padding", 4096);

And now the main query, I tried going the 'obvious way':

metadata.SetQuery("/xmp/MyNamespace:MyTag", "AwesomeTagValue");

And sure enough, if I save the image, open it and run

var value = (string) metadata.GetQuery("/xmp/MyNamespace:MyTag");

it returns the correct value - AwesomeTagValue.

Here's the problem though, the tags are written to the file with malformed namespace. I peeked into the file and here's the stripped XML/RDF view:

<rdf:Description rdf:about="" xmlns:prefix0="MyNamespace"> ... </rdf:Description>

so all the tags are prefixed with prefix0 and I'd like it to be

<rdf:Description rdf:about="" xmlns:MyNamespace="http://test"> ... </rdf:Description>

Any ideas how to do that or if it's even possible with BitmapMetadata?

回答1:

The best you can achieve with BitmapMetadata:

<rdf:Description xmlns:prefix0="http://test">...</rdf:Description>

Here is the metadata query:

metadata.SetQuery("/xmp/{wstr=http://test}:MyTag", "AwesomeTagValue");

According to documentation:

If there is no friendly schema prefix for a particular schema, for example if an image contains XMP metadata using a custom third party schema, the metadata query should use the full schema URL.

So if you are not satisfied with the result you could:

  • Open the image file and replace all prefix0 with MyNamespace
  • Use a third party library to modify metadata