IronBarcode 教程 在 .NET 中讀取條碼 C# Barcode Scanner: Read Barcodes & QR Codes in .NET Applications Jacob Mellor 更新日期:7月 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 This article was translated from English: Does it need improvement? Translated View the article in English 需要快速在你的 .NET 應用程式中掃描條碼或 QR 碼嗎? IronBarcode 讓條碼讀取變得簡單可靠,無論你是處理完美的數位圖像還是具有挑戰性的現實世界照片。 本指南向你展示如何在C#中實作條碼掃描,並提供立即可以使用的實用示例。 快速入門: 立即從文件中讀取條碼 這個快速示例顯示了你如何輕鬆開始使用 IronBarcode。 只需一行代碼,你就可以從圖像文件中讀取條碼,無需複雜的設定。 Get started making PDFs with NuGet now: Install IronBarcode with NuGet Package Manager PM > Install-Package BarCode Copy and run this code snippet. var results = IronBarCode.BarcodeReader.Read("path/to/barcode.png"); Deploy to test on your live environment Start using IronBarcode in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 最小工作流程(5個步驟) 從 NuGet 或透過 DLL 下載安裝 IronBarcode 使用 BarcodeReader.Read 方法來掃描任何條碼或 QR 碼 在一次掃描、PDF 或多幀 TIFF 文件中讀取多個條碼或 QR 碼 啟用 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 使用高級選項成功讀取的旋轉 QR 碼 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 處理,使用 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 文件 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 文件掃描 支持多執行緒的高性能批量處理 支持所有主要條碼格式 延伸閱讀 擴展你的條碼處理能力,參考以下資源: 條碼生成教程 - 創建自定義條碼 QR 碼指南 - 專用的 QR 碼功能 BarcodeReader 類參考 - 完整 API 文件 故障排除指南 - 常見問題和解決方案 原始代碼下載 自己運行這些示例: 教程 GitHub 儲存庫 C# 源代碼壓縮文件 準備好要在你的應用程式中實現條碼掃描了嗎? 開始免費試用,立即為你的 .NET 專案添加專業的條碼讀取功能。 立即開始使用 IronBarcode。 免費啟動 常見問題解答 如何在 .NET 項目中安裝條碼讀取庫? 你可以使用命令 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'); 每個結果都包含找到條碼的頁碼。 如果條碼圖像模糊或旋轉,應該怎麼辦? 配置 BarcodeReaderOptions 以通過設置 AutoRotate = true 以及應用如 SharpenFilter 或 AdaptiveThresholdFilter 的圖像篩選器來處理具有挑戰性的圖像。使用 Speed = ExtremeDetail 提高準確性。 .NET 應用程序支持哪些條碼格式? IronBarcode 支持所有主要條碼格式,如 QR 代碼、Code 128、Code 39、EAN-13、UPC-A、Data Matrix、PDF417 等。利用 BarcodeEncoding.All 掃描所有支持的格式。 如何在 C# 應用程序中提高條碼掃描性能? 通過指定預期的條碼類型 ExpectBarcodeTypes、啟用多執行緒處理和選擇合適的 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 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。他的旗艦產品 IronPDF & IronSuite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。 準備好開始了嗎? Nuget 下載 1,935,276 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:1,935,276 查看許可證