My goal with this project is to create a windows form application using VB.net to read a barcode from an image (form a file for now, taken with a webcam later) and write the text to a textbox. I've found plenty of examples of how to decode bar codes using the Zxing library for pretty much every language except vb.net. I have an image which contains a code 39 (I believe) bar code.
After adding a reference to Zxing.dll and importing the desired namespace, I have this:
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
the line reader.decode(image1)
generates an error:
"Error 1 Value of type 'System.Drawing.Bitmap' cannot be converted to 'ZXing.BinaryBitmap'"
Clearly, I'm dabbling with things I don't yet understand... so I'm begging for help! I'm using Visual Studio 2010 Express.
I've changed it yet again. this code generates no error, yet it returns nothing.
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
EDIT* This is the working solution. Thanks for the help guys!
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
Your problem is with the barcodes you are trying to read. Not with how you are trying to read them. You need start and stop characters on code 39. Add an asterisk *
to the beginning and end of each string you are rendering barcodes for. Without these characters your barcodes are invalid and can not be read.