跳至頁尾內容
使用IRONBARCODE

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

在科技迅速演變的環境中,條碼掃瞄器裝置已成為各行業的重要組成部分,涵蓋零售、物流、醫療保健到製造業。 Microsoft 的 Visual Basic .NET 是一種多功能且強大的程式語言,為開發者提供了一個堅固的框架以建立可以直接從相機供應中讀取條碼的應用程式。 本文旨在提供一份全面的條碼閱讀教學,使用 IronBarcode 程式庫來自 Iron Software 在 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 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包

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

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

步驟3:從相機讀取條碼

要掃描供應並從相機捕獲影像,我們需要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

    ' 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以獲取試用授權,提交後,金鑰將通過電郵發送。

如何在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 Forms應用程式。使用NuGet安裝IronBarcode程式庫,並配置PictureBox來捕捉來自相機的圖像。使用AForge程式庫處理相機輸入,並使用IronBarcode來解碼條碼。

如何在VB.NET應用程式中整合相機捕捉功能?

您可以通過使用AForge程式庫來存取和管理相機輸入來在VB.NET應用程式中整合相機捕捉功能。然後可以處理這些輸入以捕捉圖像進行條碼解碼使用IronBarcode。

在VB.NET專案中可以使用IronBarcode解碼哪些型別的條碼?

IronBarcode支援解碼包括QR碼和Code 128在內的一系列條碼型別於VB.NET專案中。該程式庫很有彈性,可以配置識別不同的條碼格式。

開發一個VB.NET條碼掃描應用程式需要哪些必要的組成部分?

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

如何在VB.NET中排除從相機讀取條碼的常見問題?

確保您的相機已正確連接並被系統識別。驗證IronBarcode和AForge程式庫已正確安裝,並確保您的應用程式可以存取相機輸入。檢查程式碼語法和程式庫引用是否有錯誤。

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

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

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

是的,您可以通過從Iron Software網站獲取試用授權金鑰來試用條碼程式庫。提交您的電子郵件ID,然後您將透過電子郵件收到試用金鑰以供VB.NET專案使用。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話