使用 IRONBARCODE

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

在快速變化的科技領域中,條碼掃描設備已成為各種行業的重要組成部分,從零售和物流到醫療保健和製造業。 來自微軟的 Visual Basic .NET 是一種多功能且強大的編程語言,為開發人員提供了一個強大的框架,用於創建可以直接從攝像頭影像讀取條碼的應用程序。 本文旨在提供使用相機和 Iron SoftwareIronBarcode 庫在 Visual Basic 中的全面條碼閱讀器教程。

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 套件管理器主控台安裝 IronBarcode 函式庫:

Install-Package BarCode

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

如下面所示,也可以使用 Visual Studio 的 NuGet 套件管理器來安裝 NuGet 套件。

如何透過 VB .NET 使用相機讀取條碼:圖 4 - 透過 Visual Studio 的套件管理員安裝 IronBarcode

步驟3:從相機讀取條碼

要掃描供稿並從相機捕獲圖像,我們需要 AForge 庫。 從 NuGet 套件管理器安裝如下

如何在 VB .NET 中從相機讀取條碼:圖 5 - Visual Studio 套件管理器中的 AForge 庫包

接下來的步驟是從工具箱將 PictureBox 控制項添加到表單中。 這用於從相機捕獲圖像。

如何在 VB .NET 中從相機讀取條碼:圖 6 - 添加 PictureBox 控件

然後將以下代碼複製到 Forms 應用程序中,並從 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 碼、數據矩陣和具有代碼 128 條形碼類型的條形碼圖像。 首先,我們使用 PictureBox 從網路攝影機或任何攝像裝置掃描條碼以捕獲條碼圖像。 然後我們創建一個位圖圖像。 然後將其作為輸入提供給IronBarcode BarcodeReader類。 此應用程式從圖像中讀取二維條碼並解碼。 如果在解碼後獲得正結果,則結果將顯示在消息框中。

授權(免費試用可用)

IronBarcode。 此鍵需要放置在 appsettings.json 中。

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

提供用戶電子郵件地址以獲取試用許可證,提交電子郵件地址後,許可證密鑰將通過電子郵件發送。

如何在 VB .NET 中從相機讀取條碼:圖 7 - 成功提交試用表單後的彈出視窗

結論

VB.NET中實現從相機讀取條碼是一種強大的功能,可以提高各個行業中應用程式的效能。 通過利用類似IronBarcode的庫並將其整合到您的VB.NET專案中,您可以創建高效和可靠的條碼掃描應用程式,以滿足當今技術驅動世界的需求。 本指南作為起點,開發人員可以根據他們的具體需求、條碼類型和使用案例進一步自訂和優化解決方案。

喬迪·巴迪亞
軟體工程師
Jordi 最擅長 Python、C# 和 C++,當他不在 Iron Software 發揮技能時,他會進行遊戲編程。他負責產品測試、產品開發和研究,為持續產品改進增添了巨大的價值。多樣化的經驗使他感到挑戰和投入,他說這是與 Iron Software 合作的最喜歡的方面之一。Jordi 在佛羅里達州邁阿密長大,並在佛羅里達大學學習計算機科學和統計學。
< 上一頁
如何在VB .NET中使用2D條碼
下一個 >
如何在 C# 中生成 Code 128 條形碼