跳過到頁腳內容
使用IRONBARCODE

如何從VB .NET中的攝像頭讀取條碼

在科技快速發展的環境中,條碼掃描器設備已成為從零售業和物流業到醫療保健和製造業等各種行業中不可或缺的一部分。 來自 Microsoft 的 Visual Basic .NET 是一種多功能且強大的編程語言,為開發人員提供了一個穩健的框架,以創建可以直接從相機饋送中讀取條碼的應用程式。 This article aims to provide a comprehensive barcode reader tutorial using a camera in Visual Basic using the IronBarcode library from Iron Software.

IronBarcode 程式庫允許您讀取條碼圖像文件,並且在從相機串流時也能實現。 它還支持從 PDF 文件中讀取條碼。 它能夠一次最多掃描一個條碼。在以 VB.NET 條碼讀取 SDK 讀取條碼圖像時,需要指定條碼類型。

如何在 VB .NET 中從相機讀取條碼

  1. 在 Visual Studio 中創建一個新的 VB.NET 專案
  2. 安裝 IronBarcode 程式庫並將其應用於您的專案
  3. 使用 AForge Library 從相機獲取條碼圖像
  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 程式庫

打開您的 VB.NET 專案並使用 NuGet 包管理器控制台安裝 IronBarcode 程式庫:

Install-Package BarCode

如何在 VB .NET 中從相機讀取條碼:圖 3 - 安裝 NuGet IronBarcode 包

也可以通過 Visual Studio 的 NuGet 包管理器安裝 NuGet 包,如下所示。

如何在 VB .NET 中從相機讀取條碼:圖 4 - 通過 Visual Studio 的包管理器安裝 IronBarcode

步驟 3: 從相機讀取條碼

要掃描饋入並從相機捕獲圖像,我們需要 AForge Library。 從 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

    ' 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

在此示例代碼中,我們已將其配置為讀取 QR 代碼和 Code 128 條碼。 首先,我們使用 PictureBox 從網絡攝像頭或任何相機設備中捕獲條碼圖像,通過掃描條碼。 然後,我們創建一個位圖圖像,然後將其作為輸入提供給 IronBarcode 的 BarcodeReader 類。 此應用程式從圖像中讀取 2D 條碼並解碼它們。 如果在解碼後獲得正面結果,則結果會顯示在消息框中。

授權(免費試用可用)

要使用 IronBarcode,您需要在 appsettings.json 中放置授權密鑰。

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

提供您的電子郵件 ID 以獲取試用授權,提交電子郵件 ID 後,密鑰將通過電子郵件發送。

如何在 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 專案中解碼多種條碼類型,包括二維碼和 Code 128 條碼。此庫功能強大,可配置為識別不同的條碼格式。

在VB.NET中開發條碼掃描應用程式需要哪些必要的元件?

要在 VB.NET 中開發條碼掃描應用程序,您需要 Visual Studio、相容的相機、透過 NuGet 安裝的 IronBarcode 庫以及用於處理相機輸入的 AForge 庫。

如何在VB.NET排查從攝影機讀取條碼時遇到的常見問題?

請確保您的攝影機已正確連接並被系統識別。確認 IronBarcode 和 AForge 庫已正確安裝,並且您的應用程式可以存取攝影機視訊串流。檢查程式碼語法和函式庫引用是否有錯誤。

在VB.NET應用程式中顯示條碼掃描結果的過程是怎樣的?

使用 IronBarcode 解碼條碼後,您可以在 VB.NET 應用程式中透過 UI 元件(例如 MessageBox 或 Label)顯示結果,從而將條碼資料呈現給使用者。

我可以在購買前試用條碼庫嗎?如何獲得試用版?

是的,您可以從 Iron Software 網站取得試用許可證金鑰來試用條碼庫。提交您的電子郵件地址,您將透過電子郵件收到試用金鑰,即可在您的 VB.NET 專案中使用。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。