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

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

準確性對於可擴展性和讀取大量條碼至關重要,但條碼閱讀器如何分配資源以及其效率如何也必須考慮。 根據輸入影像和影像本身的質量,開發人員必須決定條碼閱讀器應該如何處理和讀取影像,例如,如果影像清晰,是否跳過影像預處理;或採用資源密集型選項來提高條碼閱讀器的讀取精度。

IronBarCode 讓您可以靈活地選擇條碼處理的速度和精度,從而可以微調和控制流程的各個方面。 您可以根據您擁有的輸入圖像和您想要分配的資源來做出決定。

以下的文章將提供一份關於如何選擇最佳閱讀速度的一般性指導原則。 我們將使用一組二維碼樣本來簡要說明改變讀取速度會如何影響結果。

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

使用 IronBarcode 的BarcodeReaderOptions可以立即設定掃描Speed等級。 此範例展示了開發人員如何使用Balanced設定快速讀取條碼,從而獲得快速可靠的結果。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronBarcode

    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設定為四個不同的選項: FasterBalancedDetailedExtremeDetail 。 我們將逐一介紹這些選項,並透過一個範例集作為基準,來了解ReadingSpeed值如何影響流程的輸出。 樣本集包含一些品質較差的條碼圖像和一些品質較差的圖像,其中品質較差的圖像佔比較大,以說明該庫的功能。

我們還將使用流行的 .NET 基準測試庫來測試時間和記憶體使用情況,說明每種選項與其他選項的比較情況,並確定每種閱讀速度選項的理想場景和情況。 我們將展示用於使用該程式庫進行基準測試的程式碼,以及一種更直接的方法來推斷 IronBarCode 可以讀取多少降級條碼。

更快的速度選項

第一個值是Faster 。 一般來說,將Speed屬性設定為此值可以以最少的資源實現最快的條碼讀取速度,但代價是精度降低。 過程會跳過影像預處理,如果影像本身在輸入到該過程之前已經清晰銳利,則通常建議採用此方法。

在這個例子中,我們將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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Faster在 25 秒內檢測出 430 個條碼結果中的 146 個。 此選項速度很快,可以解碼條碼的約 33.95%。 雖然速度很快,但這種方法通常只適用於影像品質完美無瑕的情況。

平衡速度選項

Balanced值兼顧了準確性和讀取性能。 套用此設定時,IronBarcode 會對影像進行輕微處理,以清楚顯示條碼區域,使其更容易條碼讀取器偵測和讀取。 一般來說,對於大多數現代圖像來說,這是建議的設置,因為輕微的處理應該足以產生準確的結果。

讓我們使用相同的圖像,並展示Balanced值如何影響輸出結果。

: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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Balanced在 43 秒內偵測出 430 個條碼結果中的 237 個。 與Faster選項相比,"平衡"選項的準確率提高了 55.11%,而耗時僅略有增加。 Balanced選項在記憶體和速度之間保持了高效的平衡,使其成為大多數情況下的理想選擇,也是建議的初始設定。

詳細速度選項

在影像嚴重模糊或扭曲,以及Balanced選項無法清晰檢測和產生結果的情況下,開發人員可以選擇使用Detailed屬性對影像應用中等程度的預處理,以進一步清晰地顯示條碼區域,並清除更多數位噪聲,以便條碼讀取器能夠檢測到條碼。

這次我們將Detailed設定應用於Speed屬性,看看它是否會影響影像的整體輸出。

: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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Detailed檢測在 5 分 30 秒內偵測到 430 個條碼中的 237 個結果。 在嚴重劣化的條碼資料集上,其成功率達到 55.11%,證明了其準確性。 然而,這種權衡是顯著的,因為處理時間會大幅增加。 因此,此選項必須專門用於條碼影像品質下降的情況。

極致細節速度選項

Speed屬性的最後一個設定是ExtremeDetail ,它會對條碼影像進行大量處理,以便讀取器可以讀取它,這通常會降低 IronBarcode 的讀取效能。 此選項非常適合掃描單一輸入檔中大量條碼,尤其適用於批次掃描不清晰或模糊的條碼。 此操作會佔用大量 CPU 資源,並應在其他方法皆無法達到預期效果時作為最後的選擇。

讓我們將ExtremeDetail設定套用到Speed屬性,看看是否會影響結果。

: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");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

ExtremeDetail選項能夠識別 430 個條碼影像中的 313 個,每次運行平均需要 10 分鐘。 它功能強大,但由於資源消耗量高,因此只適合作為嚴重退化選項的最後手段。 即使在嚴重損壞的條碼資料集中,它也能以令人印象深刻的 72.79% 的準確率找到最多的條碼,但仍然建議在將條碼輸入條碼查找器之前對其進行預處理。

總計表

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

選擇合適的速度

經過上述簡要比較和上述不同場景後,開發人員通常應該從最低設定Faster開始嘗試,然後逐步提高到BalancedDetailedExtremeDetail ,看看輸出結果之間是否存在任何顯著差異。 關於使用 IronBarCode 的可擴展性,在大多數情況下, Balanced通常足以處理所有內容,開發人員只需根據影像的扭曲程度使用DetailedExtremeDetail模式。 此外,雖然這兩個選項在使用DetailedExtremeDetail時都會對影像進行中等和重度處理,但在某些情況下,將處理過程分成兩步,在將影像放入條碼閱讀器之前手動套用影像濾鏡,比使用單一處理流程更有價值。 有關條碼閱讀器圖像處理的更多信息,請參閱此處。 總的來說,這裡有一個簡表和總結,說明了每種不同速度適用的情況。

決策圖

輸出

常見問題解答

IronBarcode 中的閱讀速度選項有哪些?

IronBarcode 允許您調整閱讀速度以適應應用程序的需求,優化條碼閱讀的更快處理或更高的準確性。

如何在 IronBarcode 中配置條碼閱讀速度?

您可以通過在條碼閱讀器的設置中設置特定的參數來配置 IronBarcode 中的閱讀速度,允許您優先考慮速度或準確性。

為什麼我需要調整 IronBarcode 中的閱讀速度?

調整閱讀速度可以幫助平衡性能和準確性,這在需要快速處理或精確條碼檢測的應用中至關重要。

在 IronBarcode 中是否可以同時實現高準確性和快速閱讀速度?

雖然 IronBarcode 允許在速度和準確性之間進行自定義平衡,但達到完美的組合可能取決於特定的使用案例和條碼的複雜性。

在 IronBarcode 中,哪些因素會影響對閱讀速度設置的選擇?

條碼類型、圖像質量以及操作環境等因素會影響在 IronBarcode 中對閱讀速度設置的選擇。

我可以在 IronBarcode 中動態更改閱讀速度設置嗎?

是的,您可以在 IronBarcode 中動態調整閱讀速度設置,以適應不同的掃描條件和需求。

閱讀速度如何影響 IronBarcode 中條碼掃描的性能?

IronBarcode 中的閱讀速度設置會影響條碼處理的速度,更高的速度可能降低準確性,而較低的速度則增強了精確性。

Curtis Chau
技術作家

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

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

準備好開始了嗎?
Nuget 下載 1,979,979 | Version: 2025.11 剛發表