VB.NET从图像中检索元标记

时间:2014-06-26 10:56:05

标签: vb.net image-processing metadata meta-tags

好吧,我现在已经搞乱了几天,我在板上发现了这个话题。

VB.NET Renaming File and Retagging / Edit Image MetaData / Meta Tags

通过阅读,并获得其中一个答案所涉及的库,我轻松地将代码插入到我的项目中,一切都运行良好。然而,我正在寻找这样的事情的全部原因是获取“关键字/标签”元数据,而这个代码无论我做了什么都不会给我我想要的东西。

知道'关键字'地址是& H9C9E我能够做到以下几点。

    Dim test As String = EX.GetPropertyString(&H9C9E)
    MsgBox(test)

这是有道理的,我搞砸了一段时间,但无论我改变了什么,我都无法显示标签的第一个字符。例如,我在关键字中放置了“测试”,它只返回“T”。我查看了图书馆的编码,但我无法弄清楚为什么它没有归还所有内容。我已经尝试更改标签中的内容以试图加载它,并且我尝试了一些不同的图像,尽管它总是只返回第一个字符。

此外,当我尝试更改关键字数据时,即使以下代码与其他所有代码一起使用,我也无法显示任何内容。

    EX.SetPropertyString(ExifWorks.TagNames.ExifUserComment, "This Worked")
    EX.GetBitmap.Save(SaveLoc + "1decfabd90e2355eac81b3f9735e10e3.jpg")

所以我的问题是,为什么它只显示第一个字符,为什么它不能保存到地址?

编辑:

在玩了一下之后,我能够得到“关键词”的内容,虽然它并不完全理想。

    Dim test = EX.GetProperty(&H9C9E)
    Dim test2 = EX.Encoding.GetCharCount(test)
    Dim test3 = EX.Encoding.GetChars(test)

    Dim tolnum As Integer = test2
    Dim cur As Integer = -1

    While tolnum > 0
        cur = cur + 1
        tolnum = tolnum - 1
        MsgBox(test3(cur))
    End While

例如,在“关键字”中使用“TEST”,我得到'T; E; S; T ;;;;' ;标记未知字符/空白msgbox。

1 个答案:

答案 0 :(得分:1)

好的,在Grim的帮助下,我已经能够纠正这个问题并找到答案。这是我用来解决问题的解决方案。

由于'Keywords'字段是Unicode,我必须确保在从字段读取信息时它是用Unicode编码的,并且在保存时我需要确保在保存之前将其转换为Unicode。

守则:

Sub Test()

    ''Get SaveLoc from settings
    Dim SaveLoc As String
    SaveLoc = My.Settings.SaveLocation

    ''Select photo to edit/view
    Dim Bitmap As String
    Bitmap = SaveLoc + "\Test\" + "1decfabd90e2355eac81b3f9735e10e3.jpg"

    ''Use ExifWorks library
    Dim EX As New ExifWorks(Bitmap)

    ''Set Encoding to Unicode
    Dim enc As New System.Text.UnicodeEncoding

    ''Get data from Keywords field
    Dim keywords = EX.GetProperty(&H9C9E)

    ''Use Unicode encoding to get the contents currently within Keywords
    Dim decode = enc.GetChars(keywords)

    ''View the current tags, decoded from Unicode
    MsgBox(decode)

    ''Set Encoding to Unicode
    Dim enc2 As Encoding = Encoding.Unicode

    ''Tags to Add
    Dim tags As String = "These; are; testing; tags"

    ''Encode to Unicode and switch to Bytes
    Dim Data() As Byte = enc2.GetBytes(tags & vbNullChar)

    ''Append the Keywords field with the new tags
    EX.SetProperty(&H9C9E, Data, ExifWorks.ExifDataTypes.UnsignedByte)

    ''Save the changes
    EX.GetBitmap.Save(SaveLoc + "\Test\" + "NEW1decfabd90e2355eac81b3f9735e10e3.jpg")

End Sub