Published March 7, 2024
How to Read a Barcode from Camera in VB .NET
In the rapidly evolving landscape of technology, barcode scanner devices have become an integral part of various industries, ranging from retail and logistics to healthcare and manufacturing. Visual Basic .NET from Microsoft, a versatile and powerful programming language, provides developers with a robust framework for creating applications that can read barcodes directly from a camera feed. This article aims to provide a comprehensive barcode reader tutorial using a camera in Visual Basic using the IronBarcode library from IronSoftware.
IronBarcode library allows you to read barcode image files and also when streamed from cameras. It also supports the reading of barcodes from a PDF document. Able to scan a maximum one barcode at a time. The barcode type needs to be specified at the time of reading the barcode image in the VB.NET barcode reader SDK.
How to Read a Barcode from Camera in VB .NET
- Create a new VB.NET Project in Visual Studio
- Install the IronBarcode Library and apply it to your project
- Get the barcode from the camera as an image from the Aforge Library
- Decode the barcode image using IronBarcode
Prerequisites
Visual Studio: Ensure you have Visual Studio or any other VB.NET development environment installed.
Compatible Camera: Ensure said camera is connected to your device
- NuGet Package Manager: Make sure you can use NuGet to manage packages in your project
Step 1: Create a New Visual Basic .NET Project in Visual Studio
Create a new VB.NET Windows form application (or use an existing project) where you want to host the code to read the barcode from your camera.
In The Next Step, you can provide the solution and project names.
Select the .NET version and click the "Create" button.
Step 2: Install the IronBarcode Library
Open your C# project and install the IronBarcode library using the NuGet Package Manager Console:
Install-Package BarCode -Version 2024.2.1
Install-Package BarCode -Version 2024.2.1
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package BarCode -Version 2024.2.1
The NuGet package can also be installed using Visual Studio's NuGet Package Manager, as shown below.
Step3: Reading the Barcode from the Camera
To scan the feed and capture the image from the camera, we need the AForge Library. Install it as below from the NuGet package manager
The next step is to add the PictureBox control from ToolBox to the forms. This is used to capture the image from the camera.
Then copy the below code to the forms application and create the VB .NET barcode reader component from IronBarcode.
Imports IronBarCode
Imports AForge.Video
Imports AForge.Video.DirectShow
Public Class Form1
Private videoDevices As FilterInfoCollection
Private videoSource As VideoCaptureDevice
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
If videoDevices.Count > 0 Then
videoSource = New VideoCaptureDevice(videoDevices(0).MonikerString)
AddHandler videoSource.NewFrame, AddressOf VideoSource_NewFrame
videoSource.Start()
Else
MessageBox.Show("No video devices found.")
Close()
End If
End Sub
Private Sub VideoSource_NewFrame(sender As Object, eventArgs As NewFrameEventArgs)
pictureBoxCamera.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
' Process each frame for barcode recognition
Dim image = DirectCast(pictureBoxCamera.Image, Bitmap)
Dim result = BarcodeReader.QuicklyReadOneBarcode(image, BarcodeEncoding.QRCode Or BarcodeEncoding.Code128)
If result IsNot Nothing Then
' Barcode found, handle the new result (e.g., display the barcode value)
Dim barcodeValue As String = result.Text
ShowBarcodeResult(barcodeValue)
End If
End Sub
Private Sub ShowBarcodeResult(barcodeValue As String)
' Invoke on UI thread to update UI controls
If InvokeRequired Then
Invoke(New Action(Of String)(AddressOf ShowBarcodeResult), barcodeValue)
Else
' Display the barcode font value in a MessageBox or any other UI element
MessageBox.Show("Barcode Value: " & barcodeValue, "Barcode Detected")
End If
End Sub
Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If videoSource IsNot Nothing AndAlso videoSource.IsRunning Then
videoSource.SignalToStop()
videoSource.WaitForStop()
End If
End Sub
End Class
Imports IronBarCode
Imports AForge.Video
Imports AForge.Video.DirectShow
Public Class Form1
Private videoDevices As FilterInfoCollection
Private videoSource As VideoCaptureDevice
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
videoDevices = New FilterInfoCollection(FilterCategory.VideoInputDevice)
If videoDevices.Count > 0 Then
videoSource = New VideoCaptureDevice(videoDevices(0).MonikerString)
AddHandler videoSource.NewFrame, AddressOf VideoSource_NewFrame
videoSource.Start()
Else
MessageBox.Show("No video devices found.")
Close()
End If
End Sub
Private Sub VideoSource_NewFrame(sender As Object, eventArgs As NewFrameEventArgs)
pictureBoxCamera.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
' Process each frame for barcode recognition
Dim image = DirectCast(pictureBoxCamera.Image, Bitmap)
Dim result = BarcodeReader.QuicklyReadOneBarcode(image, BarcodeEncoding.QRCode Or BarcodeEncoding.Code128)
If result IsNot Nothing Then
' Barcode found, handle the new result (e.g., display the barcode value)
Dim barcodeValue As String = result.Text
ShowBarcodeResult(barcodeValue)
End If
End Sub
Private Sub ShowBarcodeResult(barcodeValue As String)
' Invoke on UI thread to update UI controls
If InvokeRequired Then
Invoke(New Action(Of String)(AddressOf ShowBarcodeResult), barcodeValue)
Else
' Display the barcode font value in a MessageBox or any other UI element
MessageBox.Show("Barcode Value: " & barcodeValue, "Barcode Detected")
End If
End Sub
Private Sub MainForm_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If videoSource IsNot Nothing AndAlso videoSource.IsRunning Then
videoSource.SignalToStop()
videoSource.WaitForStop()
End If
End Sub
End Class
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'@Imports IronBarCode @Imports AForge.Video @Imports AForge.Video.DirectShow @Public @Class Form1 @Private videoDevices @As FilterInfoCollection @Private videoSource @As VideoCaptureDevice @Private @Sub MainForm_Load(sender @As Object, e @As EventArgs) @Handles @MyBase.Load videoDevices = @New FilterInfoCollection(FilterCategory.VideoInputDevice) @If videoDevices.Count > 0 @Then videoSource = @New VideoCaptureDevice(videoDevices(0).MonikerString) @AddHandler videoSource.NewFrame, @AddressOf VideoSource_NewFrame videoSource.Start() @Else MessageBox.Show("No video devices found.") Close() @End @If @End @Sub @Private @Sub VideoSource_NewFrame(sender @As Object, eventArgs @As NewFrameEventArgs) pictureBoxCamera.Image = @DirectCast(eventArgs.Frame.Clone(), Bitmap) ' Process @each frame for barcode recognition @Dim image = @DirectCast(pictureBoxCamera.Image, Bitmap) @Dim result = BarcodeReader.QuicklyReadOneBarcode(image, BarcodeEncoding.QRCode @Or BarcodeEncoding.Code128) @If result @IsNot @Nothing @Then ' Barcode found, handle the New result(e.g., display the barcode value) @Dim barcodeValue @As String = result.Text ShowBarcodeResult(barcodeValue) @End @If @End @Sub @Private @Sub ShowBarcodeResult(barcodeValue @As String) ' Invoke on UI thread @to update UI controls @If InvokeRequired @Then Invoke(@New Action(@Of String)(@AddressOf ShowBarcodeResult), barcodeValue) @Else ' Display the barcode font value in a MessageBox @or any other UI element MessageBox.Show("Barcode Value: " & barcodeValue, "Barcode Detected") @End @If @End @Sub @Private @Sub MainForm_FormClosing(sender @As Object, e @As FormClosingEventArgs) @Handles @MyBase.FormClosing @If videoSource @IsNot @Nothing @AndAlso videoSource.IsRunning @Then videoSource.SignalToStop() videoSource.WaitForStop() @End @If @End @Sub @End @Class
In this sample code, we have configured it to read QR code, data matrix, and barcode images with code 128 barcode type. First, we use PictureBox to capture barcode images from a webcam or any camera device by scanning the barcode. Then we create a bitmap image. Which is then provided as input to the IronBarcode BarcodeReader class. This application reads the 2D barcode from images and decodes them. If a positive result is obtained after decoding, then the result is displayed in the message box.
Licensing (Free Trial Available)
IronBarcode. This key needs to be placed in appsettings.json.
{
"IronBarcode.LicenseKey":"MYLICENSE.KEY.TRIAL"
}
{
"IronBarcode.LicenseKey":"MYLICENSE.KEY.TRIAL"
}
If True Then
"IronBarcode.LicenseKey":"MYLICENSE.KEY.TRIAL"
End If
Provide the user email ID to get a trial license and after submitting the email ID, the key will be delivered via email.
Conclusion
Implementing barcode reading from a camera in VB.NET is a powerful feature that can enhance various applications across different industries. By leveraging libraries like IronBarcode and integrating them into your VB.NET project, you can create efficient and reliable barcode scanning applications that meet the demands of today's technology-driven world. This guide serves as a starting point, and developers can further customize and optimize the solution based on their specific requirements, barcode types, and use cases.