如何使用IronBarcode在 C# 中調整讀取速度

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

IronBarcode提供了四種讀取速度選項(ExtremeDetail),讓您在 C# 中讀取條碼時可以控制處理速度和準確性之間的權衡,其中2 @--CODE@CODE@CODE@CO-96@CODE@CO-96@CODE@CO-96@CODE@CO-96@CODE@CO-96@CODE@CO-96@CODE@CO-96@CODE@CO-96@CODE@CO-96@CODE@COv.

介紹

讀取大量條碼時,準確性至關重要,但資源分配和處理效率同樣重要。 輸入影像的品質決定了條碼閱讀器應該如何處理它們——對於清晰的影像是否跳過預處理,或者對於劣化條碼是否使用資源密集型選項來提高準確性。

IronBarcode提供靈活的處理速度和準確度選擇,讓您可以控制條碼讀取過程的各個方面。 您可以根據輸入的圖像和可用資源做出決策。 對於更高級的條碼讀取場景,請瀏覽我們全面的條碼讀取教程,其中涵蓋了各種格式和技術。

本文提供了針對不同場景選擇最佳閱讀速度的指導原則。 我們將使用二維碼範例來示範改變讀取速度如何影響結果。 如果您專門使用二維碼,請查看我們的C# 二維碼產生器教學課程,以建立測試樣本。

快速入門:以均衡速度讀取條碼

使用 IronBarcode 的 BarcodeReaderOptions 功能,即可立即設定掃描的 Speed 等級。 本範例展示如何使用 Balanced 設定快速讀取條碼,以獲得快速可靠的結果。

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

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

    var results = IronBarCode.BarcodeReader.Read("path/to/image.png", new IronBarCode.BarcodeReaderOptions { Speed = IronBarCode.ReadingSpeed.Balanced });
  3. 部署到您的生產環境進行測試

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

    arrow pointer


閱讀速度選項有哪些?

IronBarcode提供四種 ReadingSpeed 選項:DetailedExtremeDetail。 我們將使用一個樣本集來檢驗每個選項,該樣本集主要包含一些品質較差的條碼圖像,但也包含一些清晰的圖像,以展示該庫的功能。 如需查看所有支援的格式,請造訪我們支援的條碼格式頁面

我們將使用.NET基準測試函式庫來測量處理時間和記憶體使用情況,展示每個選項的比較情況,並確定每種讀取速度的理想場景。 我們將示範基準測試程式碼和一種簡單的方法來統計成功讀取的降級條碼的數量。 有關配置讀取器選項的更多詳細信息,請參閱我們的條碼讀取器設定範例

何時應該使用更快的速度選項?

Faster 選項以最少的資源提供最快的條碼讀取速度,但會降低準確性。 這個過程跳過了影像預處理,當輸入影像已經清晰銳利時效果最佳。

此範例將 Speed 屬性設為 ReadingSpeed.Faster,匯入條碼目錄,並列印找到的條碼及其值、類型和每張影像的數量。 為了更好地了解如何從各種圖像格式中讀取條碼,請查看我們關於從圖像中讀取條碼的指南。

:path=/static-assets/barcode/content-code-examples/how-to/reading-speed-option-faster.cs
using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

var optionsFaster = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Faster
};

// Directory containing PDF files
string folderPath = @"YOUR_FILE_PATH";

// Get all PDF files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

int countFaster = 0;
var stopwatch = Stopwatch.StartNew();
foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file, optionsFaster);

    if (results.Any())
    {
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
        foreach (var result in results)
        {
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}");
            countFaster++;
        }
    }
    else
    {
        Console.WriteLine($"No barcode found in: {Path.GetFileName(file)}");
    }
}

stopwatch.Stop();

// Print number of images the barcode reader could decode
Console.WriteLine($"Faster could read = {countFaster} out of {pdfFiles.Length} in {stopwatch.ElapsedMilliseconds}ms");
$vbLabelText   $csharpLabel

Faster 選項在 25 秒內偵測到 430 個條碼結果中的 146 個,準確率達到 33.95%。 雖然速度很快,但這種方法只適用於影像品質極佳的情況。 當處理單一影像中的多個條碼時,請參考我們的多條碼讀取指南,以獲得最佳配置。

為什麼推薦使用平衡模式?

Balanced 選項平衡了準確性和讀取性能。 IronBarcode採用輕量影像處理技術來清楚顯示條碼區域,使其更容易偵測和讀取。 對於大多數現代圖像,建議使用此設置,因為輕微的處理通常會產生準確的結果。

