IRONSOFTWAREHOME

如何使用 IronOCR 在 C## 中读取条形码和二维码

Curtis Chau
Curtis Chau
Updated: 2026年6月4日

IronOCR 通过在配置中设置 ReadBarCodes = true 读取条形码和二维码。 该单一设置可从 PDF 和图像中自动提取条形码值,同时还能识别常规文本,支持 20 多种条形码格式,包括 QR 码、Code 128 码和数据矩阵。

快速开始:立即从PDF读取条形码

只需一个设置即可启用条形码检测,并使用 IronOCR 扫描 PDF。 下面的代码展示了如何打开条形码读取、处理 PDF 和检索解码值。

  1. 1Install IronOCR with NuGet Package Manager

    PM > Install-Package IronOcr

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

    var result = new IronOcr.IronTesseract() { Configuration = new IronOcr.TesseractConfiguration { ReadBarCodes = true } }.Read(new IronOcr.OcrPdfInput("document.pdf"));
    foreach(var bc in result.Barcodes) Console.WriteLine(bc.Value);
    C#
  3. 3部署到您的生产环境中进行测试

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

如何从 PDF 文档中读取 BarCode?

创建一个 IronTesseract 对象来进行读取。 设置 ReadBarCodes 属性为 true 以启用条形码检测。 使用OcrPdfInput 构造函数导入 PDF 文档。 使用 Read 方法对导入的 PDF 执行 OCR。

下面是一个使用该 PDF 文档的示例:

using IronOcr;
using System;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = true;

// Add PDF
using var imageInput = new OcrPdfInput("pdfWithBarcodes.pdf");

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output detected barcodes and text values
Console.WriteLine("Extracted text:");
Console.WriteLine(ocrResult.Text);
Console.WriteLine("Extracted barcodes:");
foreach (var barcode in ocrResult.Barcodes)
{
    Console.WriteLine(barcode.Value);
}
IronOCR 调试输出显示从带有业务简介的 PDF 中提取的文本和三个条形码(A,B,C)

多个条形码值出现在条形码下方,并包含在提取的文本中。

为什么 IronOCR 可同时提取文本和条形码值?

IronOCR 的双重提取功能可提供全面的文档分析。 在处理包含文本和条形码的文档时,该库可执行标准 OCR 文本提取,同时解码条形码符号。 这种统一的方法无需进行多次处理或使用单独的库。

文本提取捕捉人类可读的元素,而条形码检测则识别和解码机器可读的数据。 这将使发票、运输标签或库存报告等文档受益匪浅,因为这些文档中的条形码值与打印文本相关联。 OcrResult将这些输出分开—通过 Text 属性访问文本,通过 Barcodes 集合访问条形码数据。

支持哪些 BarCode 格式?

IronOCR 支持 20 多种条形码格式:

一维条形码:

  • 代码 128、代码 39、代码 93
  • EAN-13、EAN-8
  • UPC-A、UPC-E 科达巴尔
  • ITF(交错 2,共 5 页)
  • MSI 普莱西

二维条形码:

  • 二维码
  • 数据矩阵
  • PDF417
  • 阿兹台克代码
  • MaxiCode

对于读取 MICR 支票或处理身份文件等专业应用,IronOCR 的条形码功能是对其文本提取功能的补充。

什么情况下应使用 OCR 读取条形码而不是专用条形码库?

在以下情况下选择 IronOCR 的集成条形码读取功能:

1.混合内容处理:文档包含文本和 BarCode(运输标签、发票或 扫描文档)。 2.单库偏好:您希望尽量减少依赖性并使用一种解决方案 3.PDF处理:您已经在使用IronOCR进行PDF OCR文本提取。 4.复杂文档布局:文档在文本区域或表格中嵌入了 BarCode

使用专用条形码库时:

  • 处理大容量的纯条形码图像
  • 要求实时扫描 BarCode(响应时间 < 50ms)
  • 处理损坏或低质量的 BarCode,需要专门的算法
  • 利用摄像头优化实施移动条形码扫描

如何从文档中读取二维码?

像读取条形码一样,将 ReadBarCodes 属性设置为 true。 除文件路径外,无需更改其他代码。 使用二维码处理此 PDF 文档:

using IronOcr;
using System;

// Instantiate IronTesseract
IronTesseract ocrTesseract = new IronTesseract();

// Enable barcode reading
ocrTesseract.Configuration.ReadBarCodes = true;

