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

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

IronBarcode 提供四種讀取速度選項(ExtremeDetail),讓您在 C# 環境中讀取 BarCode 時,能靈活掌控處理速度與精準度之間的權衡,其中 Balanced 是多數應用程式的建議起始設定。

簡介

在讀取大量BarCode時,準確性至關重要,但資源分配與處理效率同樣是重要的考量因素。 輸入影像的品質決定了條碼讀取器應如何處理這些影像——對於清晰的影像,可跳過預處理步驟;對於品質較差的BarCode,則需採用更耗資源的選項來提升辨識準確度。

IronBarcode 提供靈活的處理速度與精確度選項,讓您能全面掌控 BarCode 讀取流程的每個環節。 您可以根據您提供的圖片及可用資源來做出判斷。 若需處理更進階的BarCode讀取情境,請參閱我們的完整BarCode讀取教學指南,內容涵蓋多種格式與技術。

本文提供針對不同情境選擇最佳閱讀速度的指引。 我們將使用 QR 碼範例,展示閱讀速度的變化如何影響結果。 若您專門處理 QR 碼相關工作,請參閱我們的 C# QR 碼產生器教學,以建立測試範例。

快速入門:以平衡的速度讀取BarCode

使用 IronBarcode 的 BarcodeReaderOptions 即可立即為您的掃描設定 Speed 等級。 此範例展示如何透過 Balanced 設定快速讀取 BARCODE,以獲得快速且可靠的結果。

  1. using 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 選項:Detailed 以及 ExtremeDetail。 我們將透過一套樣本集來檢視各項選項,該樣本集主要包含畫質受損的BarCode圖像,並摻雜部分清晰圖像,以展示該函式庫的功能。 如需查看完整支援格式清單,請造訪我們的"支援BarCode格式"頁面

我們將使用 .NET 基準測試函式庫來測量處理時間與記憶體使用量,藉此比較各選項的表現,並找出適合各讀取速度的理想應用情境。 我們將展示基準測試程式碼,以及一種用於統計成功讀取的退化BarCode的簡易方法。 有關設定讀取器選項的更多詳細資訊,請參閱我們的BarCode讀取器設定範例

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

Faster 選項可在消耗最少資源的情況下提供最快的 BARCODE 讀取速度,但會降低準確度。 此流程省略了影像預處理步驟,當輸入影像已具備清晰銳利度時,效果最佳。

此範例將 Speed 屬性設定為 ReadingSpeed.Faster,匯入一個 BARCODE 目錄,並列印所找到的 BARCODE 及其值、類型與每張圖像中的 BARCODE 數量。 若要更深入了解如何從各種圖像格式讀取BARCODE,請參閱我們關於從圖像讀取BARCODE的指南。

: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");
Imports IronBarCode
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Linq

Dim optionsFaster As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.Faster
}

' Directory containing PDF files
Dim folderPath As String = "YOUR_FILE_PATH"

' Get all PDF files in the directory
Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg")

Dim countFaster As Integer = 0
Dim stopwatch As Stopwatch = Stopwatch.StartNew()
For Each file In pdfFiles
    ' Read the barcode
    Dim results = BarcodeReader.Read(file, optionsFaster)

    If results.Any() Then
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}")
        For Each result In results
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}")
            countFaster += 1
        Next
    Else
        Console.WriteLine($"No barcode found in: {Path.GetFileName(file)}")
    End If
Next

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 個 BarCode 中偵測到 146 個結果,準確度達 33.95%。 雖然速度快,但此方法僅適用於圖像狀態完好的情況。 若需處理單一影像中的多個BarCode,請參考我們的《讀取多個BarCode指南》,以獲得最佳設定方案。

為何"平衡"是推薦的速率選項?

Balanced 選項在準確性與可讀性之間取得平衡。 IronBarcode 運用簡易的影像處理技術來清晰化 BarCode 區域,使其更容易被偵測與讀取。 此設定適用於大多數現代影像,因為輕度處理通常能產生精確的結果。

讓我們使用相同的圖片來示範 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");
Imports IronBarCode
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Linq

Dim optionsFaster As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.Balanced
}

' Directory containing PDF files
Dim folderPath As String = "YOUR_FILE_PATH"

' Get all PDF files in the directory
Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg")

