在 Access

问题描述

我有我的平装书收藏的 Access 数据库,该数据库使用 VB 从免费在线服务下载和保存条形码文件。该脚本将它们保存为 .png 文件,并在我的表单和报告中将它们显示为图像对象(我认为如果我的书籍丢失、被盗或毁坏,使用 ISBN 和/或 UPC 代码来识别书籍可以帮助确定损失的价值)。

网站无法生成特定的条形码 - 但我可以通过下载两个条形码图像,裁剪一个,将其附加到第二个图像,然后将结果保存为第三个图像来自己制作。之后,可以丢弃(删除)下载的图像,并且可以将生成的合成图像用作我的 Access 表单和报告中的图像对象。

ImageA.png 的大小为 688 x 209 像素(它被称为 ISBN 13+5 条码,左侧是 13 位 EAN 样式的条码,右侧是 5 位价格条码)。

ImageB.png 的大小为 471 x 209 像素(这是标准的 12 位 UPC-A 条形码,可在从 DVD 到洗手液分配器、威士忌酒瓶到汽车电池的所有物品上找到)。

我想要在 VB 中做的是从 ImageA 的左侧裁剪 450 像素,有效地从图像中剥离 EAN 代码,然后将剩下的 238 x 209 像素保存为 ImageA.png 或 TempA.png ...然后我想将其附加到 ImageB.png 的右侧并将组合图像保存为 ImageC.png(709 x 209 像素)。组合操作有效地获取“EAN + 5”条码图像,去除 EAN 代码,然后将其替换为 UPC 代码

通过查看以下内容可能更容易理解我想要做什么:Step-by-step graphical example

保存 ImageC.png 后,可以删除 ImageA.png 和/或 TempA.png 和 ImageB.png。因为我真正想要的是 ImageC.png。

我希望我能提供示例代码,但我到处都能看到显示图片框或需要鼠标移动的代码示例 - 下载的图像始终大小相同,所以我只想加载和裁剪图像不显示它...然后将其附加到第二张图像并保存...然后我想显示生成的合成图像。

感谢您抽出宝贵时间。

解决方法

我想我明白了...

    ShortISBN = Mid((LongISBNString),5,5)        'sets 5-digit string for filename
    ShortISBNFile = [ShortISBN] & ".png"                      'sets the new filename
    Set IP = CreateObject("WIA.ImageProcess")                    'create WIA objects
    Set Img = CreateObject("WIA.ImageFile")
    Img.LoadFile CurrentProject.Path & "\temp.png"            'load downloaded image
    IP.Filters.Add IP.FilterInfos("Crop").FilterID                'setup crop filter
    With IP.Filters(1)
        .Properties("Left") = 450
    End With
    Set Img = IP.Apply(Img)                                            'apply change
    Img.SaveFile CurrentProject.Path & [ShortISBNFile] 'save image with new filename
    Kill CurrentProject.Path & "\temp.png"                  'delete downloaded image

至于合并两个图像,https://www.vbforums.com/showthread.php?725361-RESOLVED-merge-two-scanned-images-into-one 似乎有答案,但是代码格式不同,并且在我将其逐字复制并粘贴到我的项目中时出错...重新格式化代码以反映裁剪代码中使用的格式,这似乎更正了该错误:

    Dim oIF1 As Object,oIF2 As Object,oVectNew As Object,oIFNew As Object
    Dim oIP As Object,TempCodeFile As String,lPixelsWide As Long,lPixelsHigh As Long
    ShortISBN = Mid([ISBN Number],7,5)
    ShortISBNFile = Mid([ISBN Number],5) & ".png"
    Set oIF1 = CreateObject("WIA.ImageFile")
    With oIF1
        .LoadFile CurrentProject.Path & [UPCCodeFile]
        lPixelsWide = .Width
        lPixelsHigh = .Height
    End With
    Set oIF2 = CreateObject("WIA.ImageFile")
    With oIF2
        .LoadFile CurrentProject.Path & [ShortISBNFile]
        lPixelsWide = lPixelsWide + .Width
    End With
    With New BmpGen                    'This is the Class Module from the above link 
         Set oVectNew = CreateObject("WIA.Vector")
         oVectNew.BinaryData = .MakeMono(BG_COLOR,lPixelsWide,lPixelsHigh)
    End With
    Set oIFNew = oVectNew.ImageFile
    Set oVectNew = Nothing
    Set oIP = CreateObject("WIA.ImageProcess") 'create WIA objects        
    With oIP
        oIP.Filters.Add oIP.FilterInfos("Stamp").FilterID
        With oIP.Filters(1)            
            Set .Properties("ImageFile") = oIF1
            .Properties("Left") = 0
            .Properties("Top") = 0
        End With        
        oIP.Filters.Add oIP.FilterInfos("Stamp").FilterID
        With oIP.Filters(2)
            Set .Properties("ImageFile") = oIF2
            .Properties("Left") = oIF1.Width
            .Properties("Top") = 0
        End With
        Set oIFNew = oIP.Apply(oIFNew)
    End With
    TempCodeFile = left([UPCCodeFile],15) & " " & [ShortISBNFile]
    UPCCodeFile = TempCodeFile
    oIFNew.SaveFile CurrentProject.Path & [UPCCodeFile]
    Kill CurrentProject.Path & [ShortISBNFile]

我应该在代码生成过程中进行一些错误检查(如果 UPC 代码字符串太短或太长怎么办?)但最终当你真的不知道自己在做什么时,你能做些什么真是太棒了.