// Add PDF
using var imageInput = new OcrPdfInput("pdfWithQrCodes.pdf");

// Perform OCR
OcrResult ocrResult = ocrTesseract.Read(imageInput);

// Output detected barcodes and text values
Console.WriteLine("Extracted text:");
Console.WriteLine(ocrResult.Text);
Console.WriteLine("Extracted barcodes:");
foreach (var barcode in ocrResult.Barcodes)
{
    Console.WriteLine(barcode.Value);
}
Visual Studio 中的 IronOCR 输出,显示从文档中提取的文本和成功解码的 QR 代码 A、B 和 C

为什么相同的配置对 BarCode 和 QR 码都有效?

IronOCR 的统一条形码检测引擎对所有机器可读代码一视同仁。 ReadBarCodes 配置激活了一个全面的符号检测器,它可以识别 1D(线性条形码)和 2D(二维码,数据矩阵)格式,而无需格式特定设置。 这种设计简化了实施过程,降低了配置的复杂性。

自动检测算法

  • 根据模式识别识别符号类型
  • 应用适当的解码算法
  • 处理方向和尺寸变化
  • 无论条形码类型如何,都能以一致的格式返回结果

这种方法反映了计算机视觉模型的工作方式--对多种格式进行培训,以提供通用的检测能力。

使用 OCR 阅读 QR 码时有哪些常见问题?

处理 QR 代码时常见的挑战包括

1.** 解决问题**:PDF 中的 QR 代码可能被降采样,低于最小模块大小。请使用DPI设置以确保足够的分辨率(建议最低为 300 DPI)。

2.图像质量:扫描的 QR 代码通常会出现模糊、噪点或失真。 应用图像校正过滤器以提高清晰度:

// Apply filters to improve QR code readability
ocrTesseract.Configuration.ReadBarCodes = true;
var input = new OcrImageInput("qr-code-scan.jpg");
input.DeNoise();
input.Sharpen();
input.EnhanceResolution();

var result = ocrTesseract.Read(input);

3.方向问题:有角度的 QR 代码可能无法正确解码。 启用页面旋转检测以处理错位文档。

4.混合内容干扰:文字或图形与 QR 代码重叠会妨碍检测。 必要时使用裁剪区域来隔离二维码区域。

如何提高二维码识别准确率?

使用这些技术优化 QR 代码识别:

1.预处理图像:使用 Filter Wizard 确定最佳增强设置:

// Enhanced QR code reading with preprocessing
var ocrTesseract = new IronTesseract();
ocrTesseract.Configuration.ReadBarCodes = true;

// Configure for better QR detection
var input = new OcrImageInput("document-with-qr.pdf");
input.TargetDPI = 300; // Ensure sufficient resolution
input.Binarize(); // Convert to black and white
input.DeNoise(); // Remove image artifacts

var result = ocrTesseract.Read(input);

2.处理多个页面:适用于多页文档,其中包含跨多页的二维码:

// Process multi-page documents efficiently
using var pdfInput = new OcrPdfInput("multi-page-qr-document.pdf");
pdfInput.TargetDPI = 300;

var results = ocrTesseract.Read(pdfInput);
foreach (var page in results.Pages)
{
    Console.WriteLine($"Page {page.PageNumber}:");
    foreach (var barcode in page.Barcodes)
    {
        Console.WriteLine($"  QR Code: {barcode.Value}");
        Console.WriteLine($"  Format: {barcode.Format}");
    }
}
C#

3.同步处理:使用 async 方法可提高处理多个文档的性能:

// Asynchronous QR code reading
var result = await ocrTesseract.ReadAsync(imageInput);

4.调试识别问题:启用结果高亮显示以可视化 IronOCR 检测到的内容:

input.HighlightTextAndSaveAsImages(ocrTesseract, "qr-detection-debug.png", ResultHighlightType.Word);
C#

大规模条形码处理的性能优化

在处理带有 BarCode 和 QR 码的数千份文件时,实施这些优化策略:

1.多线程:利用多线程处理同时处理多个文档:

// Process multiple documents in parallel
var documents = new[] { "doc1.pdf", "doc2.pdf", "doc3.pdf" };
var results = documents.AsParallel().Select(doc =>
{
    var tesseract = new IronTesseract();
    tesseract.Configuration.ReadBarCodes = true;
    return tesseract.Read(new OcrPdfInput(doc));
}).ToList();