Dim countFaster As Integer = 0
Dim stopwatch As Stopwatch = Stopwatch.StartNew()
For Each file In pdfFiles
    ' Read the barcode
    Dim results = BarcodeReader.Read(file, optionsFaster)

    If results.Any() Then
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}")
        For Each result In results
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}")
            countFaster += 1
        Next
    Else
        Console.WriteLine($"No barcode found in: {Path.GetFileName(file)}")
    End If
Next

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 個 BARCODE 中偵測到 237 個結果。 其準確度達 55.11%——相較於 Faster 有顯著提升——且僅需稍許額外時間。 此選項在記憶體與速度之間取得有效平衡,使其成為大多數情況下的理想選擇,也是建議的起點。 這種平衡的處理方式,搭配適當的影像預處理技術,效果尤為顯著。

何時需要使用"詳細速度"選項?

當圖片嚴重模糊或變形,且 Balanced 無法產生清晰結果時,請使用 Detailed 選項。 它會進行中度的預處理,以釐清BarCode區域並降低數位雜訊,從而提升偵測效果。 若遇到嚴重損壞的圖片,請參閱我們的圖片修正指南,其中涵蓋了各種預處理技術。

讓我們套用 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");
Imports IronBarCode
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Linq

Dim optionsFaster As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.Detailed
}

' Directory containing PDF files
Dim folderPath As String = "YOUR_FILE_PATH"

' Get all PDF files in the directory
Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg")

Dim countFaster As Integer = 0
Dim stopwatch As Stopwatch = Stopwatch.StartNew()
For Each file In pdfFiles
    ' Read the barcode
    Dim results = BarcodeReader.Read(file, optionsFaster)

    If results.Any() Then
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}")
        For Each result In results
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}")
            countFaster += 1
        Next
    Else
        Console.WriteLine($"No barcode found in: {Path.GetFileName(file)}")
    End If
Next

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 個 BARCODE 中偵測到 237 個結果。 其在嚴重損毀 BARCODE 上達 55.11% 的成功率,充分展現其精準度。 然而,由於處理時間大幅增加,此選項應僅限於品質較差的BARCODE影像。 若需處理不完整的BARCODE,請參閱我們的"不完整BARCODE處理範例"以獲取更多策略。

哪些情況需要 ExtremeDetail Speed?

ExtremeDetail 設定會對 BARCODE 影像進行大量處理,導致讀取效能大幅降低。 此選項需大量 CPU 運算資源,最適合用於掃描單一輸入檔案中多個模糊不清的 BARCODE。當其他選項無法產生預期結果時,請將其作為最後手段使用。 針對大量處理的情境,建議探索從 PDF 檔案中讀取 BarCode 的功能,因為每頁 PDF 檔案通常包含多個 BarCode。

讓我們套用 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");
Imports IronBarCode
Imports System
Imports System.Diagnostics
Imports System.IO
Imports System.Linq

Dim optionsFaster As New BarcodeReaderOptions With {
    .Speed = ReadingSpeed.ExtremeDetail
}

' Directory containing PDF files
Dim folderPath As String = "YOUR_FILE_PATH"

' Get all PDF files in the directory
Dim pdfFiles = Directory.GetFiles(folderPath, "*.jpg")

Dim countFaster As Integer = 0
Dim stopwatch As Stopwatch = Stopwatch.StartNew()

For Each file In pdfFiles
    ' Read the barcode
    Dim results = BarcodeReader.Read(file, optionsFaster)

    If results.Any() Then
        Console.WriteLine($"Barcode(s) found in: {Path.GetFileName(file)}")
        For Each result In results
            Console.WriteLine($"  Value: {result.Value}, Type: {result.BarcodeType}")
            countFaster += 1
        Next
    Else
        Console.WriteLine($"No barcode found in: {Path.GetFileName(file)}")
    End If
Next

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 張 BarCode 圖像中的 313 張。 儘管在嚴重損毀的BARCODE上達到了令人印象深刻的 72.79% 準確度,但其高資源消耗使其僅適合作為最後的手段。 建議在使用此選項前先對圖片進行預處理。

不同速度的比較如何?

