如何使用 IronBarcode 调整 C# 的阅读速度
IronBarcode 提供了四种读取速度选项(Faster, ExtremeDetail),让您在 C# 中读取 BarCode 时能够控制处理速度与准确性之间的权衡,其中 Balanced 是大多数应用程序的推荐起始值。
简介
在读取大量条形码时,准确性至关重要,但资源分配和处理效率也是同样重要的考虑因素。 输入图像的质量决定了条形码阅读器应如何处理这些图像--是跳过清晰图像的预处理,还是使用更耗费资源的选项来提高降级条形码的准确性。
IronBarcode 可灵活选择处理速度和准确度级别,让您控制条码读取过程的方方面面。 您可以根据输入的图像和可用资源做出决定。 如需了解更高级的条形码读取场景,请浏览我们的综合条形码读取教程,其中涵盖了各种格式和技术。
本文提供了针对不同情况选择最佳阅读速度的指南。 我们将使用二维码样本来演示改变阅读速度对结果的影响。 如果您要专门处理 QR 代码,请查看我们的 C# QR 代码生成器教程,以创建测试样本。
快速入门:以平衡的速度读取BarCode
使用 IronBarcode 的 BarcodeReaderOptions 即可立即为您的扫描设置 Speed 级别。 此示例演示了如何使用 Balanced 设置快速读取 BarCode,以获得快速且可靠的结果。
最小工作流程(5 个步骤)
- 下载 C# 库以调整阅读速度
- 使用BarcodeReaderOptions类设置读取速度
- 使用
Read方法从各种图像格式中提取条形码值 - 打印出条形码的值
- 评估不同阅读速度之间的性能权衡
有哪些不同的阅读速度选项?
IronBarcode 提供四种 ReadingSpeed 选项:Detailed 和 ExtremeDetail。 我们将使用一个包含大部分降级条形码图像和一些清晰图像的样本集来检查每个选项,以展示库的功能。 有关支持格式的完整列表,请访问我们的支持的条形码格式页面。
我们将使用 .NET 基准库来测量处理时间和内存使用情况,展示每种选项的比较情况,并确定每种阅读速度的理想场景。 我们将演示基准代码和计算成功读取降级条形码的直接方法。 有关配置阅读器选项的更多详情,请参阅我们的条码阅读器设置示例。
何时应使用更快的速度选项?
Faster 选项可在消耗最少资源的情况下提供最快的 BarCode 读取速度,但会降低准确性。 该流程跳过了图像预处理,在输入图像已经清晰锐利的情况下效果最佳。
此示例将 Speed 属性设置为 ReadingSpeed.Faster,导入一个 BARCODE 目录,并 PRINT 出找到的 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")
Faster 选项在 25 秒内从 430 个 BARCODE 中检测出 146 个结果,准确率为 33.95%。 这种方法虽然速度快,但只适合原始图像条件。 在处理单个图像中的多个条形码时,请考虑我们的读取多个条形码指南,以获得最佳配置。
为什么平衡是推荐的速度选项?
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")
Balanced 选项在 43 秒内从 430 个 BARCODE 中检测到 237 个匹配结果。 其准确率达到 55.11%——相较于 Faster 有了显著提升——且耗时仅略有增加。 该选项在内存和速度之间保持了有效的平衡,是大多数情况下的理想选择,也是推荐的起点。 这种平衡的方法与适当的图像预处理技术配合使用效果尤佳。
何时需要详细速度选项?
当图像严重模糊或变形,且 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")
Detailed 选项在 5 分 30 秒内从 430 个 BARCODE 中检测到 237 个结果。 其在严重降级的 BarCode 上 55.11% 的成功率证明了其准确性。 不过,由于处理时间大大增加,因此这一方案应专门用于降级的 BarCode 图像。 在处理不完全条形码时,请查阅我们的不完全条形码处理示例,了解更多策略。
哪些情况需要极高的细节速度?
ExtremeDetail 设置会对 BarCode 图像进行大量处理,从而显著降低读取性能。 这种 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")
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万) | 与 Detailed 相比 +32.08 |
如何为我的应用程序选择合适的速度?
根据上述对比,请从 Faster 设置开始,依次测试 Detailed 和 ExtremeDetail,以识别输出结果中的显著差异。 在大多数情况下,Balanced 已能充分满足需求。 仅对严重变形的图片使用 Detailed 和 ExtremeDetail。 对于较细或质量较差的BarCode,请将速度设置与 MinScanLines = 1 结合使用,以提高检测灵敏度。
尽管 Detailed 和 ExtremeDetail 分别执行中等和重度处理,但有时拆分处理流程会更高效——即在读取 BARCODE 前手动应用图像滤镜,而非使用单一处理流程。 有关图像预处理的更多信息,请参阅本指南。
哪种速度设置符合我的使用案例?
常见问题解答
有哪四种条形码读取速度可供选择?
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 基准库进行测量,以跟踪处理时间和内存使用情况。这有助于您根据具体使用情况和资源限制确定最佳速度设置。