2.内存管理:对于长期运行的操作,请使用 中止令牌

// Start an async read that can be aborted if it runs too long
OcrReadTask ocrReadTask = ocrTesseract.ReadAsync(ocrInput);

// Cancel if processing takes longer than 5 minutes
if (!ocrReadTask.Wait(TimeSpan.FromMinutes(5)))
{
    ocrReadTask.Cancel();
}
C#

3.结果导出:将结果保存为可搜索的 PDF 文件,以保留文本和条形码数据:

// Export results with embedded barcode values
result.SaveAsSearchablePdf("output-with-barcodes.pdf");

与业务应用程序集成

IronOCR 的 BarCode 功能可与现有的 .NET 应用程序无缝集成。 常见的集成场景包括

  • 库存管理:从装运清单中提取产品代码
  • 文档存档:通过嵌入的条形码标识符索引扫描文档
  • 发票处理:将条形码 SKU 与 财务文档中的细列项目相匹配
  • 医疗记录:在处理医疗表格的同时处理患者腕带条形码

对于处理大量条形码和 QR 码的生产应用程序,可考虑实施 进度跟踪,以监控处理状态并根据实际指标优化性能。

常见问题解答

如何在 C# 应用程序中启用条形码读取功能?

using IronOCR,通过在 TesseractConfiguration 中设置 ReadBarCodes = true 来启用条形码读取功能。只需这一项设置,就能激活从 PDF 和图像中自动提取条形码值,同时还能识别常规文本,支持 20 多种条形码格式。

我能从同一文档中同时读取文本和 BarCode 吗?

是的,IronOCR 执行双重提取--它通过标准 OCR 捕捉人类可读文本,同时解码机器可读条形码。OcrResult 类将这些输出分开,文本可通过 Text 属性访问,条形码数据可通过 Barcodes 集合访问。

可以检测哪些条形码格式?

IronOCR 支持 20 多种条形码格式,包括一维条形码(Code 128、Code 39、Code 93、EAN-13、EAN-8、UPC-A、UPC-E、Codabar、ITF、MSI、Plessey)和二维条形码(QR Code、Data Matrix 等)。

如何从 PDF 文档中提取 BarCode?

创建一个 IronTesseract 对象,将 ReadBarCodes 设置为 true,使用 OcrPdfInput 构造函数导入 PDF,然后使用 Read 方法。IronOCR 将执行 OCR 并提取所有检测到的条形码值,您可以通过 result.Barcodes 集合访问这些值。

文本 OCR 和条形码阅读是否需要单独的库?

不,IronOCR 的统一方法消除了多重处理传递或单独库的需要。它在执行标准 OCR 文本提取的同时,还能在一次操作中解码条形码符号。

How can I improve the accuracy of QR code recognition with IronOCR?

Improve QR code recognition by ensuring high resolution, applying image corrections like de-noising and sharpening, and handling orientation issues.

What are common challenges when reading QR codes with OCR?

Common challenges include resolution issues, image quality problems, orientation errors, and interference from overlapping text or graphics.

Can IronOCR handle multi-page PDF documents with barcodes?

Yes, IronOCR can process multi-page PDF documents with barcodes by reading each page individually and outputting detected barcode values and formats.

What is the benefit of using IronOCR for PDF text extraction and barcode reading?

Using IronOCR allows for unified text and barcode extraction from PDF documents, reducing the need for separate libraries and processing steps for each type of data.

How can IronOCR's barcode capabilities integrate into business applications?

IronOCR can be used in various business applications such as inventory management, document archiving, invoice processing, and healthcare records by extracting and processing barcode information.

Curtis Chau
技术作家

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

...
阅读更多

准备开始了吗?

Nuget Downloads 6,236,385版本:2026.9刚刚发布

免费获取

30天试用密钥 即刻获取。

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

版本: 2026.9

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

版本: 2026.9

或在这里下载Windows安装程序。

  1. 下载并解压IronOCR到你的解决方案目录中的~/Libs位置
  2. 在Visual Studio解决方案资源管理器中,右键点击引用。选择浏览,“IronOCR.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
获取您的无义务咨询
填写下面的表格或通过sales@ironsoftware.com
您的资料将始终保密。
深受全球数百万工程师信赖
Iron Software 的客户徽标
立即获取您的免费30 天试用密钥
无需信用卡或创建账户