如何用 C# 一次读取多个条形码
IronBarcode通过设置 ExpectMultipleBarcodes = true,能够同时从图像和 PDF 中读取多个条形码,从而简化物流、零售和库存管理应用程序的数据处理。 无论是构建仓库系统、零售销售点应用程序,还是文档处理解决方案,IronBarcode 的高级读取功能都能提供您所需的可靠性和性能。
快速入门:轻松读取图像中的所有条形码
本示例展示了如何快速使用 IronBarcode 扫描图像中包含的每一个条形码。 只需在所需的条形码类型旁边设置 ExpectMultipleBarcodes = true 即可——无需样板代码,轻松便捷。
-
使用 NuGet 包管理器安装 https://www.nuget.org/packages/BarCode
PM > Install-Package BarCode -
复制并运行这段代码。
var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional }); -
部署到您的生产环境中进行测试
通过免费试用立即在您的项目中开始使用IronBarcode
最小工作流程(5 个步骤)
- 下载用于读取多个条形码的 C# 库
- 使用`Read`方法从各种图像格式中提取条形码值
- 利用 `ExpectMultipleBarcodes` 属性配置读取单个或多个条形码
- 将 `ExpectMultipleBarcodes` 属性设置为 false 以提高性能
- 打印出条形码的值
如何从图像中读取多个 BarCode?
默认情况下,IronBarcode 会持续扫描文档以读取多个条形码。 然而,曾经出现过即使存在多个条形码,也只返回一个条形码值的情况。 为了解决这个问题,请自定义设置,以便能够读取多个 BarCode,如下图所示。 ExpectMultipleBarcodes 属性同时存在于 BarcodeReaderOptions 和 PdfBarcodeReaderOptions 类中,允许您将其用于 读取图像和 PDF 文档中的条形码。
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-read-multiple-barcodes.cs
using IronBarCode;
using System;
// Set the option to read multiple barcodes
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
Imports IronBarCode
Imports System
' Set the option to read multiple barcodes
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next result
将 ExpectMultipleBarcodes 设置为 true 可使 IronBarcode 扫描整个文档以获取多个条形码,并将其存储在 BarcodeResults 变量中。 使用 foreach 循环,您可以轻松访问所有 BarCode 值并将其打印到控制台。
高级多重条形码读取场景
在处理多个 BarCode 时,可能会遇到需要额外配置的情况。 下面是一个综合示例,演示如何从复杂的文档中读取不同格式的多个 BarCode:
using IronBarCode;
using System;
using System.Linq;
// Configure advanced options for mixed barcode types
BarcodeReaderOptions advancedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
// Read both 1D and 2D barcodes
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional | BarcodeEncoding.QRCode | BarcodeEncoding.DataMatrix,
// Apply image correction filters for better accuracy
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new ContrastFilter()
},
// Set speed vs accuracy balance
Speed = ReadingSpeed.Balanced
};
// Read from various sources
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
var pdfResults = BarcodeReader.ReadPdf("document-with-barcodes.pdf", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Confidence: {result.Confidence}%");
Console.WriteLine($"Page: {result.PageNumber}");
Console.WriteLine("---");
}
using IronBarCode;
using System;
using System.Linq;
// Configure advanced options for mixed barcode types
BarcodeReaderOptions advancedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = true,
// Read both 1D and 2D barcodes
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional | BarcodeEncoding.QRCode | BarcodeEncoding.DataMatrix,
// Apply image correction filters for better accuracy
ImageFilters = new ImageFilterCollection() {
new SharpenFilter(),
new ContrastFilter()
},
// Set speed vs accuracy balance
Speed = ReadingSpeed.Balanced
};
// Read from various sources
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
var pdfResults = BarcodeReader.ReadPdf("document-with-barcodes.pdf", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
Console.WriteLine($"Confidence: {result.Confidence}%");
Console.WriteLine($"Page: {result.PageNumber}");
Console.WriteLine("---");
}
Imports IronBarCode
Imports System
Imports System.Linq
' Configure advanced options for mixed barcode types
Dim advancedOptions As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = True,
' Read both 1D and 2D barcodes
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional Or BarcodeEncoding.QRCode Or BarcodeEncoding.DataMatrix,
' Apply image correction filters for better accuracy
.ImageFilters = New ImageFilterCollection() From {
New SharpenFilter(),
New ContrastFilter()
},
' Set speed vs accuracy balance
.Speed = ReadingSpeed.Balanced
}
' Read from various sources
Dim imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions)
Dim pdfResults = BarcodeReader.ReadPdf("document-with-barcodes.pdf", advancedOptions)
' Process results with error handling
For Each result In imageResults
Console.WriteLine($"Barcode Type: {result.BarcodeType}")
Console.WriteLine($"Value: {result.Value}")
Console.WriteLine($"Confidence: {result.Confidence}%")
Console.WriteLine($"Page: {result.PageNumber}")
Console.WriteLine("---")
Next
这个高级示例展示了几个重要功能:
- 支持混合条形码格式:结合不同的条形码编码类型。
- 图像校正过滤器:使用图像过滤器提高阅读准确性
- 阅读速度优化:利用阅读速度选项平衡速度和准确性
- 信心分数:访问每个检测到的 BarCode 的置信度阈值</a
如何读取单个条形码以获得更好的性能?
IronBarcode 可读取图像或 PDF 中的单个和多个条形码。 默认情况下,即使只有一个 BarCode,引擎也会扫描整个文档。 为提高读取单个条形码时的性能,请将 ExpectMultipleBarcodes 设置为 false。 这样,引擎在检测到第一个条形码后就不会再扫描整个文档,从而加快了条形码的检索速度。 下面的代码演示了这种方法。
:path=/static-assets/barcode/content-code-examples/how-to/read-multiple-barcodes-read-single-barcode.cs
using IronBarCode;
using System;
// Set the option to read single barcode
BarcodeReaderOptions options = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional,
};
// Read barcode
var results = BarcodeReader.Read("testbc1.png", options);
foreach (var result in results)
{
Console.WriteLine(result.ToString());
}
Imports IronBarCode
Imports System
' Set the option to read single barcode
Private options As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.AllOneDimensional
}
' Read barcode
Private results = BarcodeReader.Read("testbc1.png", options)
For Each result In results
Console.WriteLine(result.ToString())
Next result
在此示例中,我们使用了与之前相同的带有多个 BarCode 的图片,但将 ExpectMultipleBarcodes 设置为 false。 因此,只会返回第一个条形码值,扫描过程在检索到第一个条形码后停止。
使用裁剪区域优化单个条形码读取
为了在读取单个条形码时获得更好的性能,请将 ExpectMultipleBarcodes = false 设置与 裁剪区域规范相结合。 当您知道 BarCode 的大致位置时,这种技术就特别有用:
using IronBarCode;
using IronSoftware.Drawing;
// Define a crop region where the barcode is likely located
var cropRegion = new Rectangle(100, 100, 300, 200);
// Configure options for optimal single barcode reading
BarcodeReaderOptions optimizedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.Code128,
CropArea = cropRegion,
Speed = ReadingSpeed.Faster
};
// Read with optimized settings
var result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault();
if (result != null)
{
Console.WriteLine($"Barcode found: {result.Value}");
Console.WriteLine($"Read time: {result.ReadTime}ms");
}
using IronBarCode;
using IronSoftware.Drawing;
// Define a crop region where the barcode is likely located
var cropRegion = new Rectangle(100, 100, 300, 200);
// Configure options for optimal single barcode reading
BarcodeReaderOptions optimizedOptions = new BarcodeReaderOptions()
{
ExpectMultipleBarcodes = false,
ExpectBarcodeTypes = BarcodeEncoding.Code128,
CropArea = cropRegion,
Speed = ReadingSpeed.Faster
};
// Read with optimized settings
var result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault();
if (result != null)
{
Console.WriteLine($"Barcode found: {result.Value}");
Console.WriteLine($"Read time: {result.ReadTime}ms");
}
Imports IronBarCode
Imports IronSoftware.Drawing
' Define a crop region where the barcode is likely located
Dim cropRegion As New Rectangle(100, 100, 300, 200)
' Configure options for optimal single barcode reading
Dim optimizedOptions As New BarcodeReaderOptions() With {
.ExpectMultipleBarcodes = False,
.ExpectBarcodeTypes = BarcodeEncoding.Code128,
.CropArea = cropRegion,
.Speed = ReadingSpeed.Faster
}
' Read with optimized settings
Dim result = BarcodeReader.Read("product-label.png", optimizedOptions).FirstOrDefault()
If result IsNot Nothing Then
Console.WriteLine($"Barcode found: {result.Value}")
Console.WriteLine($"Read time: {result.ReadTime}ms")
End If
单个条形码读取速度有多快?
将 ExpectMultipleBarcodes 设置为 false 可以大大提高读取单个条形码的效率。 在处理高分辨率图像或在高吞吐量应用程序中实施 异步条形码读取时,性能提升尤为明显。
使用提供的代码片段,以下是同一台机器上将ExpectMultipleBarcodes设置为 true 和 false 时性能差异的粗略估计:
| ExpectMultipleBarcodes = true | ExpectMultipleBarcodes = false |
|---|---|
| 0.91秒 | 0.10 秒 |
在读取单个 BarCode 时,性能大约提高了 9倍。 实际绩效收益根据以下因素而变化:
- 图像分辨率和复杂程度
- 图像中出现的 BarCode 数量
- 选定的 BarCode 格式
- 应用图像过滤器
- 硬件规格
多条码读取的最佳实践
在生产应用程序中实施多条形码读取时,请考虑这些最佳实践:
1.指定预期条形码类型:不要使用 BarcodeEncoding.All,而只指定您期望的格式。 这将大大提高性能。
2.使用适当的图像格式:为获得最佳效果,请使用高对比度的图片。 了解有关创建最佳条形码图像的更多信息。
3.处理不完善的条形码:现实世界中的条形码可能会损坏或打印不良。 使用图像校正技术提高阅读成功率。
4.流处理:对于大批量数据,请考虑从流中读取,以优化内存使用。
5.错误处理:在无法读取 BarCode 的情况下,始终执行适当的错误处理:
try
{
var results = BarcodeReader.Read("barcodes.png", new BarcodeReaderOptions
{
ExpectMultipleBarcodes = true
});
if (!results.Any())
{
Console.WriteLine("No barcodes found in the image");
}
else
{
Console.WriteLine($"Found {results.Count()} barcodes");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading barcodes: {ex.Message}");
// Log error for debugging
}
try
{
var results = BarcodeReader.Read("barcodes.png", new BarcodeReaderOptions
{
ExpectMultipleBarcodes = true
});
if (!results.Any())
{
Console.WriteLine("No barcodes found in the image");
}
else
{
Console.WriteLine($"Found {results.Count()} barcodes");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error reading barcodes: {ex.Message}");
// Log error for debugging
}
Imports System
Try
Dim results = BarcodeReader.Read("barcodes.png", New BarcodeReaderOptions With {
.ExpectMultipleBarcodes = True
})
If Not results.Any() Then
Console.WriteLine("No barcodes found in the image")
Else
Console.WriteLine($"Found {results.Count()} barcodes")
End If
Catch ex As Exception
Console.WriteLine($"Error reading barcodes: {ex.Message}")
' Log error for debugging
End Try
通过遵循这些实践并利用 IronBarcode 的综合功能,您可以构建强大的应用程序,有效处理不同行业和用例中的多种条码读取场景。
常见问题解答
如何用 C# 从一张图片中读取多个 BarCode?
使用 IronBarcode,您可以通过在 BarcodeReaderOptions 中设置 ExpectMultipleBarcodes = true 从单个图像中读取多个条形码。这将使 IronBarcode 能够扫描整个文档,并在 BarcodeResults 集合中返回所有找到的条形码,您可以遍历这些条形码。
扫描图像中所有 BarCode 的最快方法是什么?
最快的方法是使用 ExpectMultipleBarcodes = true 的 IronBarcode 读取方法:var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true })。此最小代码无需复杂配置即可提取所有条码值。
我可以从 PDF 文档以及图像中读取多个 BarCode 吗?
是的,IronBarcode 支持从图像和 PDF 文档中读取多个条码。ExpectMultipleBarcodes 属性在 BarcodeReaderOptions 和 PdfBarcodeReaderOptions 类中都可用,允许您为任何文档类型配置多条码读取。
如果不将 ExpectMultipleBarcodes 设置为 true 会发生什么情况?
默认情况下,IronBarcode 会连续扫描文档中的多个条形码。但在某些情况下,即使存在多个条码,也可能只返回一个条码值。明确设置 ExpectMultipleBarcodes = true 可确保 IronBarcode 扫描并返回文档中的所有条码。
读取多个条码后,如何访问单个条码值?
使用 IronBarcode 读取多个条码后,结果将存储在 BarcodeResults 变量中。您可以使用foreach循环轻松访问单个条码值,遍历集合并处理每个条码的值、文本和格式属性。
读取多个 BarCode 是否适合零售和物流应用?
是的,IronBarcode 的多重条码读取功能是零售点销售系统、仓库管理、物流跟踪和库存管理应用程序的理想选择。它能同时有效扫描发货标签、产品目录或库存表中的所有条形码,从而简化数据处理。
读取多个条形码时,能否指定要查找的条形码类型?
是的,IronBarcode 允许您使用 ExpectBarcodeTypes 属性指定预期条码类型。您可以将其设置为扫描特定格式,如 AllOneDimensional、QRCode 或任何支持的条码类型组合,以优化扫描性能。
设置 ExpectMultipleBarcodes 会影响扫描性能吗?
当您知道文档中只存在一个条码时,设置 ExpectMultipleBarcodes = false 可以提高性能。IronBarcode 会在找到第一个条码后停止扫描,从而在单条码情况下速度更快,同时在需要时仍能灵活地进行多条码读取。