讓我們使用相同的圖像來演示 Balanced 如何影響輸出結果。 對於非同步操作,請參閱我們關於IronBarcode的非同步和多執行緒指南。

:path=/static-assets/barcode/content-code-examples/how-to/reading-speed-option-balanced.cs
using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

var optionsFaster = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Balanced
};

// Directory containing PDF files
string folderPath = @"YOUR_FILE_PATH";

// Get all PDF files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

int countFaster = 0;
var stopwatch = Stopwatch.StartNew();
foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file, optionsFaster);

    if (results.Any())
    {
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
        foreach (var result in results)
        {
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}");
            countFaster++;
        }
    }
    else
    {
        Console.WriteLine($"No barcode found in: {Path.GetFileName(file)}");
    }
}

stopwatch.Stop();

// Print number of images the barcode reader could decode
Console.WriteLine($"Balanced could read = {countFaster} out of {pdfFiles.Length} in {stopwatch.ElapsedMilliseconds}ms");
$vbLabelText   $csharpLabel

Balanced 選項在 43 秒內偵測到 430 個條碼結果中的 237 個。 它提供了 55.11% 的準確率——比 Faster 有了顯著的改進——而時間只略有增加。 此選項在記憶體和速度之間保持了有效的平衡,使其成為大多數情況下的理想選擇,也是建議的起始選項。 這種平衡的方法與適當的影像預處理技術結合使用效果尤佳。

我什麼時候需要詳細速度選項?

當影像嚴重模糊或扭曲,且 Balanced 無法產生清晰的結果時,請使用 Detailed 選項。 它採用中等預處理方法來清晰化條碼區域並減少數位噪聲,從而實現更好的檢測。 對於嚴重劣化的影像,請參閱我們的影像校正指南,其中涵蓋了各種預處理技術。

讓我們套用 Detailed 設定並觀察其對輸出的影響。

:path=/static-assets/barcode/content-code-examples/how-to/reading-speed-option-detailed.cs
using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

var optionsFaster = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.Detailed
};

// Directory containing PDF files
string folderPath = @"YOUR_FILE_PATH";

// Get all PDF files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

int countFaster = 0;
var stopwatch = Stopwatch.StartNew();
foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file, optionsFaster);

    if (results.Any())
    {
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
        foreach (var result in results)
        {
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}");
            countFaster++;
        }
    }
    else
    {
        Console.WriteLine($"No barcode found in: {Path.GetFileName(file)}");
    }
}

stopwatch.Stop();

// Print number of images the barcode reader could decode
Console.WriteLine($"Detailed could read = {countFaster} out of {pdfFiles.Length} in {stopwatch.ElapsedMilliseconds}ms");
$vbLabelText   $csharpLabel

Detailed 選項在 5 分 30 秒內偵測到 430 個條碼結果中的 237 個。 它在嚴重損壞的條碼上仍能達到 55.11% 的成功率,這證明了它的準確性。 然而,處理時間顯著增加意味著此選項應僅用於處理品質較差的條碼影像。 處理不完美條碼時,請參考我們的不完美條碼處理範例,以了解更多策略。

哪些情況需要極致細節速度?

ExtremeDetail 設定會對條碼影像進行大量處理,從而顯著降低讀取效能。 這種佔用大量 CPU 資源的選項最適合掃描單一輸入檔中多個模糊不清的條碼。當其他選項均無法獲得理想結果時,才可將其作為最後的選擇。 對於大批量處理場景,可以探索從 PDF 文件中讀取條碼,PDF 文件通常每頁包含多個條碼。

讓我們應用 ExtremeDetail 設定來觀察其影響。

:path=/static-assets/barcode/content-code-examples/how-to/reading-speed-option-extreme-detailed.cs
using IronBarCode;
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;

var optionsFaster = new BarcodeReaderOptions
{
    Speed = ReadingSpeed.ExtremeDetail
};

// Directory containing PDF files
string folderPath = @"YOUR_FILE_PATH";

// Get all PDF files in the directory
var pdfFiles = Directory.GetFiles(folderPath, "*.jpg");

int countFaster = 0;
var stopwatch = Stopwatch.StartNew();
foreach (var file in pdfFiles)
{
    // Read the barcode
    var results = BarcodeReader.Read(file, optionsFaster);

    if (results.Any())
    {
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}");
        foreach (var result in results)
        {
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}");
            countFaster++;
        }
    }
    else
    {
        Console.WriteLine($"No barcode found in: {Path.GetFileName(file)}");
    }
}

stopwatch.Stop();

