使用IRONBARCODE

如何在VB .NET中使用摄像头读取条形码

发布 2024年三月6日
分享:

在技术飞速发展的今天,条形码扫描仪设备已成为从零售、物流到医疗保健和制造业等各行各业不可或缺的一部分。Microsoft 的 Visual Basic .NET 是一种通用且功能强大的编程语言,它为开发人员提供了一个强大的框架,用于创建可直接从摄像头馈送中读取条形码的应用程序。本文旨在提供一个全面的条形码阅读器教程,在 Visual Basic 中使用摄像头读取条形码。 IronBarcode铁软件

通过 IronBarcode 库,您可以读取条形码图像文件,也可以读取从摄像头传输的条形码流。它还支持从 PDF 文档中读取条码。一次最多能扫描一个条形码。在 VB.NET 条码阅读器 SDK 中读取条码图像时需要指定条码类型。

如何在 VB .NET 中从相机读取条形码

1.在 Visual Studio 中创建新的 VB.NET 项目

2.安装 IronBarcode 库并将其应用到您的项目中

3.从 Aforge 库中获取相机中的条形码图像

4.使用 IronBarcode 解码条码图像

先决条件

  1. Visual Studio: 确保已安装 Visual Studio 或任何其他 VB.NET 开发环境。

  2. 兼容摄像头: 确保所述摄像头已连接至设备

  3. NuGet软件包管理器: 确保可以使用NuGet管理项目中的软件包

步骤 1:在 Visual Studio 中创建一个新的 Visual Basic .NET 项目

创建一个新的 VB.NET Windows 窗体应用程序 (或使用现有项目) 在此处输入代码,以便从相机读取条形码。

如何在 VB .NET 中从相机读取条形码:图 1 - 创建新的 VB.NET Windows 窗体应用程序

在 "下一步 "中,您可以提供解决方案和项目名称。

如何在 VB .NET 中从相机读取条形码:图 2 - 用名称和解决方案配置项目

选择 .NET 版本,然后点击 "创建 "按钮。

第 2 步:安装 IronBarcode 库

打开 C# 项目,使用 NuGet 软件包管理器控制台:

Install-Package BarCode

如何在 VB .NET 中从相机读取条形码:图 3 - 安装 NuGet IronBarcode 软件包

还可以使用 Visual Studio 的 NuGet 包管理器安装 NuGet 包,如下所示。

如何在 VB .NET 中从相机读取条形码:图 4 - 通过 Visual Studio's 软件包管理器安装 IronBarcode

第三步:从摄像头读取条形码

要扫描饲料并从摄像头捕捉图像,我们需要 AForge 库。从 NuGet 软件包管理器安装如下

如何在 VB .NET 中从相机读取条形码:图 5 - 在 Visual Studio 软件包管理器中找到的 AForge 库软件包

下一步是将工具箱中的 PictureBox 控件添加到表单中。该控件用于捕捉相机中的图像。

如何在 VB .NET 中从相机读取条形码:图 6 - 添加 PictureBox 控件

然后将以下代码复制到窗体应用程序中,并从 IronBarcode 中创建 VB .NET 条码阅读器组件。

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
VB.NET

在本示例代码中,我们将其配置为读取 QR 码、数据矩阵和 Code 128 条形码类型的条形码图像。首先,我们使用 PictureBox 通过扫描条形码从网络摄像头或任何摄像设备捕获条形码图像。然后,我们创建一个位图图像。然后将其作为输入提供给 IronBarcode BarcodeReader 类。该应用程序从图像中读取二维条形码并进行解码。如果解码后得到了肯定的结果,那么结果就会显示在消息框中。

许可 (可免费试用)

IronBarcode.此密钥需要放在 appsettings.json 中。

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

提供用户电子邮件 ID 以获取试用许可证,提交电子邮件 ID 后,密钥将通过电子邮件发送。

如何在 VB .NET 中从相机读取条形码:图 7 - 成功提交试用表单后的弹出窗口

结论

VB.NET 是一项强大的功能,可以增强不同行业的各种应用。通过利用 IronBarcode 使用本指南,并将其集成到您的 VB.NET 项目中,您就可以创建高效可靠的条形码扫描应用程序,以满足当今技术驱动型世界的需求。本指南可作为一个起点,开发人员可根据其具体要求、条形码类型和使用案例进一步定制和优化解决方案。

< 前一页
如何在VB .NET中使用二维条形码
下一步 >
如何在C#中生成Code 128条形码

准备开始了吗? 版本: 2024.8 刚刚发布

免费NuGet下载 总下载量: 1,167,541 查看许可证 >