使用IRONBARCODE C#條碼掃描器逐步教程 Jordi Bardia 更新日期:6月 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 Forms或Windows Forms應用程序中。 打開Visual Studio 點擊創建新項目 選擇模板,點擊下一步按鈕 命名項目,點擊下一步按鈕 選擇目標框架,點擊下一步按鈕 點擊創建按鈕,點擊下一步按鈕 一個新項目將被創建,如下所示: 控制台應用 下一步是安裝IronBarcode NuGet包以使用其功能。 安裝IronBarcode的NuGet包 您可以使用包管理器控制台、NuGet包管理器解決方案或直接從NuGet BarCode包頁面安裝庫。 按照以下步驟進行: 點擊工具 > NuGet包管理器 > 包管理器控制台。 包管理器控制台UI 寫入以下命令 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 Code 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 Software套件的一部分。該套件包含其他非常有用的庫,如IronPDF用於讀寫PDF文件,IronXL用於處理Excel文件,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 / Expanded,Databar,Codabar,Aztec,數據矩陣,MaxiCode,PDF417,MSI,Plessey,USPS 和 QR 碼。 我可以用 C# 從 PDF 文件中讀取條碼嗎? 是的,IronBarcode 允許你使用 ReadPdf 方法從 PDF 文件中讀取條碼,該方法會掃描整個 PDF 文件以查找條碼圖像並返回其數據。 可以從不同的圖像文件中掃描多個條碼嗎? 是的,你可以使用 IronBarcode 的 ReadAsync 函數同時從不同的圖像文件中掃描多個條碼,利用異步處理來提高性能。 如何安裝 .NET 庫來實現條碼功能? 要安裝 IronBarcode,請在 Visual Studio 的套件管理器控制台使用命令 Install-Package Barcode,這將從 NuGet 套件源添加該庫。 可以使用與條碼相同的方法來掃描 QR 碼嗎? 是的,你可以使用 IronBarcode 中相同的 Read 函數掃描 QR 碼,從而在各種圖像源中一致地處理條碼和 QR 碼。 使用 IronBarcode 進行條碼掃描的優勢是什麼? IronBarcode 使用方便、高效且適應性強。它支持多種類型的條碼,並提供簡單的 API 來讀取和寫入條碼和 QR 碼,非常適合 .NET 應用程式。 Iron Software 套件中包含哪些庫? Iron Software 套件中包括 IronPDF 用於 PDF 操作,IronXL 用於 Excel 文件處理,IronOCR 用於光學字符識別,以及 IronWebScraper 用於網頁數據提取。 Jordi Bardia 立即與工程團隊聊天 軟體工程師 Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。 相關文章 發表日期 10月 19, 2025 如何使用VB.NET在Crystal Reports中打印條碼 在VB.NET中使用IronBarcode SDK在Crystal Reports中生成和打印條碼的分步教程,確保可靠的條碼集成。 閱讀更多 發表日期 9月 29, 2025 IronBarcode對比.NET中的開源條碼閱讀器 了解如何使用IronBarcode在C#中讀取條碼 閱讀更多 發表日期 9月 29, 2025 如何在ASP.NET應用程式中掃描條碼 了解如何在ASP.NET中使用IronBarcode掃描條碼 閱讀更多 如何為鏈接創建QR碼(C#教程).NET QR碼產生器(代碼示例...
發表日期 10月 19, 2025 如何使用VB.NET在Crystal Reports中打印條碼 在VB.NET中使用IronBarcode SDK在Crystal Reports中生成和打印條碼的分步教程,確保可靠的條碼集成。 閱讀更多