如何在 C# 中一次讀取多個 BarCode
IronBarcode 透過設定 ExpectMultipleBarcodes = true,可同時從圖片和 PDF 檔案中讀取多個 BarCode,從而簡化物流、零售及庫存管理應用程式的資料處理流程。 無論是建置倉儲系統、零售銷售點應用程式,還是文件處理解決方案,IronBarcode 的先進讀取功能都能提供您所需的可靠性與效能。
快速入門:輕鬆讀取圖片中的所有BarCode
此範例展示如何快速使用 IronBarcode 掃描圖片,以識別其中包含的所有 BarCode。 只需在您想要的 BARCODE 類型旁設定 ExpectMultipleBarcodes = true —— 無需重複的範本文字,輕鬆無煩惱。
-
using NuGet 套件管理員安裝 https://www.nuget.org/packages/BarCode
PM > Install-Package BarCode -
請複製並執行此程式碼片段。
var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional }); -
部署至您的生產環境進行測試
立即透過免費試用,在您的專案中開始使用 IronBarcode
簡化工作流程(5 個步驟)
- 下載 C# 函式庫以讀取多個 BarCode
- 使用
Read方法從各種圖像格式中提取 BarCode 值 - 使用
ExpectMultipleBarCodes屬性來設定讀取單一或多個 BarCode - 將
ExpectMultipleBarcodes屬性設為 false 以提升效能 - 列印BarCode值
如何從圖片中讀取多個BarCode?
預設情況下,IronBarcode 會持續掃描文件以讀取多個 BARCODE。 然而,曾有情況發生,即使存在多個BarCode,系統卻僅返回一個BarCode值。 為解決此問題,請參照下方示意,自訂設定以啟用讀取多個BARCODE的功能。 ExpectMultipleBarcodes 屬性同時存在於 BarcodeReaderOptions 和 PdfBarcodeReaderOptions 類別中,讓您能運用它讀取圖片和 PDF 文件中的 BARCODE。
: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
將 ExpectMultipleBarcodes 設為 true,可讓 IronBarcode 掃描整個文件以尋找多個 BarCode,並將其儲存於 BarcodeResults 變數中。 透過 foreach 迴圈,您可以輕鬆存取並PRINT所有BarCode值至主控台。
進階的多重BarCode讀取情境
在處理多個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 from various sources
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
var pdfResults = BarcodeReader.ReadPdf("document-with-barcodes.pdf", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Confidence: {result.Confidence}%");
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 from various sources
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
var pdfResults = BarcodeReader.ReadPdf("document-with-barcodes.pdf", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Confidence: {result.Confidence}%");
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 from various sources
Dim imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions)
Dim pdfResults = BarcodeReader.ReadPdf("document-with-barcodes.pdf", advancedOptions)
' Process results with error handling
For Each result In imageResults
Console.WriteLine($"Barcode Type: {result.BarcodeType}")
Console.WriteLine($"Value: {result.Value}")
Console.WriteLine($"Confidence: {result.Confidence}%")
Console.WriteLine($"Page: {result.PageNumber}")
Console.WriteLine("---")
Next
此進階範例展示了幾個重要功能:
- 混合BarCode格式支援:結合不同類型的BarCode編碼
- 影像校正濾鏡:運用影像濾鏡來提升辨識準確度
- 閱讀速度優化:透過閱讀速度選項在速度與準確性之間取得平衡
- 信心分數:取得每個偵測到的BarCode之信心閾值
如何讀取單一條碼以獲得更佳效能?
IronBarcode 能讀取圖片或 PDF 中的單一及多個 BarCode。 預設情況下,即使文件中僅存在一個BarCode,引擎仍會掃描整份文件。 若需提升讀取單一條碼的效能,請將 ExpectMultipleBarcodes 設為 false。 此功能可防止引擎在偵測到第一個BarCode後掃描整個文件,從而加快BarCode擷取速度。 以下程式碼示範了此翻譯方法。
: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
在此範例中,我們使用了與先前相同的含多個 BARCODE 的圖片,但將 ExpectMultipleBarcodes 設為 false。 因此,系統僅會傳回第一個BarCode的值,且一旦讀取到第一個BarCode,掃描程序便會停止。
透過裁切區域優化單的一條碼讀取
若要進一步提升單的一條碼的讀取效能,請將 ExpectMultipleBarcodes = false 設定與裁切區域規格結合使用。 當您知道BARCODE的大致位置時,此技術特別有用:
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}");
Console.WriteLine($"Read time: {result.ReadTime}ms");
}
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}");
Console.WriteLine($"Read time: {result.ReadTime}ms");
}
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}")
Console.WriteLine($"Read time: {result.ReadTime}ms")
End If
單的一條碼讀取速度快了多少?
設定 ExpectMultipleBarcodes = false 可大幅提升讀取單一條碼的效率。 在處理高解析度圖像,或在高吞吐量應用程式中實作非同步BarCode讀取時,效能提升尤為顯著。
根據提供的程式碼片段,以下是同一台機器上設定 ExpectMultipleBarcodes = true 與 false 之間的效能差異粗略估算:
| ExpectMultipleBarcodes = true | ExpectMultipleBarcodes = false |
|---|---|
| 00.91 秒 | 00.10 秒 |
這代表在讀取單一條碼時,效能提升了約 9 倍。 實際效能提升程度會因以下因素而異:
- 圖片解析度與複雜度
- 圖像中包含的BarCode數量
- 精選BarCode格式
- 已套用影像濾鏡
- 硬體規格
多重BarCode讀取的最佳實務
在生產環境應用程式中實作多重BarCode讀取時,請參考以下最佳實務:
-
指定預期的BARCODE類型:請勿使用
BarcodeEncoding.All,而應僅列出您預期的格式。 這顯著提升了效能。 -
使用適當的圖片格式:為獲得最佳效果,請使用高對比度的圖片。 進一步了解如何製作最佳的BarCode圖像。
-
處理不完整的 BarCode:現實世界中的 BarCode 可能有損壞或印刷品質不佳的情況。 請運用圖像修正技術以提升閱讀成功率。
-
流處理:對於大型批次資料,建議採用流讀取方式以優化記憶體使用效率。
- 錯誤處理:針對無法讀取BarCode的情境,務必實作適當的錯誤處理機制:
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
}
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 的全面功能,您將能建置穩健的應用程式,有效處理各行各業及各種使用情境中的多種 BarCode 讀取情境。
常見問題
如何在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的多條碼讀取功能非常適合零售銷售點系統、倉庫管理、物流跟踪和庫存管理應用。通過同時有效掃描運單標籤、產品目錄或庫存表中的所有條碼來簡化數據處理。
當讀取多個條碼時,我可以指定要查找的條碼類型嗎?
是的,IronBarcode允許您通過ExpectBarcodeTypes屬性指定預期的條碼類型。您可以設置為掃描特定格式,如AllOneDimensional、QRCode或任何支持的條碼類型組合,以優化掃描性能。
設置ExpectMultipleBarcodes是否會影響掃描性能?
當您知道文檔中只有一個條碼時,設置ExpectMultipleBarcodes = false可以提高性能。IronBarcode會在找到第一個條碼後停止掃描,使其在單條碼場景下更快,同時在需要時仍提供多條碼讀取的靈活性。
將IronBarcode實現於專案中需要什麼程式設計技能?
基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文檔來指導開發者。
IronBarcode適合於小型專案和大型企業應用嗎?
IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

