在實際環境中測試
在生產環境中測試無浮水印。
在任何需要的地方都能運作。
在快速發展的科技領域中,條碼掃描器設備已成為各個行業的重要組成部分,涵蓋從零售和物流到醫療保健和製造業的各個方面。Microsoft 的 Visual Basic .NET 是一種靈活且強大的程式設計語言,為開發人員提供了一個強健的框架,用於創建可以直接從攝像頭影片中讀取條碼的應用程式。本文旨在提供使用 Visual Basic 和攝像頭進行條碼讀取的全面教程 IronBarcode 圖書館由 Iron Software
IronBarcode 库允許讀取條碼圖像文件以及從相機串流時讀取。它也支持從 PDF 文件中讀取條碼。能夠一次掃描一個條碼。在 VB.NET 條碼讀取 SDK 中讀取條碼圖像時需要指定條碼類型。
在 Visual Studio 中建立一個新的 VB.NET 專案
安裝 IronBarcode 函式庫並將其應用到您的專案
從 Aforge 函式庫獲取條碼作為圖像
建立新的 VB.NET Windows 表單應用程式 (或使用現有的專案) 您想將程式碼託管於何處,以從您的相機讀取條碼。
在下一步,您可以提供解決方案和項目名稱。
選擇 .NET 版本並點擊“創建”按鈕。
打開您的 C# 項目並使用 NuGet 套件管理器控制台:
Install-Package BarCode
可以使用 Visual Studio 的 NuGet 套件管理器安裝 NuGet 套件,如下所示。
要掃描視頻並從相機捕獲圖像,我們需要 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
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
在這個範例程式碼中,我們已經配置它來讀取QR碼、資料矩陣和128條碼類型的條形碼圖像。首先,我們使用PictureBox從網絡攝像頭或任何攝像設備掃描條形碼來捕捉條形碼圖像。然後,我們創建一個位圖圖像,並將其提供給IronBarcode BarcodeReader類作為輸入。此應用程式從圖像中讀取2D條形碼並解碼。如果解碼後獲得正面結果,則結果會顯示在訊息框中。
IronBarcode這個密鑰需要放置在 appsettings.json。
{
"IronBarcode.LicenseKey":"MYLICENSE.KEY.TRIAL"
}
提供用戶電子郵件以取得試用授權,提交電子郵件後,授權金鑰將通過電子郵件發送。
從攝像頭實現條形碼讀取 VB.NET 是一個強大的功能,可以增強各行各業的各種應用程式。通過利用像 IronBarcode 將它們整合到您的 VB.NET 專案中,您可以創建出高效且可靠的條碼掃描應用程式,以滿足現今科技驅動世界的需求。本指南作為起點,開發者可以根據其特定需求、條碼類型和使用案例進一步定制和優化解決方案。