I need to access the XMP-metadata stored by Photoshop in jpeg-files. I have
been looking for a solution for hours on google, google groups etc et c
Fields can be found here: http://www.portfoliofaq.com/pfaq/FAQ00311.htm
And Adobe (who invented the format as far as I can tell) has created and
SDK: http://partners.adobe.com/public/developer/xmp/sdk/index.html#gs
Unfortunately there is only some sort of C-source in expat_config.h (NOT
C#), which I have NO clue what to do with...
I have also found a commercial .net component which will read the metadata
(http://www.atalasoft.com/products/dotImage/) but is is priced at 999$ - and
I only need to read a bit of metadata from an open format, so 999$ seems
like overkill. ;)
I have tried to view the jpeg-files in a text-editor, and the meta-data is
very neatly formatted, example:
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.print.PageFormat.PMHorizontalRes</key>
[etc etc]
What I need is some code to access the metadata. Any help will be greatly
appreciated.
Thanks in advance
Klaus Jensen, MCSD
Allow me to answer my own question. :)
I did manage to get the xml in the file using a hack, and then I had access
to the xml-metadata.
It is certainly NOT very pretty code, but it will give another developer
facing a similar challenge something to work with. :)
Public Class ImageMetaData
Public CaptionWriter As String
Public AuthorsPosition As String
Public Credit As String
Public Source As String
Public Category As String
Public Colormode As String
Public ICCProfile As String
Public DateCreated As String
Public Urgency As String
Public Instructions As String
Public Headline As String
Public Description As String
Public Sub New(ByVal filename As String)
Dim xmpXmlDoc As String
xmpXmlDoc = GetXmpXmlDocFromImage(filename)
LoadDoc(xmpXmlDoc)
End Sub
Private Sub LoadDoc(ByVal xmpXmlDoc As String)
Dim xr As New Xml.XmlDocument
Dim nl As Xml.XmlNodeList
Try
xr.LoadXml(xmpXmlDoc)
Catch ex As Exception
Throw New ApplicationException("An error occured while loading XML metadata
from image. The error was: " & ex.Message)
End Try
Try
xr.LoadXml(xmpXmlDoc)
nl = xr.GetElementsByTagName("dc:description")
Me.Description = nl.Item(0).ChildNodes(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:CaptionWriter")
Me.CaptionWriter = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:AuthorsPosition")
Me.AuthorsPosition = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:Credit")
Me.Credit = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:Source")
Me.Source = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:Category")
Me.Category = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:ColorMode")
Me.Colormode = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:ICCProfile")
Me.ICCProfile = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:DateCreated")
Me.DateCreated = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:Urgency")
Me.Urgency = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:Instructions")
Me.Instructions = nl.Item(0).InnerText()
nl = xr.GetElementsByTagName("photoshop:Headline")
Me.Headline = nl.Item(0).InnerText()
Catch ex As Exception
Throw New ApplicationException("Error occured while readning meta-data from
image. The error was: " & ex.Message)
Finally
nl = Nothing
xr = Nothing
End Try
End Sub
Public Overrides Function Tostring() As String
Dim output
output += "CaptionWriter: " & Me.CaptionWriter & vbCrLf
output += "AuthorsPosition: " & Me.AuthorsPosition & vbCrLf
output += "Credit: " & Me.Credit & vbCrLf
output += "Source: " & Me.Source & vbCrLf
output += "Category: " & Me.Category & vbCrLf
output += "Colormode: " & Me.Colormode & vbCrLf
output += "ICCProfile: " & Me.ICCProfile & vbCrLf
output += "DateCreated: " & Me.DateCreated & vbCrLf
output += "Urgency: " & Me.Urgency & vbCrLf
output += "Instructions: " & Me.Instructions & vbCrLf
output += "Headline: " & Me.Headline & vbCrLf
output += "Description: " & Me.Description & vbCrLf
Return output
End Function
Public Shared Function GetXmpXmlDocFromImage(ByVal filename As String) As
String
Dim contents As String
Dim xmlPart As String
Dim beginCapture As String = "<x:xmpmeta"
Dim endCapture As String = "</x:xmpmeta>"
Dim beginPos, endPos As Integer
Dim sr As New System.IO.StreamReader(filename)
contents = sr.ReadToEnd
Debug.Write(contents.Length & " chars" & vbCrLf)
sr.Close()
sr = Nothing
beginPos = InStr(contents, beginCapture, CompareMethod.Text) - 1
endPos = InStr(contents, endCapture, CompareMethod.Text)
Debug.Write("xml found at pos: " & beginPos.ToString & " - " &
endPos.ToString)
xmlPart = contents.Substring(beginPos, (endPos - beginPos) +
endCapture.Length)
Debug.Write("Xml len: " & xmlPart.Length.ToString)
Return xmlPart
End Function
End Class