跳至页脚内容
USING IRONBARCODE

How to Read a Barcode from Camera in VB .NET

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

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.兼容的摄像头:确保所述摄像头已连接到您的设备。
  2. NuGet 包管理器:确保您可以使用 NuGet 管理项目中的包。

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

创建一个新的 VB.NET Windows 窗体应用程序(或使用现有项目),在其中承载从相机读取条码的代码。

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

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

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

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

步骤 2:安装 IronBarcode 库

打开您的 VB.NET 项目,并使用NuGet程序包管理器控制台安装 IronBarcode 库:

Install-Package BarCode

如何在 VB.NET 中从摄像头读取条形码:图 3 - 安装 NuGet IronBarcode 包

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

如何在 VB.NET 中从摄像头读取条形码:图 4 - 通过 Visual Studio 的包管理器安装 IronBarcode

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

要扫描视频流并从摄像头捕获图像,我们需要 AForge 库。 请按照以下步骤从 NuGet 包管理器安装。

如何在 VB.NET 中从摄像头读取条形码:图 5 - Visual Studio 包管理器中提供的 AForge 库包

下一步是将 ToolBox 中的 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

    ' 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

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

许可(提供免费试用)

要使用IronBarcode ,您需要在appsettings.json中放置一个许可证密钥。

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

提供您的电子邮件地址以获取试用许可证,提交电子邮件地址后,密钥将通过电子邮件发送给您。

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

结论

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

常见问题解答

如何使用 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 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。