跳至頁尾內容
使用 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讀取相機中的條碼?

若要在 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 專案中使用。

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。