
如何從VB .NET中的攝像頭讀取條碼
在科技迅速演變的環境中,條碼掃瞄器裝置已成為各行業的重要組成部分,涵蓋零售、物流、醫療保健到製造業。 Microsoft 的 Visual Basic .NET 是一種多功能且強大的程式語言,為開發者提供了一個堅固的框架以建立可以直接從相機供應中讀取條碼的應用程式。 本文旨在提供一份全面的條碼閱讀教學,使用 IronBarcode 程式庫來自 Iron Software 在 Visual Basic 使用攝影機。
IronBarcode程式庫允許您讀取條碼影像檔案,也可以在來自攝影機的串流時讀取。 它也支援從PDF文件中讀取條碼。 它一次只能掃描一個條碼。在VB.NET 條碼閱讀SDK中讀取條碼影像時需要指定條碼型別。
如何在VB .NET中從相機讀取條碼
- 在Visual Studio中建立一個新的VB.NET專案
- 安裝IronBarcode程式庫並將其應用於您的專案
- 使用AForge程式庫從相機獲取條碼作為影像
- 使用IronBarcode解碼條碼影像
必要條件
- **Visual Studio:**確保您已安裝Visual Studio或其他VB.NET開發環境。
- **相容的相機:**確保所述相機已連接到您的裝置。
- **NuGet包管理器:**確保您可以使用NuGet來管理專案中的包。
步驟1:在Visual Studio中建立新的Visual Basic .NET專案
新建一個VB.NET Windows Forms應用程式(或使用現有專案),在此您想要主機程式碼來讀取相機的條碼。

在下一步中,您可以提供方案和項目名稱。

選擇.NET版本並點擊"建立"按鈕。
步驟2:安裝IronBarcode程式庫
打開您的VB.NET專案並使用 NuGet 包管理器主控台安裝IronBarcode程式庫:

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

步驟3:從相機讀取條碼
要掃描供應並從相機捕獲影像,我們需要AForge程式庫。 從NuGet包管理器安裝如下。

下一步是在表單中從工具箱中新增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
在此範例程式碼中,我們已配置它來閱讀QR碼和Code 128條碼。 首先,我們使用PictureBox從網路攝影機或任何相機裝置中捕獲條碼影像,通過掃描條碼。 然後我們建立一個位圖影像,然後將其作為輸入提供給IronBarcode BarcodeReader類。 此應用程式從影像中讀取2D條碼並解碼它們。 如果解碼後獲得正面結果,則結果將顯示在消息框中。
授權(提供免費試用)
要使用 IronBarcode,您需要在您的appsettings.json中放置授權金鑰。
{
"IronBarCode.LicenseKey": "MYLICENSE.KEY.TRIAL"
}
提供您的電子郵件ID以獲取試用授權,提交後,金鑰將通過電郵發送。

結論
在 VB.NET 中實現從相機讀取條碼是一個強大的功能,可以增強不同行業的多種應用程式。 通過利用像 IronBarcode 這樣的程式庫並將其整合到您的VB.NET專案中,您可以創造出高效且可靠的條碼掃描應用程式,以滿足當今科技驅動世界的需求。 本指南作為起點,開發者可以根據其特定需求、條碼型別和使用案例進一步自定義和優化解決方案。

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