// Print number of images the barcode reader could decode
Console.WriteLine($"ExtremeDetail could read = {countFaster} out of {pdfFiles.Length} in {stopwatch.ElapsedMilliseconds}ms");
$vbLabelText   $csharpLabel

ExtremeDetail 選項在大約 10 分鐘內辨識出 430 個條碼影像中的 313 個。 雖然對嚴重劣化的條碼能達到令人印象深刻的 72.79% 的準確率,但由於資源消耗量高,因此只適合作為最後的手段。 使用此選項前,請考慮對影像進行預處理。

不同速度有何不同?

模式 找到條碼 平均時間 每份文件所需時間 GC壓力 精度提升
**快點** 147/430 (33.95%) 25秒 0.058秒 高(第二代:177K) 基線
**均衡** 237/430 (55.11%) 43秒 0.1秒 高(第二代:151K) 與`Faster`相比,成長了 62.32%。
**詳細的** 237/430 (55.11%) 5.50分鐘 0.767秒 非常高(第二代:297K) +0% 對比`Balanced`
**極致細節** 313/430 (72.79%) 10.14分鐘 1.414秒 Extreme(二代:474萬) 與`Detailed`相比,+32.08%

如何為我的應用程式選擇合適的速度?

根據上述比較,從 Faster 設定開始,逐步過渡到 DetailedExtremeDetail,以確定顯著的輸出差異。 對於大多數情況,Balanced 都能妥善處理一切。 僅對嚴重失真的圖像使用 DetailedExtremeDetail

雖然 DetailedExtremeDetail 應用了中等和重度處理,但有時將處理過程拆分會更有效率——在條碼讀取之前手動應用圖像濾鏡,而不是使用單一處理過程。 有關圖像預處理的更多信息,請參閱本指南

哪種速度設定最符合我的使用需求?

基於影像品質的取樣速度選擇決策樹,選項範圍從

常見問題解答

四種可用的 BarCode 讀取速度選項是什麼?

IronBarcode 提供四種 ReadingSpeed 選項:Faster、Balanced、Detailed 和 ExtremeDetail。每個選項在處理速度和精確度之間提供不同的平衡,平衡是大多數應用程式的建議起點。

掃描 BarCode 時,如何設定讀取速度?

您可以使用 IronBarcode 中的 BarcodeReaderOptions 類設定讀取速度。只需創建一個新的 BarcodeReaderOptions 物件,並將 Speed 屬性設置為您所需的 ReadingSpeed 值 (Faster、Balanced、Detailed 或 ExtremeDetail),然後將其傳遞至 Read 方法。

我的應用程式應該使用哪個閱讀速度選項?

IronBarcode 建議大部分應用程式從平衡速度設定開始。如果您擁有高品質、清晰的 BarCode 影像,您可以使用 Faster 模式。對於降級或品質較差的影像,可考慮使用 Detailed 或 ExtremeDetail 模式,以獲得更高的精確度。

不同閱讀速度選項之間如何取捨?

IronBarcode 讀取速度的取捨是在處理速度與精準度之間。Faster 模式可快速處理影像,但可能會遺漏品質不佳影像中的 BarCode。ExtremeDetail 模式提供最高的精確度,但需要更多的處理時間和記憶體資源。

我可以使用不同的速度設定讀取多種條碼格式嗎?

是的,IronBarcode 支援讀取各種條碼格式,包括所有速度設定的 QR 碼。速度設定會影響處理方式,但不會限制您可以讀取的條碼類型。請造訪支援的 BarCode 格式頁面以取得完整清單。

影像品質如何影響我應該選擇的閱讀速度?

圖像品質直接影響您在 IronBarcode 中的速度選擇。清晰、高品質的 BarCode 影像可使用 Faster 模式進行有效處理。退化、模糊或低對比度的影像需要使用 Detailed 或 ExtremeDetail 模式,以確保準確的條碼偵測和讀取。

讀取有速度選項的 BarCode 的最小工作流程是什麼?

IronBarcode 的最小工作流程包括 5 個步驟:1) 下載 C# 函式庫;2) 使用 BarcodeReaderOptions 設定讀取速度;3) 使用您的影像路徑呼叫讀取方法;4) 擷取並列印條碼值;5) 評估不同速度之間的效能權衡。

如何衡量不同閱讀速度對效能的影響?

IronBarcode 在不同讀取速度下的效能可使用 .NET 基準函式庫來追蹤處理時間與記憶體使用量。這可協助您針對特定的使用情況和資源限制,找出最佳的速度設定。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 2,121,847 | 版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

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