IronBarcode 教程 在 .NET 中读取条形码 C# 条形码扫描器:在 .NET 应用程序中读取条形码和二维码 Jacob Mellor 已更新:七月 22, 2025 下载 IronBarcode NuGet 下载 DLL 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 This article was translated from English: Does it need improvement? Translated View the article in English 需要在 .NET 应用程序中快速扫描条形码或二维码吗? 无论您是处理完美的数字图像还是具有挑战性的真实世界照片,IronBarcode 都能让条形码读取变得简单可靠。 本指南将通过您可以立即使用的实际示例,向您展示如何在 C# 中实现条形码扫描。 快速入门:立即从文件中读取条形码 这个简单的示例向您展示了IronBarcode入门是多么容易。 只需一行代码,即可从图像文件中读取条形码——无需复杂的设置。 立即开始使用 NuGet 创建 PDF 文件: 使用 NuGet 包管理器安装 IronBarcode PM > Install-Package BarCode 复制并运行这段代码。 var results = IronBarCode.BarcodeReader.Read("path/to/barcode.png"); 部署到您的生产环境中进行测试 立即开始在您的项目中使用 IronBarcode,免费试用! 免费试用30天 最小工作流程(5 个步骤) 从 NuGet 或通过 DLL 下载安装 IronBarcode 使用BarcodeReader.Read方法扫描任何条形码或二维码 在单个扫描件、PDF 或多帧 TIFF 文件中读取多个条形码或二维码 启用 IronBarcode,利用高级滤镜解码不完美的扫描件和照片。 下载教程项目并立即开始扫描 如何在我的.NET项目中安装IronBarcode? IronBarcode 可以通过 NuGet 包管理器轻松安装,也可以直接下载 DLL 文件进行安装。 建议使用 NuGet 安装,因为它能够自动管理依赖项和更新。 今天在您的项目中使用 IronBarcode,免费试用。 第一步: 免费开始 Install-Package BarCode 安装完成后,在 C# 文件中添加using IronBarCode;即可访问条形码扫描功能。 有关不同开发环境下的详细安装说明,请查看我们的安装指南。 如何使用 C# 读取我的第一个条形码? 使用 IronBarcode 读取条形码只需要一行代码。 该库可自动检测条形码格式并提取所有编码数据。 IronBarcode 可以立即读取的标准 Code128 条形码 using IronBarCode; using System; // Read barcodes from the image file - supports PNG, JPG, BMP, GIF, and more BarcodeResults results = BarcodeReader.Read("GetStarted.png"); // Check if any barcodes were detected if (results != null && results.Count > 0) { // Process each barcode found in the image foreach (BarcodeResult result in results) { // Extract the text value from the barcode Console.WriteLine("Barcode detected! Value: " + result.Text); // Additional properties available: // result.BarcodeType - The format (Code128, QR, etc.) // result.BinaryValue - Raw binary data if applicable // result.Confidence - Detection confidence score } } else { Console.WriteLine("No barcodes detected in the image."); } using IronBarCode; using System; // Read barcodes from the image file - supports PNG, JPG, BMP, GIF, and more BarcodeResults results = BarcodeReader.Read("GetStarted.png"); // Check if any barcodes were detected if (results != null && results.Count > 0) { // Process each barcode found in the image foreach (BarcodeResult result in results) { // Extract the text value from the barcode Console.WriteLine("Barcode detected! Value: " + result.Text); // Additional properties available: // result.BarcodeType - The format (Code128, QR, etc.) // result.BinaryValue - Raw binary data if applicable // result.Confidence - Detection confidence score } } else { Console.WriteLine("No barcodes detected in the image."); } IRON VB CONVERTER ERROR developers@ironsoftware.com $vbLabelText $csharpLabel BarcodeReader.Read方法返回一个BarcodeResults集合,其中包含所有检测到的条形码。 每个BarcodeResult都提供对条形码的文本值、格式类型、位置坐标和二进制数据的访问。 这种方法可以与常见的条形码格式无缝配合,包括 Code128、Code39、QR 码和 Data Matrix 码。 哪些方法可以帮助读取难以辨认或损坏的条形码? 现实世界中的条形码扫描经常会遇到图像不完美的情况——角度倾斜、光线不足或部分损坏。 IronBarcode 的高级选项可以有效应对这些挑战。 using IronBarCode; // Configure advanced reading options for difficult barcodes BarcodeReaderOptions options = new BarcodeReaderOptions { // Speed settings: Faster, Balanced, Detailed, ExtremeDetail // ExtremeDetail performs deep analysis for challenging images Speed = ReadingSpeed.ExtremeDetail, // Specify expected formats to improve performance // Use bitwise OR (|) to combine multiple formats ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128, // Maximum number of barcodes to find (0 = unlimited) MaxParallelThreads = 4, // Crop region for faster processing of specific areas CropArea = null // Or specify a Rectangle }; // Apply options when reading BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options); // Process detected barcodes foreach (var barcode in results) { Console.WriteLine($"Format: {barcode.BarcodeType}, Value: {barcode.Text}"); } using IronBarCode; // Configure advanced reading options for difficult barcodes BarcodeReaderOptions options = new BarcodeReaderOptions { // Speed settings: Faster, Balanced, Detailed, ExtremeDetail // ExtremeDetail performs deep analysis for challenging images Speed = ReadingSpeed.ExtremeDetail, // Specify expected formats to improve performance // Use bitwise OR (|) to combine multiple formats ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128, // Maximum number of barcodes to find (0 = unlimited) MaxParallelThreads = 4, // Crop region for faster processing of specific areas CropArea = null // Or specify a Rectangle }; // Apply options when reading BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options); // Process detected barcodes foreach (var barcode in results) { Console.WriteLine($"Format: {barcode.BarcodeType}, Value: {barcode.Text}"); } Imports IronBarCode ' Configure advanced reading options for difficult barcodes Private options As New BarcodeReaderOptions With { .Speed = ReadingSpeed.ExtremeDetail, .ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128, .MaxParallelThreads = 4, .CropArea = Nothing } ' Apply options when reading Private results As BarcodeResults = BarcodeReader.Read("TryHarderQR.png", options) ' Process detected barcodes For Each barcode In results Console.WriteLine($"Format: {barcode.BarcodeType}, Value: {barcode.Text}") Next barcode $vbLabelText $csharpLabel IronBarcode 使用高级选项成功读取了旋转后的二维码 ExpectBarcodeTypes属性通过将搜索限制在特定格式内,显著提高了性能。 为了最大限度地提高处理问题图像的精度,请将图像滤波器与自动旋转功能结合使用: using IronBarCode; BarcodeReaderOptions options = new BarcodeReaderOptions { // Apply image processing filters to enhance readability ImageFilters = new ImageFilterCollection { new AdaptiveThresholdFilter(9, 0.01f), // Handles varying lighting new ContrastFilter(2.0f), // Increases contrast new SharpenFilter() // Reduces blur }, // Automatically rotate to find barcodes at any angle AutoRotate = true, // Use multiple CPU cores for faster processing Multithreaded = true }; BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options); foreach (var result in results) { Console.WriteLine($"Detected {result.BarcodeType}: {result.Text}"); Console.WriteLine($"Confidence: {result.Confidence}%"); Console.WriteLine($"Position: X={result.X}, Y={result.Y}"); } using IronBarCode; BarcodeReaderOptions options = new BarcodeReaderOptions { // Apply image processing filters to enhance readability ImageFilters = new ImageFilterCollection { new AdaptiveThresholdFilter(9, 0.01f), // Handles varying lighting new ContrastFilter(2.0f), // Increases contrast new SharpenFilter() // Reduces blur }, // Automatically rotate to find barcodes at any angle AutoRotate = true, // Use multiple CPU cores for faster processing Multithreaded = true }; BarcodeResults results = BarcodeReader.Read("TryHarderQR.png", options); foreach (var result in results) { Console.WriteLine($"Detected {result.BarcodeType}: {result.Text}"); Console.WriteLine($"Confidence: {result.Confidence}%"); Console.WriteLine($"Position: X={result.X}, Y={result.Y}"); } Imports IronBarCode Private options As New BarcodeReaderOptions With { .ImageFilters = New ImageFilterCollection From { New AdaptiveThresholdFilter(9, 0.01F), New ContrastFilter(2.0F), New SharpenFilter() }, .AutoRotate = True, .Multithreaded = True } Private results As BarcodeResults = BarcodeReader.Read("TryHarderQR.png", options) For Each result In results Console.WriteLine($"Detected {result.BarcodeType}: {result.Text}") Console.WriteLine($"Confidence: {result.Confidence}%") Console.WriteLine($"Position: X={result.X}, Y={result.Y}") Next result $vbLabelText $csharpLabel IronBarcode 的这些高级功能使其成为扫描照片、监控摄像头或移动设备拍摄的图像中条形码的理想选择,尤其适用于图像质量差异很大的情况。 如何扫描PDF文档中的多个条形码? PDF条形码扫描对于处理发票、发货标签和库存文件至关重要。 IronBarcode 可以高效读取每一页上的所有条形码。 从PDF文件中读取条形码 using System; using IronBarCode; try { // Scan all pages of a PDF for barcodes BarcodeResults results = BarcodeReader.ReadPdf("MultipleBarcodes.pdf"); if (results != null && results.Count > 0) { foreach (var barcode in results) { // Access barcode data and metadata string value = barcode.Text; int pageNumber = barcode.PageNumber; BarcodeEncoding format = barcode.BarcodeType; byte[] binaryData = barcode.BinaryValue; // Extract barcode image if needed System.Drawing.Bitmap barcodeImage = barcode.BarcodeImage; Console.WriteLine($"Found {format} on page {pageNumber}: {value}"); } } else { Console.WriteLine("No barcodes found in the PDF."); } } catch (Exception ex) { Console.WriteLine($"Error reading PDF: {ex.Message}"); } using System; using IronBarCode; try { // Scan all pages of a PDF for barcodes BarcodeResults results = BarcodeReader.ReadPdf("MultipleBarcodes.pdf"); if (results != null && results.Count > 0) { foreach (var barcode in results) { // Access barcode data and metadata string value = barcode.Text; int pageNumber = barcode.PageNumber; BarcodeEncoding format = barcode.BarcodeType; byte[] binaryData = barcode.BinaryValue; // Extract barcode image if needed System.Drawing.Bitmap barcodeImage = barcode.BarcodeImage; Console.WriteLine($"Found {format} on page {pageNumber}: {value}"); } } else { Console.WriteLine("No barcodes found in the PDF."); } } catch (Exception ex) { Console.WriteLine($"Error reading PDF: {ex.Message}"); } Imports System Imports IronBarCode Try ' Scan all pages of a PDF for barcodes Dim results As BarcodeResults = BarcodeReader.ReadPdf("MultipleBarcodes.pdf") If results IsNot Nothing AndAlso results.Count > 0 Then For Each barcode In results ' Access barcode data and metadata Dim value As String = barcode.Text Dim pageNumber As Integer = barcode.PageNumber Dim format As BarcodeEncoding = barcode.BarcodeType Dim binaryData() As Byte = barcode.BinaryValue ' Extract barcode image if needed Dim barcodeImage As System.Drawing.Bitmap = barcode.BarcodeImage Console.WriteLine($"Found {format} on page {pageNumber}: {value}") Next barcode Else Console.WriteLine("No barcodes found in the PDF.") End If Catch ex As Exception Console.WriteLine($"Error reading PDF: {ex.Message}") End Try $vbLabelText $csharpLabel !在显示控制台输出的 PDF 页面中检测到多个条形码 控制台输出显示在不同的 PDF 页面中发现了多个条形码 对于特定页面范围或高级 PDF 处理,请使用BarcodeReaderOptions : // Read only specific pages to improve performance BarcodeReaderOptions pdfOptions = new BarcodeReaderOptions { // Scan pages 1-5 only PageNumbers = new[] { 1, 2, 3, 4, 5 }, // PDF-specific settings PdfDpi = 300, // Higher DPI for better accuracy ReadBehindVectorGraphics = true }; BarcodeResults results = BarcodeReader.ReadPdf("document.pdf", pdfOptions); // Read only specific pages to improve performance BarcodeReaderOptions pdfOptions = new BarcodeReaderOptions { // Scan pages 1-5 only PageNumbers = new[] { 1, 2, 3, 4, 5 }, // PDF-specific settings PdfDpi = 300, // Higher DPI for better accuracy ReadBehindVectorGraphics = true }; BarcodeResults results = BarcodeReader.ReadPdf("document.pdf", pdfOptions); ' Read only specific pages to improve performance Dim pdfOptions As New BarcodeReaderOptions With { .PageNumbers = { 1, 2, 3, 4, 5 }, .PdfDpi = 300, .ReadBehindVectorGraphics = True } Dim results As BarcodeResults = BarcodeReader.ReadPdf("document.pdf", pdfOptions) $vbLabelText $csharpLabel 通过我们的详细示例,了解更多关于PDF条形码提取技术的信息。 如何处理多帧TIFF图像? 多帧 TIFF 文件(常见于文档扫描和传真系统)与 PDF 文件一样,都得到了全面的支持。 包含跨帧多个条形码的多帧 TIFF 文件 一个多帧 TIFF 文件,不同帧上带有条形码。 using IronBarCode; // TIFF files are processed similarly to regular images // Each frame is scanned automatically BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff"); foreach (var result in multiFrameResults) { // Access frame-specific information int frameNumber = result.PageNumber; // Frame number in TIFF string barcodeValue = result.Text; Console.WriteLine($"Frame {frameNumber}: {barcodeValue}"); // Save individual barcode images if needed result.BarcodeImage?.Save($"barcode_frame_{frameNumber}.png"); } using IronBarCode; // TIFF files are processed similarly to regular images // Each frame is scanned automatically BarcodeResults multiFrameResults = BarcodeReader.Read("Multiframe.tiff"); foreach (var result in multiFrameResults) { // Access frame-specific information int frameNumber = result.PageNumber; // Frame number in TIFF string barcodeValue = result.Text; Console.WriteLine($"Frame {frameNumber}: {barcodeValue}"); // Save individual barcode images if needed result.BarcodeImage?.Save($"barcode_frame_{frameNumber}.png"); } Imports IronBarCode ' TIFF files are processed similarly to regular images ' Each frame is scanned automatically Private multiFrameResults As BarcodeResults = BarcodeReader.Read("Multiframe.tiff") For Each result In multiFrameResults ' Access frame-specific information Dim frameNumber As Integer = result.PageNumber ' Frame number in TIFF Dim barcodeValue As String = result.Text Console.WriteLine($"Frame {frameNumber}: {barcodeValue}") ' Save individual barcode images if needed If result.BarcodeImage IsNot Nothing Then result.BarcodeImage.Save($"barcode_frame_{frameNumber}.png") End If Next result $vbLabelText $csharpLabel BarcodeReaderOptions也适用于 TIFF 处理,包括图像滤镜和旋转设置。 有关 TIFF 处理场景的详细说明,请参阅我们的图像处理教程。 我能否利用多线程来加快处理速度? 并行处理能极大地提高多文档处理效率。 IronBarcode 会自动利用可用的 CPU 核心,以实现最佳性能。 using IronBarCode; // List of documents to process - mix of formats supported var documentBatch = new[] { "invoice1.pdf", "shipping_label.png", "inventory_sheet.tiff", "product_catalog.pdf" }; // Configure for batch processing BarcodeReaderOptions batchOptions = new BarcodeReaderOptions { // Enable parallel processing across documents Multithreaded = true, // Limit threads if needed (0 = use all cores) MaxParallelThreads = Environment.ProcessorCount, // Apply consistent settings to all documents Speed = ReadingSpeed.Balanced, ExpectBarcodeTypes = BarcodeEncoding.All }; // Process all documents in parallel BarcodeResults batchResults = BarcodeReader.Read(documentBatch, batchOptions); // Group results by source document var resultsByDocument = batchResults.GroupBy(r => r.Filename); foreach (var docGroup in resultsByDocument) { Console.WriteLine($"\nDocument: {docGroup.Key}"); foreach (var barcode in docGroup) { Console.WriteLine($" - {barcode.BarcodeType}: {barcode.Text}"); } } using IronBarCode; // List of documents to process - mix of formats supported var documentBatch = new[] { "invoice1.pdf", "shipping_label.png", "inventory_sheet.tiff", "product_catalog.pdf" }; // Configure for batch processing BarcodeReaderOptions batchOptions = new BarcodeReaderOptions { // Enable parallel processing across documents Multithreaded = true, // Limit threads if needed (0 = use all cores) MaxParallelThreads = Environment.ProcessorCount, // Apply consistent settings to all documents Speed = ReadingSpeed.Balanced, ExpectBarcodeTypes = BarcodeEncoding.All }; // Process all documents in parallel BarcodeResults batchResults = BarcodeReader.Read(documentBatch, batchOptions); // Group results by source document var resultsByDocument = batchResults.GroupBy(r => r.Filename); foreach (var docGroup in resultsByDocument) { Console.WriteLine($"\nDocument: {docGroup.Key}"); foreach (var barcode in docGroup) { Console.WriteLine($" - {barcode.BarcodeType}: {barcode.Text}"); } } Imports Microsoft.VisualBasic Imports IronBarCode ' List of documents to process - mix of formats supported Private documentBatch = { "invoice1.pdf", "shipping_label.png", "inventory_sheet.tiff", "product_catalog.pdf" } ' Configure for batch processing Private batchOptions As New BarcodeReaderOptions With { .Multithreaded = True, .MaxParallelThreads = Environment.ProcessorCount, .Speed = ReadingSpeed.Balanced, .ExpectBarcodeTypes = BarcodeEncoding.All } ' Process all documents in parallel Private batchResults As BarcodeResults = BarcodeReader.Read(documentBatch, batchOptions) ' Group results by source document Private resultsByDocument = batchResults.GroupBy(Function(r) r.Filename) For Each docGroup In resultsByDocument Console.WriteLine($vbLf & "Document: {docGroup.Key}") For Each barcode In docGroup Console.WriteLine($" - {barcode.BarcodeType}: {barcode.Text}") Next barcode Next docGroup $vbLabelText $csharpLabel 这种并行方法可以同时处理文档,在多核系统上可将总扫描时间缩短高达 75%。 对于企业级条码处理,请查阅我们的性能优化指南。 摘要 IronBarcode 将复杂的条形码扫描转换为简单的 C# 代码。 无论您是构建库存系统、文档处理器还是移动应用程序,该库都能处理从完美的数字条形码到具有挑战性的现实世界采集的一切。 主要功能涵盖: 从图像中读取单行条形码 针对损坏或旋转条形码的高级选项 全面扫描 PDF 和 TIFF 文档 高性能多线程批处理 支持所有主流条形码格式 进一步阅读 利用以下资源扩展您的条形码处理能力: 条形码生成教程- 创建自定义条形码 -二维码指南- 二维码的特色功能 BarcodeReader器类参考- 完整的 API 文档 故障排除指南- 常见问题及解决方案 源代码下载 请自行运行以下示例: -教程 GitHub 仓库 C# 源代码(压缩文件) 准备好在您的应用程序中实现条形码扫描了吗? 立即开始免费试用,并将专业的条形码读取功能添加到您的.NET项目中。 立即开始使用 IronBarcode。 免费开始 常见问题解答 .NET 项目中如何安装条形码读取库? 您可以通过 NuGet 包管理器使用命令 dotnet add package BarCode 或通过 Visual Studio 的 NuGet 接口安装 IronBarcode 库。或者,下载 DLL 手动安装。 使用 C# 从图像读取条形码的方法是什么? 使用 IronBarcode 的 BarcodeReader.Read 方法只需一行代码:var results = BarcodeReader.Read('image.png'); 该方法检测并读取图像中存在的所有条形码格式。 是否可以在单个图像或文档中检测多个条形码? 是的,IronBarcode 可以自动检测并读取图像、PDF 或多帧 TIFF 中的多个条形码,返回每个条形码的值、类型和在 BarcodeResults 集合中的位置。 如何使用 C# 从 PDF 读取条形码? 使用 IronBarcode 的 BarcodeReader.ReadPdf 方法扫描 PDF 文档的所有页面:var results = BarcodeReader.ReadPdf('document.pdf'); 每个结果包括找到条形码的页码。 如果条形码图像模糊或旋转,我该怎么办? 通过设置 AutoRotate = true 配置 BarcodeReaderOptions 来处理具有挑战性的图像,并应用 SharpenFilter 或 AdaptiveThresholdFilter 等图像过滤器。使用 Speed = ExtremeDetail 以提高精度。 .NET 应用程序支持哪些条形码格式? IronBarcode 支持所有主要的条形码格式,例如 QR 码、Code 128、Code 39、EAN-13、UPC-A、Data Matrix、PDF417 等。利用 BarcodeEncoding.All 扫描任何支持的格式。 如何提高 C# 应用程序中的条形码扫描性能? 通过指定预期的条形码类型、启用多线程处理和选择合适的 Speed 设置来提高性能。对于批处理任务,利用 BarcodeReader.Read 使用文件路径。 处理条形码读取错误的推荐方法是什么? 将条形码读取封装在 try-catch 块中,并验证结果是否为空或为空。IronBarcode 提供详细的错误信息和一个 Confidence 属性以指示检测可靠性。 在扫描后我可以提取条形码图像吗? 是的,IronBarcode 的 BarcodeResult 包括一个 BarcodeImage 属性,其中包含检测到的条形码的位图,可以单独保存或处理。 如何从 PDF 文档的特定页面读取条形码? 在 BarcodeReaderOptions 中设置 PageNumbers 属性指定页面:options.PageNumbers = new[] {1, 2, 3}; 这通过仅扫描指定页面来优化性能。 在 .NET 中兼容哪些图像格式用于条形码扫描? IronBarcode 支持扫描 PNG、JPEG、BMP、GIF、TIFF(包括多帧)和 PDF 格式。您可以从文件路径、流或字节数组加载图像。 如何在 C# 中访问扫描条形码的二进制数据? 利用 BarcodeResult 的 BinaryValue 属性获取原始二进制数据,对于包含非文本数据如压缩信息或二进制协议的条形码特别有用。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,是 C# PDF 技术的先锋工程师。作为 Iron Software 核心代码库的原始开发者,自公司成立以来,他就塑造了公司的产品架构,并与首席执行官 Cameron Rimington 一起将其转变成一家公司,拥有50多人,服务于 NASA、特斯拉和全球政府机构。Jacob 拥有曼彻斯特大学 (1998-2001) 的一级荣誉土木工程学士学位。1999 年在伦敦创办了自己的第一家软件公司,并于 2005 年创建了他的第一个 .NET 组件后,他专注于解决微软生态系统中的复杂问题。他的旗舰 IronPDF 和 Iron Suite .NET 库在全球已获得超过 3000 万次的 NuGet 安装,其基础代码继续为全球使用的开发者工具提供支持。拥有 25 年商业经验和 41 年编程经验的 Jacob 仍专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 准备开始了吗? Nuget 下载 1,979,979 | Version: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,979,979 查看许可证