跳至页脚内容
USING IRONBARCODE

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 Iron Software.

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. It is able to scan a maximum of 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

  1. Create a new VB.NET Project in Visual Studio
  2. Install the IronBarcode Library and apply it to your project
  3. Get the barcode from the camera as an image using the AForge Library
  4. Decode the barcode image using IronBarcode

Prerequisites

  1. Visual Studio: Ensure you have Visual Studio or any other VB.NET development environment installed.
  2. Compatible Camera: Ensure said camera is connected to your device.
  3. 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 Forms application (or use an existing project) where you want to host the code to read the barcode from your camera.

How to Read a Barcode from Camera in VB .NET: Figure 1 - Create new VB.NET Windows form application

In the next step, you can provide the solution and project names.

How to Read a Barcode from Camera in VB .NET: Figure 2 - Configuring the project with the name and solution

Select the .NET version and click the "Create" button.

Step 2: Install the IronBarcode Library

Open your VB.NET project and install the IronBarcode library using the NuGet Package Manager Console:

Install-Package BarCode

How to Read a Barcode from Camera in VB .NET: Figure 3 - Installing the NuGet IronBarcode package

The NuGet package can also be installed using Visual Studio's NuGet Package Manager, as shown below.

How to Read a Barcode from Camera in VB .NET: Figure 4 - Installing IronBarcode through Visual Studio's Package Manager

Step 3: 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.

How to Read a Barcode from Camera in VB .NET: Figure 5 - AForge library package's that are found in Visual Studio Package Manager

The next step is to add the PictureBox control from the ToolBox to the forms. This is used to capture the image from the camera.

How to Read a Barcode from Camera in VB .NET: Figure 6 - Adding the PictureBox control

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

    ' Event handler for form load
    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

    ' Event handler for capturing and processing new frame from the video source
    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

    ' Method to display the barcode result
    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 value in a MessageBox or any other UI element
            MessageBox.Show("Barcode Value: " & barcodeValue, "Barcode Detected")
        End If
    End Sub

    ' Event handler for form closing
    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

    ' Event handler for form load
    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

    ' Event handler for capturing and processing new frame from the video source
    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

    ' Method to display the barcode result
    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 value in a MessageBox or any other UI element
            MessageBox.Show("Barcode Value: " & barcodeValue, "Barcode Detected")
        End If
    End Sub

    ' Event handler for form closing
    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
VB .NET

In this sample code, we have configured it to read QR codes and Code 128 barcodes. First, we use a 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)

To use IronBarcode, you need to place a license key in your appsettings.json.

{
    "IronBarcode.LicenseKey": "MYLICENSE.KEY.TRIAL"
}

Provide your email ID to get a trial license, and after submitting the email ID, the key will be delivered via email.

How to Read a Barcode from Camera in VB .NET: Figure 7 - Popup after successfully submitting a trial form

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.

常见问题解答

如何使用 VB.NET 从相机读取条码?

要在 VB.NET 中从相机读取条码,您可以使用 IronBarcode 库解码从相机视频流捕获的图像。首先,在 Visual Studio 中设置一个 VB.NET 项目,通过 NuGet 安装 IronBarcode,并使用 AForge 库管理相机输入。

设置 VB.NET 中的条码读取器项目涉及哪些步骤?

首先在 Visual Studio 中创建一个新的 VB.NET Windows 窗体应用程序。使用 NuGet 安装 IronBarcode 库,并配置一个 PictureBox 来从相机捕获图像。使用 AForge 库处理摄像头视频流,并使用 IronBarcode 解码条码。

如何在 VB.NET 应用程序中集成摄像头捕获功能?

您可以使用 AForge 库访问和管理相机视频流,在 VB.NET 应用程序中集成摄像头捕获功能。这些视频流然后可以被处理以捕获图像用于使用 IronBarcode 解码条码。

在 VB.NET 项目中,可以使用 IronBarcode 解码哪些条码类型?

IronBarcode 支持在 VB.NET 项目中解码多种条码类型,包括 QR 码和 Code 128。该库十分灵活,可以配置以识别不同的条码格式。

开发一个 VB.NET 条码扫描应用程序需要哪些必要组件?

要开发一个 VB.NET 条码扫描应用程序,您需要 Visual Studio、兼容的相机、通过 NuGet 安装的 IronBarcode 库以及处理相机输入的 AForge 库。

如何排除使用 VB.NET 从相机读取条码时的常见问题?

确保您的相机已正确连接并被系统识别。验证 IronBarcode 和 AForge 库是否正确安装,并确保您的应用程序具有访问相机视频流的权限。检查代码语法和库引用是否有错误。

在 VB.NET 应用程序中显示条码扫描结果的过程是什么?

一旦使用 IronBarcode 解码了条码,您可以通过在 UI 组件如 MessageBox 或 Label 中显示结果,将条码数据呈现给用户。

我可以在购买前试用条码库吗,以及如何获取试用版?

是的,您可以通过从 Iron Software 网站获得试用许可密钥来试用条码库。提交您的电子邮件地址,您将在邮件中收到试用密钥,用于在您的 VB.NET 项目中使用。

Jordi Bardia
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。