Adding custom namespace to metadata (BitmapMetadata)

百般思念 提交于 2019-12-10 17:39:50

问题


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


来源:https://stackoverflow.com/questions/15758054/adding-custom-namespace-to-metadata-bitmapmetadata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!