如何在 C# 中一次讀取多個條碼
IronBarcode 透過設置 ExpectMultipleBarcodes = true,使能從圖像和 PDF 中同時讀取多個條碼,簡化物流、零售和庫存管理應用的數據處理。 無論是建立倉儲系統、零售銷售點應用還是文件處理解決方案,IronBarcode 的先進讀取功能提供您所需的可靠性和性能。
本範例展示如何快速使用 IronBarcode 掃描影像,以獲得其中包含的每個條碼。 只需設置 ExpectMultipleBarcodes = true 並選擇您需要的條碼類型 - 無需樣板代碼,毫不費力。
-
1使用 IronBarcode 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional });Dim results = IronBarCode.BarcodeReader.Read("image.png", New IronBarCode.BarcodeReaderOptions With {.ExpectMultipleBarcodes = True, .ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional})Dim results = IronBarCode.BarcodeReader.Read("image.png", New IronBarCode.BarcodeReaderOptions With {.ExpectMultipleBarcodes = True, .ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional}) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronBarcode,並使用 免費試用
最小工作流程(5 個步驟)
- 下載 C# 程式庫來讀取多個條碼
- 使用
Read方法從各種圖像格式中提取條碼值 - 利用
ExpectMultipleBarcodes屬性來設定讀取單一或多個條碼 - 將
ExpectMultipleBarcodes屬性設定為 false,以提高效能 - 列印出條碼值
我如何從圖像中讀取多個條碼?
預設情況下,IronBarcode 會持續掃描文件以讀取多個條碼。 然而,曾有即使存在多個 BarCode,也只傳回一個條碼值的情況。 為解決這個問題,請自訂設定以啟用讀取多個 BarCode,如下所示。 ExpectMultipleBarcodes 屬性存在於 BarcodeReaderOptions 和 PdfBarcodeReaderOptions 類中,允許您用於讀取圖像和 PDF 文件中的條碼。

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());
}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 resultImports 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設置 ExpectMultipleBarcodes 為 true 使 IronBarcode 能夠掃描整個文件以檢測多個條碼,並將其存儲在 BarcodeResults 變量中。 使用 foreach 迴圈,您可以輕鬆地訪問並打印所有條碼值到控制台。
進階多條碼讀取場景
在處理多個 BarCode 時,您可能會遇到需要額外設定的情況。 以下是一個綜合範例,示範如何從複雜的文件中讀取不同格式的多個 BarCode:
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("---");
}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("---")
NextImports 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 中的單一或多重條碼。 預設情況下,即使只有一個 BarCode,引擎也會掃描整個文件。 為提高單個條碼的讀取性能,請設置 ExpectMultipleBarcodes 為 false。 這可以讓引擎在偵測到第一個 BarCode 之後不再掃描整個文件,進而加快條碼擷取的速度。 下面的程式碼展示了這種方法。

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());
}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 resultImports 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 設置與裁剪區域規格結合使用。 當您知道 BarCode 的大概位置時,此技術尤其有用:
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Linq;
// 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}");
}using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Linq;
// 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
Imports System
Imports System.Linq
' Define a crop region where the barcode is likely located
Dim cropRegion = 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 IfImports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
' Define a crop region where the barcode is likely located
Dim cropRegion = 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 = false 讓引擎在檢測到第一個條碼後停止,而不是掃描整個文件,這可以顯著縮短單條碼工作負載的讀取時間。 實際的效能增益依下列因素而有所不同:
- 影像解析度與複雜度
- 圖片中出現的 BarCode 數量
- 選取的 BarCode 格式
- 應用影像濾鏡
- 硬體規格
多條碼讀取的最佳實踐
在生產應用程式中實作多重 BarCode 讀取時,請考慮這些最佳實務:
- 指定預期的條碼類型:而不是使用
BarcodeEncoding.All,只指定您所預期的格式。 這可大幅提升效能。 - 使用適當的圖像格式:為獲得最佳結果,請使用高對比度圖像。 了解更多關於創建最佳條碼圖像。
- 處理不完美的條碼:現實生活中的條碼可能受損或打印不良。 使用圖像校正技術提高讀取成功率。
- 流式處理:對於大量批次,考慮從流中讀取來優化內存使用。
- 錯誤處理:總是要實施正確的錯誤處理,應對無法讀取條碼的情況:
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
}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
}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 TryTry
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 的全面功能,您可以構建可靠的應用,在各種行業和使用案例中高效地處理多條碼讀取場景。
常見問題
如何在IronBarcode中啟用多條碼掃描?
要在IronBarcode中啟用多條碼掃描,請在BarcodeReaderOptions或PdfBarcodeReaderOptions類別中將'ExpectMultipleBarcodes'屬性設置為true。
IronBarcode中的ExpectMultipleBarcodes屬性的目的是什么?
'ExpectMultipleBarcodes'屬性允許IronBarcode同時從圖像或PDF中讀取多個條碼,從而優化在物流和零售應用中的數據提取。
IronBarcode是否可以讀取1D和2D條碼?
是的,IronBarcode可以讀取1D和2D條碼。您可以通過配置BarcodeReaderOptions中的'ExpectBarcodeTypes'屬性來指定期望的條碼類型。
如何提高使用IronBarcode讀取單個條碼的性能?
要提高讀取單個條碼的性能,請在BarcodeReaderOptions中將'ExpectMultipleBarcodes'設置為false。這樣會在檢測到第一個條碼後停止掃描過程。
用於讀取不同格式的多個條碼有哪些配置選項?
IronBarcode提供了高級選項,例如結合不同的條碼解碼類型、應用圖像校正濾鏡以及調整讀取速度以處理多個條碼格式。
圖像校正如何提高IronBarcode的條碼讀取能力?
應用圖像校正濾鏡,例如SharpenFilter和ContrastFilter,可以通過補償圖像質量差或條碼損壞來提高條碼讀取準確性。
有哪些策略推薦用於實現最佳條碼讀取性能?
為了獲得最佳性能,請指定預期的條碼類型、使用高對比度圖像、實施圖像校正技術、從流中讀取大量批次,並包括錯誤處理。
如何在使用IronBarcode讀取條碼時處理錯誤?
使用try-catch塊實施適當的錯誤處理,以管理無法讀取條碼的情況,並在必要時記錄錯誤以進行調試。
IronBarcode是否支持對大量條碼批次進行流處理?
是的,IronBarcode支持從流中讀取條碼,這對於高吞吐量應用或大批量處理中特別有用以優化內存使用。