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

时间:2016-02-10 17:55:11

标签: vb.net winforms zxing

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

enter image description here

添加对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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

您的问题在于您尝试阅读的条形码。不是你如何尝试阅读它们。您需要在code 39上开始和停止字符。在要渲染条形码的每个字符串的开头和结尾添加一个星号*。没有这些字符,您的条形码无效,无法读取。