跳過到頁腳內容
使用IRONBARCODE

如何從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 Forms 應用程式(或使用現有專案),在您希望將從相機讀取條碼的代碼託管的地方。

如何在 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 從攝影機讀取 BarCode?

若要在 VB.NET 中從攝影機讀取條碼,您可以使用 IronBarcode 函式庫來解碼從攝影機擷取的影像。首先,在 Visual Studio 中建立一個 VB.NET 專案,透過 NuGet 安裝 IronBarcode,並使用 AForge 函式庫來管理攝影機的輸入。

在 VB.NET 中建立 BarCode 閱讀器專案需要哪些步驟?

首先在 Visual Studio 中建立一個新的 VB.NET Windows Forms 應用程式。使用 NuGet 安裝 IronBarcode 函式庫,並配置一個 PictureBox 來擷取相機中的影像。使用 AForge 函式庫來處理攝影機饋送、並使用 IronBarcode 來解碼條碼。

如何在 VB.NET 應用程式中整合攝影機擷取功能?

您可以在 VB.NET 應用程式中整合攝影機擷取功能,方法是使用 AForge 函式庫來存取和管理攝影機訊號來源。然後,可以使用 IronBarcode 來處理這些饋送以擷取影像進行條碼解碼。

在 VB.NET 專案中,哪些條碼類型可以使用 IronBarcode for .NET 解碼?

IronBarcode for .NET 支援在 VB.NET 專案中解碼多種條碼類型,包括 QR code 和 Code 128。該程式庫用途廣泛,可配置為識別不同的條碼格式。

在 VB.NET 中開發 BarCode 掃描應用程式的必要元件為何?

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

在 VB.NET 中從攝影機讀取 BarCode 時,如何排除常見問題?

確保您的攝影機已正確連接並能被系統辨識。確認 IronBarcode 和 AForge 函式庫已正確安裝,且您的應用程式可存取攝影機訊源。檢查程式碼的語法和函式庫參考是否有錯誤。

在 VB.NET 應用程式中顯示 BarCode 掃描結果的流程為何?

一旦使用 IronBarcode 解碼了條碼,您就可以在您的 VB.NET 應用程式中顯示結果,方法是在 MessageBox 或 Label 等 UI 元件中顯示,將條碼資料呈現給使用者。

在購買之前,我是否可以試用 BarCode 程式庫,以及如何取得試用版?

是的,您可以從 Iron Software 網站取得試用授權金鑰,試用條碼函式庫。提交您的電子郵件 ID,您將透過電子郵件收到試用金鑰,以便在您的 VB.NET 專案中使用。

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