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

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

IronBarcode 提供四種讀取速度選項 (Faster, ExtremeDetail),讓您在 C# 中讀取 BARCODE 時,能控制處理速度與精確度之間的權衡,其中 Balanced 是大多數應用程式的建議起始點。

介紹

在讀取大量 BarCode 時,精確度是不可或缺的,但資源分配和處理效率也是同樣重要的考慮因素。 輸入影像的品質決定了條碼讀取器應如何處理這些影像-是跳過清晰影像的預處理,還是使用更耗費資源的選項來提高劣質條碼的精確度。

IronBarcode 提供了選擇處理速度和精確度等級的靈活性,讓您可以控制條碼讀取過程的每個方面。 您可以根據輸入的影像和可用資源來做決定。 如需更進階的條碼讀取情境,請探索我們的 全面條碼讀取教學,其中涵蓋各種格式和技術。

本文提供針對不同情境選擇最佳閱讀速度的指引。 我們會使用 QR 碼樣本來示範改變閱讀速度會如何影響結果。 如果您要特別處理 QR 碼,請查看我們的 C# QR 碼產生器教學,以建立測試範例。

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

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

  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 選項:Detailed 以及 ExtremeDetail。 我們會使用一個樣本集來檢視每個選項,該樣本集包含大部分已降級的 BarCode 影像與一些清晰的影像,以展示資料庫的功能。 如需支援格式的完整清單,請造訪我們的 支援的 BarCode 格式頁面

我們會使用 .NET 基準函式庫來測量處理時間和記憶體使用量,顯示每種選項的比較,並找出每種閱讀速度的理想情況。 我們將示範基準程式碼,以及計算成功讀取退化 BarCode 的直接方法。 有關配置閱讀器選項的詳細資訊,請參閱我們的 BarCode 閱讀器設定範例

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

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

此範例將 Speed 屬性設定為 ReadingSpeed.Faster,匯入一個 BARCODE 目錄,並 PRINT 所找到的 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 時,請考慮我們的 讀取多個條碼指南,以獲得最佳配置。

為什麼 Balanced 是推薦的速度選項?

Balanced 選項在準確性與可讀性之間取得平衡。 IronBarcode 應用輕度影像處理來釐清條碼區域,使其更容易偵測和讀取。 對於大多數的現代影像,建議使用此設定,因為輕度處理通常可以產生精確的結果。

讓我們使用相同的圖片來示範 Balanced 如何影響輸出結果。 關於異步操作,請探索我們的 async 和多執行緒與 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 影像。 在處理不完全條碼時,請參閱我們的 不完全條碼處理範例,以瞭解其他策略。

哪些情況需要極端詳細的速度?

ExtremeDetail 設定會對 BarCode 影像進行大量處理,導致讀取效能大幅下降。 此 CPU 密集型選項最適用於掃描同一輸入檔案中多個不清楚或模糊的 BarCode。當其他選項無法達到預期效果時,可將其作為最後的選擇。 針對大量處理的情境,探索 從 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");
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% 準確度,但其高資源消耗使其只適合作為最後的手段。 使用此選項之前,請考慮先預先處理影像。

不同的速度如何比較?

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

如何為我的應用程式選擇正確的速度?

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

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

哪種速度設定符合我的使用個案?

根據影像品質選擇取樣速度的決策樹,從快速到詳細+極致詳細選項

常見問題解答

四種可用的 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,145,441 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

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