模式 發現BarCode 平均時間 每份檔案所需時間 GC 壓力 準確性提升
更快 147/430 (33.95%) 25 秒 0.058 秒 高 (Gen2: 177K) 基準版本
平衡 237/430 (55.11%) 43 秒 0.1 秒 高 (Gen2: 151K) +62.32% 對比 Faster
詳細說明 237/430 (55.11%) 5.50 分鐘 0.767 秒 非常高 (Gen2: 297K) +0% vs 平衡
ExtremeDetail 313/430 (72.79%) 10.14 分鐘 1.414 秒 Extreme (Gen2: 4.74M) +32.08% 對比 詳細

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

根據上述比較,請從 Faster 設定開始,依序測試 DetailedExtremeDetail,以辨識輸出結果的顯著差異。 在大多數情況下,Balanced 已能妥善處理所有需求。 僅在圖片嚴重變形時使用 DetailedExtremeDetail。 對於線條稀疏或品質較差的BarCode,請將速度設定與 MinScanLines = 1 結合使用,以提高偵測靈敏度。

雖然 DetailedExtremeDetail 會進行中等與高強度的處理,但有時將流程拆分會更有效率——例如在讀取 BARCODE 前手動套用影像濾鏡,而非使用單一流程。 有關影像預處理的更多資訊,請參閱此指南

哪種速度設定適合我的使用情境?

根據影像品質選擇取樣速度的決策樹,從

常見問題

有哪些可用的四種條碼讀取速度選項?

IronBarcode提供四種ReadingSpeed選項:快速、平衡、詳細和極致詳細。每種選項在處理速度和準確性之間提供不同的平衡,其中平衡是大多數應用程式的推薦起點。

如何設定掃描條碼時的讀取速度?

您可以在IronBarcode中使用BarcodeReaderOptions類別設定讀取速度。簡單地創建一個新的BarcodeReaderOptions物件,並將Speed屬性設置為您所需的ReadingSpeed值(快速、平衡、詳細或極致詳細),然後傳遞給Read方法。

我應該為我的應用程式使用哪種讀取速度選項?

IronBarcode建議使用平衡速度設置作為大多數應用程式的起點。如果您有高質量、清晰的條碼圖像,您可以使用快速模式。對於劣化或質量不佳的圖像,考慮使用詳細或極致詳細模式以獲得更好的準確性。

不同讀取速度選項之間的取捨是什麼?

IronBarcode的讀取速度的取捨在於處理速度和準確性之間。快速模式能快速處理圖像,但可能會在質量不佳的圖像中漏掉條碼。極致詳細模式提供最高的準確性,但需要更多的處理時間和內存資源。

我可以用不同的速度設置閱讀多種條碼格式嗎?

是的,IronBarcode支援使用所有速度設置讀取多種條碼格式,包括QR Code。速度設置會影響處理方式,但不會限制您可以讀取的條碼類型。訪問支援條碼格式頁面以獲得完整列表。

圖像質量如何影響我應選擇的讀取速度?

圖像質量直接影響您在IronBarcode中的速度選擇。清晰、高質量的條碼圖像可以用快速模式高效處理。退化的、模糊的或對比度低的圖像需要使用詳細或極致詳細模式以確保準確的條碼檢測和讀取。

使用速度選項讀取條碼的最小化工作流程是什麼?

使用IronBarcode的最小化工作流程包含5個步驟:1)下載C#程式庫,2)使用BarcodeReaderOptions設定讀取速度,3)用您的圖像路徑調用Read方法,4)提取並打印條碼值,5)評估不同速度之間的效能取捨。

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

IronBarcode在不同讀取速度下的效能可以使用.NET基準測試庫來衡量,以跟踪處理時間和內存使用。這能幫助您確定最佳速度設置來滿足您的特定使用案例和資源限制。

將IronBarcode實現於專案中需要什麼程式設計技能?

基本的C#程式設計知識足以將IronBarcode實現於專案中,因為它提供了簡單的方法和全面的文檔來指導開發者。

IronBarcode適合於小型專案和大型企業應用嗎?

IronBarcode設計為可擴展且多功能,使其適合小型專案和需要強大條碼解決方案的大型企業應用。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備好開始了嗎?
Nuget 下載 2,240,258 | 版本: 2026.5 just released
Still Scrolling Icon

還在捲動嗎?

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