如何在C#中一次读取多个条形码
IronBarcode通过设置ExpectMultipleBarcodes = true,支持从图像和PDF中同时读取多个条形码,从而简化物流、零售和库存管理应用中的数据处理。 无论是构建仓库系统、零售销售点应用,还是文档处理解决方案,IronBarcode的高级读取功能都能提供您所需的可靠性和性能。
本示例展示了如何快速使用 IronBarcode 扫描图像中包含的每一个条形码。 只需设置ExpectMultipleBarcodes = true和您希望的条形码类型 - 无需样板,无需麻烦。
-
1使用 IronBarcode 安装 NuGet Package Manager
-
2复制并运行这段代码。
var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional });Dim results = IronBarCode.BarcodeReader.Read("image.png", New IronBarCode.BarcodeReaderOptions With {.ExpectMultipleBarcodes = True, .ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional})Dim results = IronBarCode.BarcodeReader.Read("image.png", New IronBarCode.BarcodeReaderOptions With {.ExpectMultipleBarcodes = True, .ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional}) -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronBarcode
最小工作流程(5 个步骤)
- 下载C#库以读取多个条形码
- 使用
Read方法从各种图像格式中提取条形码值 - 利用
ExpectMultipleBarcodes属性配置读取单个或多个条形码 - 将
ExpectMultipleBarcodes属性设置为 false 以提高性能 - 打印出条形码的值
如何从图像中读取多个条形码?
默认情况下,IronBarcode 会持续扫描文档以读取多个条形码。 然而,曾经出现过即使存在多个条形码,也只返回一个条形码值的情况。 为了解决这个问题,请自定义设置,以便能够读取多个 BarCode,如下图所示。 在BarcodeReaderOptions和PdfBarcodeReaderOptions类中都存在ExpectMultipleBarcodes属性,允许您使用它来读取图像和PDF文档中的条形码。

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());
}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 resultImports 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:
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 barcodes from the image
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
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 barcodes from the image
var imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions);
// Process results with error handling
foreach (var result in imageResults)
{
Console.WriteLine($"Barcode Type: {result.BarcodeType}");
Console.WriteLine($"Value: {result.Value}");
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 barcodes from the image
Dim imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions)
' Process results with error handling
For Each result In imageResults
Console.WriteLine($"Barcode Type: {result.BarcodeType}")
Console.WriteLine($"Value: {result.Value}")
Console.WriteLine($"Page: {result.PageNumber}")
Console.WriteLine("---")
NextImports 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 barcodes from the image
Dim imageResults = BarcodeReader.Read("mixed-barcodes.jpg", advancedOptions)
' Process results with error handling
For Each result In imageResults
Console.WriteLine($"Barcode Type: {result.BarcodeType}")
Console.WriteLine($"Value: {result.Value}")
Console.WriteLine($"Page: {result.PageNumber}")
Console.WriteLine("---")
Next这个高级示例展示了几个重要功能:
如何更好地读取单个条形码以提高性能?
IronBarcode 可读取图像或 PDF 中的单个和多个条形码。 默认情况下,即使只有一个 BarCode,引擎也会扫描整个文档。 为了在读取单个条形码时提高性能,将ExpectMultipleBarcodes设置为false。 这样,引擎在检测到第一个条形码后就不会再扫描整个文档,从而加快了条形码的检索速度。 下面的代码演示了这种方法。

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());
}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 resultImports 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在此示例中,我们使用了与之前相同的包含多个条形码的图像,但将ExpectMultipleBarcodes设置为false。 因此,只会返回第一个条形码值,扫描过程在检索到第一个条形码后停止。
通过裁剪区域优化单一条形码读取
为了在读取单个条形码时获得更好的性能,将ExpectMultipleBarcodes = false设置与裁剪区域规范结合使用。 当您知道 BarCode 的大致位置时,这种技术就特别有用:
using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Linq;
// 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}");
}using IronBarCode;
using IronSoftware.Drawing;
using System;
using System.Linq;
// 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}");
}Imports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
' Define a crop region where the barcode is likely located
Dim cropRegion = 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}")
End IfImports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
' Define a crop region where the barcode is likely located
Dim cropRegion = 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}")
End If单个条形码读取的速度有多快?
设置ExpectMultipleBarcodes = false可以大大提高读取单个条形码的效率。 在处理高分辨率图像或在高吞吐量应用中实现异步条形码读取时,性能提升尤其明显。
设置ExpectMultipleBarcodes = false让引擎在检测到第一个条形码后停止,而不是扫描整个文档,从而可以显著减少单个条形码工作负载的读取时间。 实际绩效收益根据以下因素而变化:
- 图像分辨率和复杂程度
- 图像中出现的 BarCode 数量
- 选定的 BarCode 格式
- 应用图像过滤器
- 硬件规格
多条形码读取的最佳实践
在生产应用程序中实施多条形码读取时,请考虑这些最佳实践:
- 指定预期的条形码类型:而不是使用
BarcodeEncoding.All,只指定您期望的格式。 这将大大提高性能。 - 使用适当的图像格式:为获得最佳效果,请使用高对比度图像。 了解更多关于创建最佳条形码图像的信息。
- 处理不完美的条形码:现实世界中的条形码可能会损坏或打印不佳。 使用图像校正技术提高读取成功率。
- 流处理:对于大批量,考虑从流中读取以优化内存使用。
- 错误处理:对无法读取条形码的情况始终实施适当的错误处理:
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
}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 TryTry
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的全面功能,您可以构建可靠的应用程序,有效处理跨各种行业和用例的多条形码读取场景。
常见问题
如何在IronBarcode中启用多条形码扫描?
要在IronBarcode中启用多条形码扫描,请在BarcodeReaderOptions或PdfBarcodeReaderOptions类中将'ExpectMultipleBarcodes'属性设置为true。
IronBarcode中的ExpectMultipleBarcodes属性的目的是什么?
'ExpectMultipleBarcodes'属性允许IronBarcode同时从图像或PDF中读取多个条形码,从而优化物流和零售应用中的数据提取。
IronBarcode可以读取1D和2D条形码吗?
是的,IronBarcode可以读取1D和2D条形码。您可以通过配置BarcodeReaderOptions中的'ExpectBarcodeTypes'属性来指定期待的条形码类型。
如何提高使用IronBarcode读取单个条形码的性能?
要提高读取单个条形码时的性能,请在BarcodeReaderOptions中将'ExpectMultipleBarcodes'设置为false。这将在检测到第一个条形码后停止扫描过程。
有哪些配置可用于读取具有不同格式的多个条形码?
IronBarcode提供了高级选项,例如结合不同的条形码解码类型,应用图像校正滤镜,以及调整读取速度以处理多种条形码格式。
图像校正如何增强IronBarcode中的条形码读取?
应用图像校正滤镜,如锐化滤镜和对比滤镜,可以通过补偿图像质量差或条形码损坏来提高条形码读取准确性。
推荐哪些策略以优化条形码读取性能?
为获得最佳性能,指定预期的条形码类型,使用高对比度图像,实施图像校正技术,从流中读取大批量数据,并包括错误处理。
如何处理使用IronBarcode读取条形码时的错误?
使用try-catch块实现适当的错误处理,以管理无法读取条形码的情况,并在必要时记录错误以进行调试。
IronBarcode是否支持对大批量条形码进行流处理?
是的,IronBarcode支持从流中读取条形码,这对于优化高吞吐量应用程序或大批量处理中的内存使用特别有用。