如何使用 IronBarcode 调整 C# 的阅读速度

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

IronBarcode 提供四种读取速度选项(Faster, Balanced, Detailed, ExtremeDetail),让您在 C# 中读取条形码时控制处理速度和准确性之间的权衡,其中 Balanced 是大多数应用程序的推荐起点。

简介

在读取大量条形码时,准确性至关重要,但资源分配和处理效率也是同样重要的考虑因素。 输入图像的质量决定了条形码阅读器应如何处理这些图像--是跳过清晰图像的预处理,还是使用更耗费资源的选项来提高降级条形码的准确性。

IronBarcode 可灵活选择处理速度和准确度级别,让您控制条码读取过程的方方面面。 您可以根据输入的图像和可用资源做出决定。 如需了解更高级的条形码读取场景,请浏览我们的综合条形码读取教程,其中涵盖了各种格式和技术。

本文提供了针对不同情况选择最佳阅读速度的指南。 我们将使用二维码样本来演示改变阅读速度对结果的影响。 如果您要专门处理 QR 代码,请查看我们的 C# QR 代码生成器教程,以创建测试样本。

快速入门:以均衡速度读取条形码

使用 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 选项:Faster, Balanced, Detailed, 和 ExtremeDetail. 我们将使用一个包含大部分降级条形码图像和一些清晰图像的样本集来检查每个选项,以展示库的功能。 有关支持格式的完整列表,请访问我们的支持的条形码格式页面

我们将使用 .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");
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%。 这种方法虽然速度快,但只适合原始图像条件。 在处理单个图像中的多个条形码时,请考虑我们的读取多个条形码指南,以获得最佳配置。

为什么平衡是推荐的速度选项?

平衡选项兼顾了准确性和阅读性能。 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 个条码中检测出 237 个条码结果。 与 Faster 相比,它提供了 55.11% 的准确率--显著提高了准确率,而时间仅略有增加。 该选项在内存和速度之间保持了有效的平衡,是大多数情况下的理想选择,也是推荐的起点。 这种平衡的方法与适当的图像预处理技术配合使用效果尤佳。

何时需要详细速度选项?

当图像严重模糊或扭曲,且 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");
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 个条码结果。 其在严重降级的 BarCode 上 55.11% 的成功率证明了其准确性。 不过,由于处理时间大大增加,因此这一方案应专门用于降级的 BarCode 图像。 在处理不完全条形码时,请查阅我们的不完全条形码处理示例,了解更多策略。

哪些情况需要极高的细节速度?

ExtremeDetail 设置对条形码图像进行了大量处理,大大降低了读取性能。 这种 CPU 密集型选项最适合扫描一个输入文件中多个不清晰或模糊的 BarCode。当其他选项无法产生预期结果时,请将其作为最后手段使用。 对于大批量处理场景,可探索 从 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 个。 虽然在严重降级的 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万) Detailed 相比 +32.08

如何为我的应用程序选择合适的速度?

根据上述比较,从 Faster 设置开始,依次为 Balanced, DetailedExtremeDetail 以确定显著的输出差异。 在大多数情况下,Balanced 可以充分处理一切问题。 仅对严重扭曲的图像使用 DetailedExtremeDetail

尽管DetailedExtremeDetail应用了中度和重度处理,但有时拆分处理--在条形码读取前手动应用图像过滤器--比使用单一处理更有效。 有关图像预处理的更多信息,请参阅本指南

哪种速度设置符合我的使用案例?

根据图像质量选择采样速度的决策树,从快速到详细+ExtremeDetail 选项

常见问题解答

有哪四种条形码读取速度可供选择?

IronBarcode 提供四种 ReadingSpeed 选项:更快、均衡、详细和极致详细。每个选项都在处理速度和准确性之间提供了不同的平衡,平衡是大多数应用程序的推荐起点。

扫描 BarCode 时如何设置读取速度?

您可以使用 IronBarcode 中的 BarcodeReaderOptions 类设置读取速度。只需创建一个新的 BarcodeReaderOptions 对象,并将 Speed 属性设置为所需的 ReadingSpeed 值(Faster、Balanced、Detailed 或 ExtremeDetail),然后将其传递给 Read 方法。

我应该为我的应用程序选择哪种阅读速度?

IronBarcode 建议大多数应用从平衡速度设置开始。如果您拥有高质量、清晰的条码图像,则可以使用 "更快 "模式。对于质量下降或较差的图像,可考虑使用 "详细 "或 "极详细 "模式,以获得更高的准确性。

不同阅读速度选项之间的权衡是什么?

IronBarcode 读取速度的权衡在于处理速度和准确性之间。更快模式能快速处理图像,但可能会漏掉质量较差图像中的条形码。ExtremeDetail 模式提供最高的准确性,但需要更多的处理时间和内存资源。

我可以用不同的速度设置读取多种条形码格式吗?

是的,IronBarcode 支持读取各种条码格式,包括所有速度设置下的 QR 码。速度设置会影响处理方式,但不会限制您可以读取的条码类型。请访问支持的条码格式页面以获取完整列表。

图像质量如何影响我应该选择的阅读速度?

图像质量直接影响您在 IronBarcode 中的速度选择。清晰、高质量的条码图像可通过 "更快 "模式进行高效处理。退化、模糊或对比度低的图像则需要使用 "详细 "或 "极详细 "模式,以确保条码检测和读取的准确性。

读取有速度选项的 BarCode 的最基本工作流程是什么?

IronBarcode 的最低工作流程包括 5 个步骤:1)下载 C# 库;2)使用 BarcodeReaderOptions 设置读取速度;3)使用图像路径调用读取方法;4)提取并打印条码值;5)评估不同速度之间的性能权衡。

如何衡量不同阅读速度对性能的影响?

IronBarcode 在不同读取速度下的性能可以使用 .NET 基准库进行测量,以跟踪处理时间和内存使用情况。这有助于您根据具体使用情况和资源限制确定最佳速度设置。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 2,070,733 | 版本: 2026.2 刚刚发布