IronOCR 操作指南 条形码/QR(超过20种格式) 如何使用 IronOCR 在 C## 中读取条形码和二维码。 Curtis Chau 已更新:2026年1月10日 下载 IronOCR NuGet 下载 DLL 下载 Windows 安装程序 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在 Grok 中打开 向 Grok 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English IronOCR 可通过在配置中设置 ReadBarCodes = true 在 C# 中读取条形码和二维码。 该单一设置可从 PDF 和图像中自动提取条形码值,同时还能识别常规文本,支持 20 多种条形码格式,包括 QR 码、Code 128 码和数据矩阵。 快速入门:立即从 PDF 读取条形码 只需一个设置即可启用条形码检测,并使用 IronOCR 扫描 PDF。 下面的代码展示了如何打开条形码读取、处理 PDF 和检索解码值。 立即开始使用 NuGet 创建 PDF 文件: 使用 NuGet 包管理器安装 IronOCR PM > Install-Package IronOcr 复制并运行这段代码。 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); 部署到您的生产环境中进行测试 立即开始在您的项目中使用 IronOCR,免费试用! 免费试用30天 ## 如何使用 IronOCR 在 C# 中读取条形码和二维码 下载一个用于读取条形码和二维码的 C# 库。 导入目标图像和PDF文档。 通过将 ReadBarCodes 属性设置为 true 来启用条形码读取。 使用Read方法执行 OCR 操作,操作方式与往常一样。 输出检测到的文本和条形码值。 如何从 PDF 文档中读取 BarCode? 创建一个 IronTesseract 对象来执行阅读。 将 ReadBarCodes 属性设置为 true 以启用条形码检测。 使用 OcrPdfInput 构造函数导入 PDF 文档。 使用 Read 方法对导入的 PDF 执行 OCR。 下面是一个使用该 PDF 文档的示例: :path=/static-assets/ocr/content-code-examples/how-to/barcodes-read-barcodes.cs 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); } Imports IronOcr Imports System ' Instantiate IronTesseract Private ocrTesseract As New IronTesseract() ' Enable barcode reading ocrTesseract.Configuration.ReadBarCodes = True ' Add PDF Dim imageInput = New OcrPdfInput("pdfWithBarcodes.pdf") ' Perform OCR Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput) ' Output detected barcodes and text values Console.WriteLine("Extracted text:") Console.WriteLine(ocrResult.Text) Console.WriteLine("Extracted barcodes:") For Each barcode In ocrResult.Barcodes Console.WriteLine(barcode.Value) Next barcode $vbLabelText $csharpLabel 多个条形码值出现在条形码下方,并包含在提取的文本中。 为什么 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 文档: :path=/static-assets/ocr/content-code-examples/how-to/barcodes-read-qr-codes.cs 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); } Imports IronOcr Imports System ' Instantiate IronTesseract Private ocrTesseract As New IronTesseract() ' Enable barcode reading ocrTesseract.Configuration.ReadBarCodes = True ' Add PDF Dim imageInput = New OcrPdfInput("pdfWithQrCodes.pdf") ' Perform OCR Dim ocrResult As OcrResult = ocrTesseract.Read(imageInput) ' Output detected barcodes and text values Console.WriteLine("Extracted text:") Console.WriteLine(ocrResult.Text) Console.WriteLine("Extracted barcodes:") For Each barcode In ocrResult.Barcodes Console.WriteLine(barcode.Value) Next barcode $vbLabelText $csharpLabel 为什么相同的配置对 BarCode 和 QR 码都有效? IronOCR 的统一条形码检测引擎对所有机器可读代码一视同仁。 The ReadBarCodes configuration activates a comprehensive symbology detector that recognizes both 1D (linear barcodes) and 2D (QR codes, Data Matrix) formats without requiring format-specific settings. 这种设计简化了实施过程,降低了配置的复杂性。 自动检测算法 根据模式识别识别符号类型 应用适当的解码算法 处理方向和尺寸变化 无论条形码类型如何,都能以一致的格式返回结果 这种方法反映了计算机视觉模型的工作方式--对多种格式进行培训,以提供通用的检测能力。 使用 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); // 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); ' Apply filters to improve QR code readability ocrTesseract.Configuration.ReadBarCodes = True Dim input As New OcrImageInput("qr-code-scan.jpg") input.DeNoise() input.Sharpen() input.EnhanceResolution() Dim result = ocrTesseract.Read(input) $vbLabelText $csharpLabel 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); // 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); Imports IronTesseract ' Enhanced QR code reading with preprocessing Dim ocrTesseract = New IronTesseract() ocrTesseract.Configuration.ReadBarCodes = True ' Configure for better QR detection Dim 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 Dim result = ocrTesseract.Read(input) $vbLabelText $csharpLabel 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($" Location: X={barcode.X}, Y={barcode.Y}"); } } // 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($" Location: X={barcode.X}, Y={barcode.Y}"); } } Imports System ' Process multi-page documents efficiently Using pdfInput As New OcrPdfInput("multi-page-qr-document.pdf") pdfInput.TargetDPI = 300 Dim results = ocrTesseract.Read(pdfInput) For Each page In results.Pages Console.WriteLine($"Page {page.PageNumber}:") For Each barcode In page.Barcodes Console.WriteLine($" QR Code: {barcode.Value}") Console.WriteLine($" Location: X={barcode.X}, Y={barcode.Y}") Next Next End Using $vbLabelText $csharpLabel 3.同步处理:使用 async 方法可提高处理多个文档的性能: // Asynchronous QR code reading var result = await ocrTesseract.ReadAsync(imageInput); // Asynchronous QR code reading var result = await ocrTesseract.ReadAsync(imageInput); ' Asynchronous QR code reading Dim result = Await ocrTesseract.ReadAsync(imageInput) $vbLabelText $csharpLabel 4.调试识别问题:启用结果高亮显示以可视化 IronOCR 检测到的内容: result.SaveAsHighlightedImage("qr-detection-debug.png"); result.SaveAsHighlightedImage("qr-detection-debug.png"); result.SaveAsHighlightedImage("qr-detection-debug.png") $vbLabelText $csharpLabel 大规模条形码处理的性能优化 在处理带有 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(); // 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(); ' Process multiple documents in parallel Dim documents = {"doc1.pdf", "doc2.pdf", "doc3.pdf"} Dim results = documents.AsParallel().Select(Function(doc) Dim tesseract = New IronTesseract() tesseract.Configuration.ReadBarCodes = True Return tesseract.Read(New OcrPdfInput(doc)) End Function).ToList() $vbLabelText $csharpLabel 2.内存管理:对于长期运行的操作,请使用 中止令牌: // Implement cancellation for large batch processing using var cts = new CancellationTokenSource(); ocrTesseract.Configuration.CancellationToken = cts.Token; // Cancel if processing takes too long cts.CancelAfter(TimeSpan.FromMinutes(5)); // Implement cancellation for large batch processing using var cts = new CancellationTokenSource(); ocrTesseract.Configuration.CancellationToken = cts.Token; // Cancel if processing takes too long cts.CancelAfter(TimeSpan.FromMinutes(5)); ' Implement cancellation for large batch processing Using cts As New CancellationTokenSource() ocrTesseract.Configuration.CancellationToken = cts.Token ' Cancel if processing takes too long cts.CancelAfter(TimeSpan.FromMinutes(5)) End Using $vbLabelText $csharpLabel 3.结果导出:将结果保存为可搜索的 PDF 文件,以保留文本和条形码数据: // Export results with embedded barcode values result.SaveAsSearchablePdf("output-with-barcodes.pdf"); // Export results with embedded barcode values result.SaveAsSearchablePdf("output-with-barcodes.pdf"); ' Export results with embedded barcode values result.SaveAsSearchablePdf("output-with-barcodes.pdf") $vbLabelText $csharpLabel 与业务应用程序集成 IronOCR 的 BarCode 功能可与现有的 .NET 应用程序无缝集成。 常见的集成场景包括 库存管理:从装运清单中提取产品代码 文档存档:通过嵌入的条形码标识符索引扫描文档 发票处理:将条形码 SKU 与 财务文档中的细列项目相匹配 医疗记录:在处理医疗表格的同时处理患者腕带条形码 对于处理大量条形码和 QR 码的生产应用程序,可考虑实施 进度跟踪,以监控处理状态并根据实际指标优化性能。 常见问题解答 如何在 C# 应用程序中启用条形码读取功能? 使用 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 文本提取的同时,还能在一次操作中解码条形码符号。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 5,384,824 | 版本: 2026.2 刚刚发布 免费 NuGet 下载 总下载量:5,384,824 查看许可证