如何在C#中同時讀取多個條碼
IronBarcode透過設置ExpectMultipleBarcodes = true來實現同時從圖像和PDF中讀取多個條碼,從而簡化物流、零售和庫存管理應用程式中的資料處理。 無論是構建倉庫系統、零售點銷售應用程式,還是文件處理解決方案,IronBarcode的先進讀取功能都提供了您所需的可靠性和性能。
快速入門:輕鬆從圖像中讀取所有條碼
此範例展示了您如何快速使用IronBarcode掃描圖像中的每個條碼。 只需設置ExpectMultipleBarcodes = true以及您想要的條碼型別——無需樣板,無需麻煩。
-
使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode
-
複製並運行這段程式碼片段。
var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional }); -
部署以在您的實時環境中測試
今天就開始在您的專案中使用IronBarcode,透過免費試用
最小化工作流程(5步驟)
- 下載C#程式庫以讀取多個條碼
- 使用
Read方法從各種圖像格式中提取條碼值 - 利用
ExpectMultipleBarcodes屬性來配置單個或多個條碼的讀取 - 將
ExpectMultipleBarcodes屬性設置為false以提高性能 - 列印出條碼值
如何從圖像中讀取多個條碼?
預設情況下,IronBarcode會連續掃描文件以讀取多個條碼。 然而,有時即使存在多個條碼,也只會返回一個條碼值。 為了解決這個問題,請按照下方所示自定義設置以啟用多個條碼的讀取。 PdfBarcodeReaderOptions類中,允許您用於讀取圖像和PDF文件中的條碼。
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-read-multiple-barcodes.cs
using IronBarCode;
using System;
// Set the option to read multiple barcodes
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
Imports IronBarCode
Imports System
' Set the option to read multiple barcodes
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next result
將BarcodeResults變數中。 使用foreach迴圈,您可以輕鬆存取並將所有條碼值列印至控制台。
高級多條碼讀取場景
處理多個條碼時,您可能會遇到需要額外配置的情況。 這裡是一個全面的範例,展示了如何從複雜文件中讀取具有不同格式的多個條碼:
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-3.cs
using IronBarCode;
using System;
using System.Linq;
// Configure advanced options for mixed barcode types
BarcodeReaderOptions advancedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
// Read both 1D and 2D barcodes
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional | BarcodeEncoding.QRCode | BarcodeEncoding.DataMatrix,
// Apply image correction filters for better accuracy
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new ContrastFilter()
},
// Set speed vs accuracy balance
Speed = ReadingSpeed.Balanced
};
// Read barcodes from the image
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Page: {result.PageNumber}");
Console.WriteLine("---");
}
Imports IronBarCode
Imports System
Imports System.Linq
' Configure advanced options for mixed barcode types
Dim advancedOptions As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
' Read both 1D and 2D barcodes
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional Or BarcodeEncoding.QRCode Or BarcodeEncoding.DataMatrix,
' Apply image correction filters for better accuracy
.ImageFilters = New ImageFilterCollection() From {
New SharpenFilter(),
New ContrastFilter()
},
' Set speed vs accuracy balance
.Speed = ReadingSpeed.Balanced
}
' Read barcodes from the image
Dim imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions)
' Process results with error handling
For Each result In imageResults
Console.WriteLine($"Barcode Type: {result.BarcodeType}")
Console.WriteLine($"Value: {result.Value}")
Console.WriteLine($"Page: {result.PageNumber}")
Console.WriteLine("---")
Next
這個高級範例展示了幾個重要功能:
如何閱讀單個條碼以提高性能?
IronBarcode可讀取圖像或PDF中的單個和多個條碼。 預設情況下,即使只有一個條碼,引擎也會掃描整個文件。 要提高讀取單個條碼的性能,將ExpectMultipleBarcodes設為false。 這將停止引擎在檢測到第一個條碼後掃描整個文件,從而實現更快的條碼檢索。 下面的程式碼展示了這種方法。
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-read-single-barcode.cs
using IronBarCode;
using System;
// Set the option to read single barcode
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
Imports IronBarCode
Imports System
' Set the option to read single barcode
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next result
在此範例中,我們使用了與之前相同的含有多個條碼的圖像,但將ExpectMultipleBarcodes設為false。 因此,僅返回第一個條碼值,一旦檢索到第一個條碼,掃描過程即停止。
利用作物區域優化單個條碼讀取
為了在讀取單個條碼時取得更好的性能,將ExpectMultipleBarcodes = false設置與作物區域規範結合使用。 此技術在您大約知道條碼位置時特別有用:
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-5.cs
using IronBarCode;
using IronSoftware.Drawing;
// Define a crop region where the barcode is likely located
var cropRegion = new Rectangle(100, 100, 300, 200);
// Configure options for optimal single barcode reading
BarcodeReaderOptions optimizedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.Code128,
CropArea = cropRegion,
Speed = ReadingSpeed.Faster
};
// Read with optimized settings
var result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault();
if (result != null)
{
Console.WriteLine($"Barcode found: {result.Value}");
}
Imports IronBarCode
Imports IronSoftware.Drawing
' Define a crop region where the barcode is likely located
Dim cropRegion As New Rectangle(100, 100, 300, 200)
' Configure options for optimal single barcode reading
Dim optimizedOptions As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.Code128,
.CropArea = cropRegion,
.Speed = ReadingSpeed.Faster
}
' Read with optimized settings
Dim result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault()
If result IsNot Nothing Then
Console.WriteLine($"Barcode found: {result.Value}")
End If
單個條碼讀取速度有多快?
設置ExpectMultipleBarcodes = false大大提高了讀取單個條碼的效率。 在處理高解析度圖像或在高吞吐量應用中運行異步條碼讀取時,性能提升特別明顯。
使用提供的程式碼片段,這裡是一個粗略估計相同機器上設置ExpectMultipleBarcodes = true和false之間的性能差異:
| ExpectMultipleBarcodes = true | ExpectMultipleBarcodes = false |
|---|---|
| 00.91 秒 | 00.10 秒 |
這表示單個條碼讀取時性能提高了大約9倍。 實際性能增益基於以下因素而有所不同:
- 圖像的解析度和複雜性
- 圖像中存在的條碼數量
- 選擇的條碼格式
- 應用的圖像濾鏡
- 硬體規格
多條碼讀取的最佳實踐
在生產應用中實施多條碼讀取時,請考慮以下最佳實踐:
-
指定預期的條碼型別:不要使用
BarcodeEncoding.All,僅指定您預期的格式。 這顯著提高了性能。 -
使用適當的圖像格式:為獲得最佳效果,使用高對比度圖像。 了解更多建立最佳條碼圖像。
-
處理不完美的條碼:實際條碼可能損壞或印刷不佳。 使用圖像校正技術提高讀取成功率。
-
流處理:對於大型批次,考慮從流中讀取以優化記憶體使用。
- 錯誤處理:針對無法讀取條碼的情況,始終實施適當的錯誤處理:
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-6.cs
try
{
var results = BarcodeReader.Read("barcodes.png", new BarcodeReaderOptions
{
ExpectMultipleBarcodes = true
});
if (!results.Any())
{
Console.WriteLine("No barcodes found in the image");
}
else
{
Console.WriteLine($"Found {results.Count()} barcodes");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading barcodes: {ex.Message}");
// Log error for debugging
}
Imports System
Try
Dim results = BarcodeReader.Read("barcodes.png", New BarcodeReaderOptions With {
.ExpectMultipleBarcodes = True
})
If Not results.Any() Then
Console.WriteLine("No barcodes found in the image")
Else
Console.WriteLine($"Found {results.Count()} barcodes")
End If
Catch ex As Exception
Console.WriteLine($"Error reading barcodes: {ex.Message}")
' Log error for debugging
End Try
通過遵循這些實踐並利用IronBarcode的全面功能,您可以構建強大的應用程式,能夠高效處理各種行業及使用案例中的多條碼讀取場景。
常見問題
如何在C#中從單一圖像中讀取多個條碼?
使用IronBarcode,您可以透過將BarcodeReaderOptions中的ExpectMultipleBarcodes設為true,從單一圖像中讀取多個條碼。這使得IronBarcode能夠掃描整個文件並返回所有找到的條碼,儲存在BarcodeResults集合中以供迭代。
掃描圖像中所有條碼的最快方式是什麼?
最快的方法是使用IronBarcode的Read方法並將ExpectMultipleBarcodes設為true:var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true })。此簡便程式碼無需複雜設置即可提取所有條碼值。
我可以從PDF文件及圖像中讀取多個條碼嗎?
可以,IronBarcode支援從圖像及PDF文件中讀取多個條碼。ExpectMultipleBarcodes屬性在BarcodeReaderOptions和PdfBarcodeReaderOptions類別中均可用,允許您為任何文件型別配置多條碼讀取。
如果我不將ExpectMultipleBarcodes設為true會發生什麼?
按照預設,IronBarcode持續掃描文件以尋找多個條碼。然而,某些情況下,即使存在多個條碼,可能僅返回一個條碼值。明確地將ExpectMultipleBarcodes設為true確保IronBarcode掃描並返回文件中的所有條碼。
讀取多個條碼後,如何獲取單個條碼值?
使用IronBarcode讀取多個條碼後,結果儲存於BarcodeResults變數中。您可以輕鬆地使用foreach迴圈來從集合中遍歷各條碼的值、文字及格式屬性。
讀取多個條碼是否適合於零售和物流應用程式?
是的,IronBarcode的多條碼讀取功能對於零售POS系統、倉儲管理、物流追蹤與庫存管理應用程式是理想之選。它通過高效掃描運輸標籤、產品目錄或庫存清單中的所有條碼來簡化資料處理。
我可以在讀取多個條碼時指定要檢測哪種型別的條碼嗎?
可以,IronBarcode允許您通過ExpectBarcodeTypes屬性指定預期的條碼型別。您可以將它設置為掃描某些特定格式,例如AllOneDimensional、QRCode或任何支援的條碼型別組合以優化掃描效能。
是否設置ExpectMultipleBarcodes會影響掃描效能?
當您確認文件中僅存在一個條碼時,設置ExpectMultipleBarcodes = false可以提升效能。IronBarcode在找到首個條碼後將停止掃描,這使其對於單條碼的情況更快,但同時保留了在需要時多條碼讀取的靈活性。
將IronBarcode實現於專案中需要什麼程式設計技能?
基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文件來指導開發者。
IronBarcode適合於小型專案和大型企業應用嗎?
IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

