IRONSOFTWAREHOME

如何用 C# 一次读取多个 BarCode;

Hairil Hasyimi Bin Omar
Hairil Hasyimi Bin Omar
Updated: 2026年6月4日

通过设置ExpectMultipleBarcodes = true,IronBarcode可以同时从图像和PDF中读取多个条形码,简化物流、零售和库存管理应用的数据处理。 无论是构建仓库系统、零售销售点应用程序,还是文档处理解决方案,IronBarcode 的高级读取功能都能提供您所需的可靠性和性能。

快速入门:轻松从图像中读取所有条形码

本示例展示了如何快速使用 IronBarcode 扫描图像中包含的每一个条形码。 只需将ExpectMultipleBarcodes = true与您想要的条形码类型一起设置——不需要样板代码,简单无忧。

  1. 1Install IronBarcode with NuGet Package Manager

    PM > Install-Package BarCode

  2. 2复制并运行这段代码。

    var results = IronBarCode.BarcodeReader.Read("image.png", new IronBarCode.BarcodeReaderOptions { ExpectMultipleBarcodes = true, ExpectBarcodeTypes = IronBarCode.BarcodeEncoding.AllOneDimensional });
    C#
  3. 3部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronBarcode
    arrow pointer

如何从图像中读取多个 BarCode?

默认情况下,IronBarcode 会持续扫描文档以读取多个条形码。 然而,曾经出现过即使存在多个条形码,也只返回一个条形码值的情况。 为了解决这个问题,请自定义设置,以便能够读取多个 BarCode,如下图所示。 PdfBarcodeReaderOptions类中,您可以使用它读取图像和PDF文档中的条形码

标有 A、B 和 C# 的三个条形码样本,显示了用于多条形码读取演示的不同条形码模式
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());
}

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("---");
}
C#

这个高级示例展示了几个重要功能:

如何读取单个条形码以获得更好的性能?

IronBarcode 可读取图像或 PDF 中的单个和多个条形码。 默认情况下,即使只有一个 BarCode,引擎也会扫描整个文档。 为了提高读取单个条形码时的性能,将ExpectMultipleBarcodes设置为false。 这样,引擎在检测到第一个条形码后就不会再扫描整个文档,从而加快了条形码的检索速度。 下面的代码演示了这种方法。

标有 A、B 和 C 的三个相同的条形码样本,用于条形码读取演示
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());
}

在此示例中,我们使用了与之前相同的含有多个条形码的图像,但将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}");
}
C#

单个条形码读取速度有多快?

设置ExpectMultipleBarcodes = false极大提高了读取单个条形码的效率。 在处理高分辨率图像或在高吞吐量应用程序中实施 异步条形码读取时,性能提升尤为明显。

使用提供的代码片段,这里是相同机器上设置ExpectMultipleBarcodes = true和false时性能差异的粗略估算:

ExpectMultipleBarcodes = trueExpectMultipleBarcodes = 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
}

通过遵循这些实践并利用 IronBarcode 的综合功能,您可以构建强大的应用程序,有效处理不同行业和用例中的多种条码读取场景。

常见问题解答

如何用 C# 从一张图片中读取多个 BarCode?

using 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 扫描并返回文档中的所有条码。

读取多个条码后,如何访问单个条码值?

using IronBarcode 读取多个条码后,结果将存储在 BarcodeResults 变量中。您可以使用foreach循环轻松访问单个条码值,遍历集合并处理每个条码的值、文本和格式属性。

读取多个 BarCode 是否适合零售和物流应用?

是的,IronBarcode 的多重条码读取功能是零售点销售系统、仓库管理、物流跟踪和库存管理应用程序的理想选择。它能同时有效扫描发货标签、产品目录或库存表中的所有条形码,从而简化数据处理。

读取多个条形码时,能否指定要查找的条形码类型?

是的,IronBarcode 允许您使用 ExpectBarcodeTypes 属性指定预期条码类型。您可以将其设置为扫描特定格式,如 AllOneDimensional、QRCode 或任何支持的条码类型组合,以优化扫描性能。

设置 ExpectMultipleBarcodes 会影响扫描性能吗?

当您知道文档中只存在一个条码时,设置 ExpectMultipleBarcodes = false 可以提高性能。IronBarcode 会在找到第一个条码后停止扫描,从而在单条码情况下速度更快,同时在需要时仍能灵活地进行多条码读取。

Is stream processing supported in IronBarcode for large barcode batches?

Yes, IronBarcode supports reading barcodes from streams, which is particularly useful for optimizing memory usage in high-throughput applications or large batch processing.

准备开始了吗?

Nuget Downloads 2,422,100版本:2026.9刚刚发布

免费获取

30天试用密钥 即刻获取。

bullet_checked无需信用卡或创建账户
bullet_test在生产环境中测试
且无水印
bullet_calendar30天完全
功能性产品
bullet_support试用期间提供
24/5技术支持
立即获取您的免费30 天试用密钥
无需信用卡或创建账户
C# 用于 PDF 的 NuGet 库
通过 NuGet 安装

版本: 2026.9

PM > Install-Package BarCode
nuget.org/packages/BarCode/
  1. 在解决方案资源管理器中,右键点击引用,管理 NuGet 包
  2. 选择浏览并搜索 "IronBarcode"
  3. 选择包并安装
C# PDF DLL
下载 DLL

版本: 2026.9

  1. 下载并将 IronBarCode 解压到解决方案目录中的 ~/Libs 等位置
  2. 在 Visual Studio 解决方案资源管理器中,右键单击引用。选择浏览,"IronBarcode.dll"

许可 $999

Key in blue circle

立即获取免费的 30 天试用版密钥

Your trial license will be sent to your email address

无任何限制。100% 解锁。无需信用卡。

bullet_checked无需信用卡或创建账户无任何限制。100% 解锁。无需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
预约您的免费现场演示
Booking Badge

深受全球数百万工程师信赖

Iron Software 的客户徽标
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户