【问题标题】:read code 39 barcode with vb.net使用 vb.net 读取代码 39 条码
【发布时间】:2016-02-10 17:55:11
【问题描述】:

我对这个项目的目标是使用 VB.net 创建一个 Windows 窗体应用程序,以从图像中读取条形码(现在形成一个文件,稍后用网络摄像头拍摄)并将文本写入文本框。我发现了很多例子,说明如何使用 Zxing 库对除 vb.net 之外的几乎所有语言的条形码进行解码。我有一张包含代码 39(我相信)条形码的图像。

添加对 Zxing.dll 的引用并导入所需的命名空间后,我有这个:

Imports ZXing.OneD
Public Class Form2
    Private webcam As WebCam
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        webcam = New WebCam()
        webcam.InitializeWebCam(imgVideo)
        webcam.Start()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        imgCapture.Image = imgVideo.Image 'I plan to use this later


        Dim reader As New ZXing.OneD.Code39Reader
        Dim image1 As Bitmap
        image1 = Image.FromFile("C:\Capture.jpg") 'this is the image I'm using for testing purposes

        reader.decode(image1)
    End Sub
End Class

reader.decode(image1) 行产生错误:

"Error  1   Value of type 'System.Drawing.Bitmap' cannot be converted to 'ZXing.BinaryBitmap'"

显然,我正在涉足一些我还不明白的事情......所以我正在寻求帮助!我正在使用 Visual Studio 2010 Express。

我又改了一次。此代码不会产生任何错误,但它不会返回任何内容。

Imports ZXing
Imports ZXing.OneD
Public Class Form2
    Private webcam As WebCam
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        webcam = New WebCam()
        webcam.InitializeWebCam(imgVideo)
        webcam.Start()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        imgCapture.Image = imgVideo.Image 'I plan to use this later

        Dim reader As Code39Reader = New Code39Reader
        Dim image1 As Bitmap = New Bitmap("C:\Capture.bmp")
        Dim bitmapBytes As Byte()
        Using stream As New System.IO.MemoryStream
            image1.Save(stream, image1.RawFormat)
            bitmapBytes = stream.GetBuffer
        End Using
        Dim Lumin As LuminanceSource = New RGBLuminanceSource(bitmapBytes, image1.Width, image1.Height, bitmapFormat:=RGBLuminanceSource.BitmapFormat.RGB24)

        Dim HBin As Common.HybridBinarizer = New Common.HybridBinarizer(Lumin)
        Dim Bitm As BinaryBitmap = New BinaryBitmap(HBin)
        Dim res As String = reader.decode(Bitm).Text
    End Sub
End Class

编辑* 这是有效的解决方案。谢谢大家的帮助!

Imports ZXing
Imports ZXing.OneD
Imports System.IO
Public Class Form2
    Private webcam As WebCam
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        webcam = New WebCam()
        webcam.InitializeWebCam(imgVideo)
        webcam.Start()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        imgCapture.Image = imgVideo.Image 'I plan to use this later
        Dim reader As New ZXing.BarcodeReader
        Dim image1 As Bitmap
        image1 = Image.FromFile("C:\Capture.bmp")
        Dim res As Result = reader.Decode(image1)
        MsgBox(res.Text)
    End Sub

    Public Function GetRGBValues(ByVal Bmp As Bitmap) As Byte()
        Dim Rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
        Dim BmpData As System.Drawing.Imaging.BitmapData = Bmp.LockBits(Rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, Bmp.PixelFormat)
        Dim Ptr As IntPtr = BmpData.Scan0
        Dim Bytes As Integer = BmpData.Stride * Bmp.Height
        Dim RgbValues As Byte() = New Byte(Bytes - 1) {}
        System.Runtime.InteropServices.Marshal.Copy(Ptr, RgbValues, 0, Bytes)
        Bmp.UnlockBits(BmpData)
        Return RgbValues
    End Function
End Class

【问题讨论】:

  • 很简单。 reader.decode() 采用 ZXing.BinaryBitmap 类型的参数,但您尝试为其提供 System.Drawing.Bitmap 类型的参数。因此,您必须以某种方式从普通的Bitmap 创建一个ZXing.BinaryBitmap
  • 或许this question 能帮到你?
  • @Visual Vincent - 我已经根据您提供的示例编辑了代码(和我的问题),但仍然没有乐趣
  • 它想要一个什么的一维数组??
  • 见鬼,如果我知道,错误说“'System.Drawing.Bitmap' 类型的值不能转换为'字节的一维数组'。”

标签: vb.net winforms zxing


【解决方案1】:

您的问题在于您尝试读取的条形码。与您尝试阅读它们的方式无关。您需要code 39 上的开始和停止字符。将星号 * 添加到要为其呈现条形码的每个字符串的开头和结尾。如果没有这些字符,您的条形码将无效且无法读取。

【讨论】:

  • 啊,你的意思是像*THIS IS BARCODE*
  • 是的...但看起来您的库也没有对空格进行编码。成品条码不应有空隙。
  • 别跟我说话,跟 user3479671 说话。 :)
  • 大声笑......对不起,我以为你是 OP :)......我没有读到我刚刚回复你的评论的名字。
  • 没关系,我注意了。我将图像更改为读取 effingwork 的条形码,但它仍然没有返回任何内容
【解决方案2】:

所以RGBLuminanceSource 在其构造函数中需要一个字节数组。

我似乎找不到合适的文档,但这会将Image 转换为Bytes 的数组:

Public Function ImageToByteArray(ByVal Img As Image) As Byte()
    Using ms As New MemoryStream
        Img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
        Return ms.ToArray()
    End Using
End Function

调用示例:

Dim Lumin As New ZXing.RGBLuminanceSource(ImageToByteArray(image1), image1.Width, image1.Height)

如果上面的方法不起作用,还有这个函数只需要图像的像素(我不确定上面是否只需要像素):

Public Function GetRGBValues(ByVal Bmp As Bitmap) As Byte()
    Dim Rect As New Rectangle(0, 0, bmp.Width, bmp.Height)
    Dim BmpData As System.Drawing.Imaging.BitmapData = Bmp.LockBits(Rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, Bmp.PixelFormat)

    Dim Ptr As IntPtr = BmpData.Scan0

    Dim Bytes As Integer = BmpData.Stride * Bmp.Height
    Dim RgbValues As Byte() = New Byte(Bytes - 1) {}

    System.Runtime.InteropServices.Marshal.Copy(Ptr, RgbValues, 0, Bytes)
    Bmp.UnlockBits(BmpData)

    Return RgbValues
End Function

【讨论】:

  • 它使用 GetRGBValues 执行没有错误,但在“Dim res As Result = reader.decode(Bitm)”行之后,res 变量什么也不返回
  • @user3479671 :那么我很抱歉地说我认为我无能为力了。我从来没有使用过这个库或类似的东西,我也没有更多的想法。
  • 感谢您的努力。如果这确实导致了解决方案,那么我一定会接受它作为答案。
  • @user3479671 :我更确定马修的回答会,但谢谢。 :)
猜你喜欢
  • 1970-01-01
  • 2016-05-03
  • 1970-01-01
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多