使用 vb.net 读取代码 39 条形码

read code 39 barcode with vb.net

我的这个项目的目标是创建一个 windows 表单应用程序,使用 VB.net 从图像中读取条形码(现在形成一个文件,稍后使用网络摄像头拍摄)并编写文本到文本框。我发现了很多关于如何使用 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。

我又改了。此代码不会产生任何错误,但它 returns 什么也没有。

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

因此 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

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