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

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

IronBarcode提供四種讀取速度選項(Faster, Balanced, Detailed, Balanced

介紹

當讀取大量條碼時,準確性是必需的,而資源分配和處理效率同樣是重要的考量因素。 輸入圖像的質量決定了條碼讀取器應該如何處理它們——是否為清晰的圖像跳過預處理,或者使用更資源密集的選項來提高受損條碼的準確性。

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

本文提供選擇不同場景最佳讀取速度的指南。 我們將使用QR碼樣本來演示改變讀取速度如何影響結果。 如果您專門處理QR碼,請查看我們的C# QR Code Generator教程以建立測試樣本。

快速入門:用平衡速度讀取條碼

使用IronBarcode的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提供四種Faster, Balanced, Detailed, 和ExtremeDetail。 我們將使用一個樣本集來檢查每個選項,主要包含受損的條碼圖像,並且還有一些清晰的圖像以展示程式庫的功能。 欲獲取支持格式的完整列表,請存取我們的支持的條碼格式頁面

我們將使用.NET基準程式庫來衡量處理時間和記憶體使用情況,展示每個選項的比較,並指出每個讀取速度的理想情景。 我們將演示基準測試程式碼和一種簡單的方法來計算成功讀取的受損條碼。 有關讀取器選項配置的更多詳細資訊,請參閱我們的條碼讀取器設置範例

什麼時候應該使用更快的速度選項?

Faster選項提供了最快的條碼讀取速度,但所需資源減少,準確性降低。 此過程跳過圖像預處理,最適合已經清晰的輸入圖像。

此範例將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");
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個條碼中的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");
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個條碼中的237個。 它達到了55.11%的準確率——比Faster有很大改善——只有稍微增加的時間。 此選項在記憶體和速度之間保持了有效的平衡,使其在大多數情況下理想且作為推薦的起始點。 這種平衡的方法特別適合與適當的圖像預處理技術配合使用。

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

當圖像嚴重模糊或失真且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");
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個條碼中的237個。 其在嚴重退化的條碼上達到了55.11%的成功率,證明了其準確性。 然而,顯著增加的處理時間意味著此選項應僅用於退化的條碼圖像。 當處理不完美的條碼時,請參閱我們的不完美條碼處理範例以獲得其他策略。

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

ExtremeDetail設置對條碼圖像進行重度處理,顯著降低讀取性能。 這種CPU密集型選項最適合掃描一個輸入文件中多個不清楚或模糊的條碼。當其他選擇無法產生期望結果時,作為最後手段使用。 對於大容量處理場景,請探索從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個條碼圖像中的313個。 雖然對於嚴重退化的條碼達到了令人印象深刻的72.79%準確率,但其高資源消耗使其僅適合作為最後的辦法。 考慮在使用此選項之前對圖像進行預處理。

不同速度如何比較?

模式 找到的條碼 平均時間 每文件時間 GC壓力 準確性增益
更快 147/430 (33.95%) 25秒 0.058秒 高(Gen2: 177K) Baseline
平衡 237/430 (55.11%) 43秒 0.1秒 高(Gen2: 151K) +62.32% 相較於 更快
詳細 237/430 (55.11%) 5.50分鐘 0.767秒 非常高(Gen2: 297K) +0% 相較於 平衡
極端詳細 313/430 (72.79%) 10.14分鐘 1.414秒 極端(Gen2: 4.74M) +32.08% 相較於 詳細

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

根據上面的比較,從ExtremeDetail以識別顯著的輸出差異。 對於大多數情況,Balanced能夠充分處理所有問題。 僅在圖像嚴重失真時使用ExtremeDetail。 對於薄的或低品質的條碼, 將您的速度設置與MinScanLines = 1結合使用以增強檢測靈敏度。

儘管ExtremeDetail應用了中等和重度處理,有時候將過程拆分更為有效——在讀取條碼之前手動應用圖像過濾器,而不是使用單一流程。 想了解更多有關圖像預處理的資訊,請參考這個指南

哪種速度設置適合我的用例?

基於圖像質量從更快到詳細+極端詳細選項的取樣速度選擇決策樹

常見問題

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

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

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

您可以使用IronBarcode中的BarcodeReaderOptions類別來設置讀取速度。只需建立一個新的BarcodeReaderOptions物件,並將Speed屬性設置為您所需的ReadingSpeed值(快速、均衡、詳細或極端詳細),然後將其傳遞給Read方法。

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

IronBarcode建議大多數應用程式從均衡速度設置開始。如果您擁有高品質、清晰的條碼影像,可以使用快速模式。對於劣化或低質量影像,考慮使用詳細或極端詳細模式以獲得更好的準確性。

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

在IronBarcode的讀取速度中,取捨在於處理速度和準確性之間。快速模式能快速處理影像,但可能會錯過劣質影像中的條碼。極端詳細模式提供最高的準確性,但需要更多的處理時間和記憶體資源。

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

可以的,IronBarcode支持讀取包括QR碼在內的各種條碼格式,速度設置會影響處理方式,但不限制可讀取的條碼型別。請存取支持的條碼格式頁面查看完整清單。

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

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

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

使用IronBarcode的最小工作流程共分為五個步驟:1) 下載C#程式庫,2) 使用BarcodeReaderOptions設置讀取速度,3) 使用您的影像路徑調用Read方法,4) 提取並列印條碼值,5) 評估不同速度之間的性能取捨。

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

可以使用.NET基準測試程式庫來衡量IronBarcode在不同讀取速度下的性能,以追踪處理時間和記憶體使用情況。這有助於您識別出最適用於您的特定使用案例和資源限制的最佳速度設置。

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

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

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

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

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

準備好開始了嗎?
Nuget 下載 2,331,688 | 版本: 2026.7 剛剛發布
Still Scrolling Icon

還在滾動嗎?

想快速驗證嗎? PM > Install-Package BarCode
運行範例觀看您的字串成為條碼。