使用 IRONBARCODE

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

發佈 2024年3月6日
分享:

在快速發展的科技領域中,條碼掃描器設備已成為各個行業的重要組成部分,涵蓋從零售和物流到醫療保健和製造業的各個方面。Microsoft 的 Visual Basic .NET 是一種靈活且強大的程式設計語言,為開發人員提供了一個強健的框架,用於創建可以直接從攝像頭影片中讀取條碼的應用程式。本文旨在提供使用 Visual Basic 和攝像頭進行條碼讀取的全面教程 IronBarcode 圖書館由 Iron Software

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 的套件管理器安裝 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碼、資料矩陣和128條碼類型的條形碼圖像。首先,我們使用PictureBox從網絡攝像頭或任何攝像設備掃描條形碼來捕捉條形碼圖像。然後,我們創建一個位圖圖像,並將其提供給IronBarcode BarcodeReader類作為輸入。此應用程式從圖像中讀取2D條形碼並解碼。如果解碼後獲得正面結果,則結果會顯示在訊息框中。

授權 (免費試用)

IronBarcode這個密鑰需要放置在 appsettings.json。

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

提供用戶電子郵件以取得試用授權,提交電子郵件後,授權金鑰將通過電子郵件發送。

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

結論

從攝像頭實現條形碼讀取 VB.NET 是一個強大的功能,可以增強各行各業的各種應用程式。通過利用像 IronBarcode 將它們整合到您的 VB.NET 專案中,您可以創建出高效且可靠的條碼掃描應用程式,以滿足現今科技驅動世界的需求。本指南作為起點,開發者可以根據其特定需求、條碼類型和使用案例進一步定制和優化解決方案。

< 上一頁
如何在VB .NET中使用2D條碼
下一個 >
如何在 C# 中生成 Code 128 條形碼

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 1,203,227 查看許可證 >