如何在C#中一次讀取多個條碼

如何在 C# 中一次讀取多個條碼

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronBarcode透過設定 ExpectMultipleBarcodes = true,能夠同時從映像和 PDF 讀取多個條碼,從而簡化物流、零售和庫存管理應用程式的資料處理。 無論是建造倉庫系統、零售銷售點應用程式還是文件處理解決方案, IronBarcode 的先進讀取功能都能提供您所需的可靠性和效能。

快速入門:輕鬆讀取影像中的所有條碼

此範例展示了使用IronBarcode可以多麼快速地掃描影像中包含的每個條碼。 只需在所需的條碼類型旁邊設定 ExpectMultipleBarcodes = true 即可——無需樣板代碼,輕鬆便捷。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode

    PM > Install-Package BarCode
  2. 複製並運行這段程式碼。

    var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional });
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronBarcode

    arrow pointer


如何從影像中讀取多個條碼?

預設情況下, IronBarcode會持續掃描文件以讀取多個條碼。 然而,也曾出現過這樣的情況:即使存在多個條碼,也只會傳回一個條碼值。 為了解決這個問題,請自訂設定以啟用讀取多個條碼的功能,如下所示。 ExpectMultipleBarcodes屬性存在於BarcodeReaderOptionsPdfBarcodeReaderOptions類別中,可讓您使用它來讀取影像和 PDF 文件中的條碼

三個條碼樣本分別標記為 A、B 和 C,展示了不同的條碼模式,用於多條碼讀取演示。
: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());
}
$vbLabelText   $csharpLabel

ExpectMultipleBarcodes設為 true 可使IronBarcode掃描整個文件中的多個條碼並將它們儲存在BarcodeResults變數中。 使用 foreach 循環,您可以輕鬆存取所有條碼值並將其列印到控制台。

高級多條碼讀取場景

使用多個條碼時,您可能會遇到需要額外配置的情況。 以下是一個全面的範例,示範如何從複雜文件中讀取多個不同格式的條碼:

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("---");
}
$vbLabelText   $csharpLabel

這個進階範例展示了幾個重要功能: -支援混合條碼格式:組合不同的條碼編碼類型 -影像校正濾波器:使用影像濾波器提高讀取精度 -閱讀速度優化:透過閱讀速度選項平衡速度和準確性 -置信度評分:取得每個偵測到的條碼的置信度閾值

如何提高讀取單條碼的效能?

IronBarcode可讀取影像或 PDF 中的單一或多個條碼。 預設情況下,即使只有一個條碼,引擎也會掃描整個文件。 為了提高讀取單一條碼時的效能,請將ExpectMultipleBarcodes設定為 false。 這樣可以防止引擎在偵測到第一個條碼後掃描整個文檔,從而加快條碼檢索速度。 以下程式碼演示了這種方法。

三個相同的條碼樣本,分別標記為 A、B 和 C,用於條碼讀取演示。
: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());
}
$vbLabelText   $csharpLabel

在這個例子中,我們使用了與之前相同的具有多個條碼的圖像,但將ExpectMultipleBarcodes設定為 false。 因此,只會傳回第一個條碼值,掃描過程在檢索到第一個條碼後停止。

利用作物區域優化單條碼讀取

為了在讀取單條碼時獲得更好的效能,請將ExpectMultipleBarcodes = false設定與裁切區域規範結合使用。 當您知道條碼的大致位置時,這種方法尤其有用:

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");
}
$vbLabelText   $csharpLabel

單條碼讀取速度能提升多少?

ExpectMultipleBarcodes設定為 false 可以大幅提高讀取單一條碼的效率。 在處理高解析度影像或在高吞吐量應用中實現非同步條碼讀取時,效能提升尤為明顯。

使用提供的程式碼片段,以下是同一台機器上將ExpectMultipleBarcodes設為 true 和 false 時效能差異的粗略估計:

ExpectMultipleBarcodes = true ExpectMultipleBarcodes = false
0.91秒 0.10 秒

這意味著讀取單條碼時效能提高了約 9 倍。 實際性能提升取決於以下因素: 影像解析度和複雜性 影像中存在的條碼數量

  • 選取的條碼格式
  • 應用了影像濾波器
  • 硬體規格

