USING IRONBARCODE C# Barcode Scanner Step-by-Step Tutorial Jordi Bardia 已更新:六月 22, 2025 Download IronBarcode NuGet 下载 DLL 下载 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article 本教程将演示如何在第三方库的帮助下创建条形码扫描器。 为此目的有多种库可用,但有些是付费的,有些提供的功能较少,还有些难以实施。 找到一个免费、高效且易于实施的非常全面且实用的库是一项具有挑战性的任务。 因此,我们将使用IronBarcode,因为它是开发.NET条形码扫描器的最佳选择。 它还具有开发免费、高效且易于实施的额外好处。 IronBarcode允许开发人员在.NET应用程序和网站内读取和写入条形码和QR码。 使用该库读取或写入条形码仅需要一行代码。 .NET条形码库读取和写入大多数条形码和QR码标准。 支持的条形码类型包括code 39/93/128, UPC A/E, EAN 8/13, ITF, RSS 14 / Expanded, Databar, Codabar, Aztec, Data Matrix, MaxiCode, PDF417, MSI, Plessey, USPS和QR。 条形码结果数据包括类型、文本、二进制数据、页面和图像文件。 条形码写入API检查和验证格式、长度、编号和校验和以自动避免编码错误。 条形码编写器允许样式化,调整大小、边距、边框、重新着色和添加文本注释。 条形码编写器可以写入包括BMP, PNG, GIF, TIFF, 和JPG的图像文件。 它还可以写入PDF或HTML文件。 让我们创建我们的条形码扫描器以更好地理解它。 创建 Visual Studio 项目 首先,创建一个用于开发演示应用程序的Visual Studio项目。您也可以打开现有的。 按照以下步骤创建一个新的控制台应用程序项目,专注于核心功能。 相同的代码可以在Web API, MVC, Web表单或Windows表单应用程序中实施。 打开Visual Studio 点击创建新项目 选择模板,点击下一步按钮 命名项目,点击下一步按钮 选择目标框架,点击下一步按钮 点击创建按钮,点击下一步按钮 将创建一个如下所示的新项目: 控制台应用程序 下一步是安装IronBarcode NuGet包以使用其功能。 安装IronBarcode的NuGet包 您可以使用包管理器控制台、NuGet包管理器解决方案或直接从NuGet BarCode包页面安装库。 请按照以下步骤操作: 点击 工具 > NuGet 包管理器 > 包管理器控制台。 包管理器控制台用户界面 编写以下命令 Install-Package BarCode 该包将被安装。 现在让我们编写代码来扫描条形码图像。 从图像文件扫描条形码 添加以下命名空间 using IronBarCode; using IronBarCode; Imports IronBarCode $vbLabelText $csharpLabel 接下来,在主函数中编写以下代码以从图像中读取条形码数据。 // Read the barcode from an image file var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg"); // Print the barcode data to the console Console.WriteLine(myBarcode); // Read the barcode from an image file var myBarcode = BarcodeReader.Read(@"D:\Barcode Images\mybarcode.jpeg"); // Print the barcode data to the console Console.WriteLine(myBarcode); ' Read the barcode from an image file Dim myBarcode = BarcodeReader.Read("D:\Barcode Images\mybarcode.jpeg") ' Print the barcode data to the console Console.WriteLine(myBarcode) $vbLabelText $csharpLabel The BarcodeReader class provides a Read function that takes a file path as an argument. 该函数读取图像并返回条形码数据。 该方法从BMP、PNG、GIF, TIFF或JPG中读取条形码,并为开发者提供了通过精细设置来权衡性能与精确度的控制。 路径中包含以下条形码图像,应用程序将进行扫描。 条形码图像 用于此控制台应用程序的条形码图像 让我们读取此条形码图像并查看该程序是否生成了正确的结果。 输出 运行应用程序时的控制台输出 可以看出,该程序生成了一个准确的输出。 从PDF中扫描条形码 在许多情况下,存在需要从PDF发票中扫描条形码的需求。在本例中,我们将从以下发票中扫描条形码。 PDF文档 PDF格式的发票 请考虑以下代码片段以扫描PDF文档中的条形码: // Read barcodes from a PDF file var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf"); // Iterate through each barcode found and print its value foreach(var barcodeData in myBarcode) { Console.WriteLine(barcodeData.Value); } // Read barcodes from a PDF file var myBarcode = BarcodeReader.ReadPdf(@"D:\Barcode Images\invoice.pdf"); // Iterate through each barcode found and print its value foreach(var barcodeData in myBarcode) { Console.WriteLine(barcodeData.Value); } ' Read barcodes from a PDF file Dim myBarcode = BarcodeReader.ReadPdf("D:\Barcode Images\invoice.pdf") ' Iterate through each barcode found and print its value For Each barcodeData In myBarcode Console.WriteLine(barcodeData.Value) Next barcodeData $vbLabelText $csharpLabel BarcodeReader类提供了ReadPdf函数,接受文件路径作为参数。 该函数在PDF文件中查找条形码图像,扫描整个条形码,并以数组形式返回其数据。 该函数从文档中嵌入的每个图像中读取条形码。 使用foreach循环在控制台中打印条形码的数据。 输出 在控制台上打印发票编号。 控制台输出显示发票号码 从多个文件扫描条形码 本示例将演示如何同时从图像文件中扫描多个条形码。 条形码图片 以下示例中使用的条形码图像 请考虑以下代码片段,这些代码片段读取多个条形码并使用多线程扫描其结果。 // Create a list of file paths containing barcode images List<string> barcodeList = new List<string> { @"D:\Barcode Images\barcode1.jpg", @"D:\Barcode Images\barcode2.jpg", @"D:\Barcode Images\barcode3.jpg" }; // Read barcodes asynchronously from multiple files var batchResults = BarcodeReader.ReadAsync(barcodeList); // Work with the results foreach (var result in batchResults) { string barcodeValue = result.Text; Console.WriteLine(barcodeValue); } // Create a list of file paths containing barcode images List<string> barcodeList = new List<string> { @"D:\Barcode Images\barcode1.jpg", @"D:\Barcode Images\barcode2.jpg", @"D:\Barcode Images\barcode3.jpg" }; // Read barcodes asynchronously from multiple files var batchResults = BarcodeReader.ReadAsync(barcodeList); // Work with the results foreach (var result in batchResults) { string barcodeValue = result.Text; Console.WriteLine(barcodeValue); } ' Create a list of file paths containing barcode images Dim barcodeList As New List(Of String) From {"D:\Barcode Images\barcode1.jpg", "D:\Barcode Images\barcode2.jpg", "D:\Barcode Images\barcode3.jpg"} ' Read barcodes asynchronously from multiple files Dim batchResults = BarcodeReader.ReadAsync(barcodeList) ' Work with the results For Each result In batchResults Dim barcodeValue As String = result.Text Console.WriteLine(barcodeValue) Next result $vbLabelText $csharpLabel 首先,创建一个列表以保存所有条形码图像的文件路径。 接下来,调用ReadAsync函数,该函数以List<string>类型作为参数并返回数据。 该方法从多张图像中并行读取条形码。 多个线程将开始并自动管理以提升批量条形码读取任务的性能。 扫描QR码 QR码的使用正在迅速增长。 因此,本节将展示如何使用C#扫描QR码。 QR码 本演示中使用的QR码 请考虑以下代码示例: // Read the QR code from an image file var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg"); // Iterate through each result and print its text foreach (var result in qrCodeResult) { Console.WriteLine(result.Text); } // Read the QR code from an image file var qrCodeResult = BarcodeReader.Read(@"D:\Barcode Images\QRcode.jpeg"); // Iterate through each result and print its text foreach (var result in qrCodeResult) { Console.WriteLine(result.Text); } ' Read the QR code from an image file Dim qrCodeResult = BarcodeReader.Read("D:\Barcode Images\QRcode.jpeg") ' Iterate through each result and print its text For Each result In qrCodeResult Console.WriteLine(result.Text) Next result $vbLabelText $csharpLabel 使用上述相同的Read函数来读取QR码。 这是IronBarcode库提供的简便性,允许相同的函数和代码用于不同的图像来源。 输出 QR码阅读器的控制台输出 摘要 本教程演示了一种非常简单的方法来从单个图像中扫描条形码,从PDF文档中扫描条形码,以及从多张图像和多个文档中并行扫描条形码。 我们使用相同的函数来实现不同的功能,同时提供性能和可用性。IronBarcode提供了生成条形码和QR码的不同配置。 有很多功能无法在同一篇文章中探讨。 请点击文档页面以进一步探索IronBarcode。 IronBarcode是Iron软件套件的一部分。此套件包含其他非常有用的库,例如用于读取和写入PDF文件的IronPDF、用于操作Excel文件的IronXL、用于从图像中读取文本的IronOCR和用于从不同网站提取数据的IronWebScraper。 您可以以两个单独库的价格购买完整的Iron Suite。 常见问题解答 我怎样才能在 C# 中创建条码扫描器? 要在 C# 中创建条码扫描器,可以使用 IronBarcode。首先设置一个 Visual Studio 项目,安装 IronBarcode 的 NuGet 包,并利用 BarcodeReader 类从图像或 PDF 文件读取条码。 使用 .NET 条码库可以读取哪些类型的条码? IronBarcode 可以读取多种条码类型,包括 Code 39/93/128、UPC A/E、EAN 8/13、ITF、RSS 14 / 扩展、Databar、Codabar、Aztec、Data Matrix、MaxiCode、PDF417、MSI、Plessey、USPS 和 QR 码。 我可以在 C# 中从 PDF 文件中读取条码吗? 是的,IronBarcode 允许您使用 ReadPdf 方法从 PDF 文件中读取条码,该方法扫描整个 PDF 文档中的条码图像并返回其数据。 是否可以在 C# 中从不同的图像文件中扫描多个条码? 是的,您可以使用 IronBarcode 的 ReadAsync 函数从不同的图像文件中同时扫描多个条码,利用异步处理提高性能。 如何安装用于条码功能的 .NET 库? 要安装 IronBarcode,请在 Visual Studio 中使用软件包管理器控制台,使用命令 Install-Package Barcode,该命令会从 NuGet 包源添加库。 QR 码可以使用与条码相同的方法扫描吗? 是的,您可以使用 IronBarcode 的相同 Read 功能扫描 QR 码,从而在各种图像来源中一致地处理条码和 QR 码。 使用 IronBarcode 进行条码扫描的优势是什么? IronBarcode 使用简单、效率高且适应性强。它支持多种条码类型,并提供简单明了的 API 用于读取和写入条码及 QR 码,使其对 .NET 应用程序非常理想。 Iron 软件套件中包含哪些库? Iron 软件套件包括诸如 IronPDF 用于 PDF 操作,IronXL 用于 Excel 文件处理,IronOCR 用于光学字符识别,以及 IronWebScraper 用于 Web 数据提取的库。 Jordi Bardia 立即与工程团队聊天 软件工程师 Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。 相关文章 已发布十月 19, 2025 How to Print Barcodes in Crystal Reports with VB.NET Generate and print barcodes in Crystal Reports using VB.NET. Step-by-step tutorial with IronBarcode SDK for reliable barcode integration. 阅读更多 已发布九月 29, 2025 IronBarcode vs. Open-Source Barcode Readers in .NET Learn how to read barcodes in C# using IronBarcode 阅读更多 已发布九月 29, 2025 How to Scan Barcodes in an ASP.NET Application Learn how to Scan Barcodes in ASP.NET using IronBarcode 阅读更多 How to Make A QR Code For A Link (C# Tutorial).NET QR Code Generator (Code Exampl...
已发布十月 19, 2025 How to Print Barcodes in Crystal Reports with VB.NET Generate and print barcodes in Crystal Reports using VB.NET. Step-by-step tutorial with IronBarcode SDK for reliable barcode integration. 阅读更多
已发布九月 29, 2025 IronBarcode vs. Open-Source Barcode Readers in .NET Learn how to read barcodes in C# using IronBarcode 阅读更多
已发布九月 29, 2025 How to Scan Barcodes in an ASP.NET Application Learn how to Scan Barcodes in ASP.NET using IronBarcode 阅读更多