多條碼讀取的最佳實踐

在生產應用中實作多條碼讀取時,請考慮以下最佳實務:

1.指定預期條碼類型:不要使用 BarcodeEncoding.All,而只指定您期望的格式。 這顯著提高了性能。

2.使用合適的影像格式:為了獲得最佳效果,請使用高對比度影像。 了解更多關於創建最佳條碼圖像的資訊。

3.處理不完美的條碼:現實世界中的條碼可能會損壞或列印品質不佳。 使用影像校正技術提高閱讀成功率。

4.流處理:對於大批量數據,考慮從流中讀取數據以優化記憶體使用。

5.錯誤處理:對於條碼無法讀取的情況,請務必實施適當的錯誤處理:

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
}
$vbLabelText   $csharpLabel

透過遵循這些做法並利用 IronBarcode 的全面功能,您可以建立強大的應用程序,以高效處理各個行業和用例中的多種條碼讀取場景。

常見問題解答

如何在 C# 中從單一影像讀取多個 BarCode?

使用 IronBarcode,您可以在 BarcodeReaderOptions 中設定 ExpectMultipleBarcodes = true,從單一影像讀取多個條碼。這使得 IronBarcode 可以掃描整個文件,並在一個 BarcodeResults 集合中返回所有找到的條碼,您可以遍歷該集合。

掃描影像中所有 BarCode 的最快方法是什麼?

最快的方法是使用 IronBarcode 的讀取方法,且 ExpectMultipleBarcodes = true: var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true })。這個最小的程式碼不需要複雜的設定就能擷取所有的 BarCode 值。

我可以從 PDF 文件以及影像讀取多個 BarCode 嗎?

是的,IronBarcode 支持從图像和 PDF 文檔中读取多個條碼。ExpectMultipleBarcodes 属性在 BarcodeReaderOptions 和 PdfBarcodeReaderOptions 类中都可用,允許您為任何文檔类型配置多條碼读取。

如果不將 ExpectMultipleBarcodes 設為 true 會發生什麼情況?

預設情況下,IronBarcode 會持續掃描文件中的多個條碼。然而,在某些情況下,即使存在多個條碼,也可能只返回一個條碼值。顯式地設定 ExpectMultipleBarcodes = true 可確保 IronBarcode 掃描並回傳文件中的所有條碼。

讀取多個 BarCode 後,如何存取個別的條碼值?

使用 IronBarcode 讀取多個條碼後,結果會儲存在 BarcodeResults 變數中。您可以使用 foreach 環路輕鬆存取個別條碼的值,以遍歷集合並處理每個條碼的值、文字和格式屬性。

讀取多個 BarCode 是否適合零售和物流應用?

是的,IronBarcode 的多重條碼讀取能力是零售點銷售系統、倉儲管理、物流追蹤和庫存管理應用程式的理想選擇。它能同時有效地掃描出貨標籤、產品目錄或庫存單中的所有 BarCode,從而簡化資料處理流程。

當讀取多個 BarCode 時,我可以指定要尋找哪些條碼類型嗎?

是的,IronBarcode 允許您使用 ExpectBarcodeTypes 屬性指定預期的條碼類型。您可以將其設置為掃描特定格式,如 AllOneDimensional、QRCode 或任何支援條碼類型的組合,以優化掃描性能。

設定 ExpectMultipleBarcodes 會影響掃描效能嗎?

當您知道文件中只存在一個條碼時,設定 ExpectMultipleBarcodes = false 可以提高性能。IronBarcode 會在找到第一個條碼後停止掃描,使其在單一條碼的情況下速度更快,同時在需要時仍提供多條碼讀取的彈性。

Hairil Hasyimi Bin Omar
軟體工程師
和所有优秀的工程师一样,Hairil 是個努力学习者。他正在细化自己的 C# 、Python 和 Java 知识,将这些知识應用于 Iron Software 各個团队成员以增加价值。Hairil 自马来西亚 Universiti Teknologi MARA 加入 Iron Software 团队,并以化学与工艺工程学士学位毕业。
準備好開始了嗎?
Nuget 下載 2,108,094 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package BarCode
執行範例 看您的字串變成 BarCode。