IronBarcode 操作指南 .NET 從影像讀取條碼 如何使用 C# 從影像讀取 BarCode Hairil Hasyimi Bin Omar 更新:2026年1月10日 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 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 IronBarcode 在 C# 中使用 BarcodeReader.Read() 只需一行代碼便可從圖像中讀取條碼,支援多種圖像格式,包括 PNG、JPEG、GIF、BMP、TIFF 和 SVG,並提供可自訂的選項以提高效能和精確度。 IronBarcode 的一個關鍵特性是它能夠開箱即用地讀取多種影像格式的條碼。 IronBarcode目前支援以下影像格式: 可縮放向量圖形 (SVG) 聯合影像專家小組 (JPEG) 可攜式網路圖形 (PNG) 圖形交換格式 (GIF) 標籤影像檔案格式 (TIFF) 點陣圖影像檔(BMP) This is made possible with the help of our open source library, IronDrawing. 如需 支援的條碼格式(包括一維和二維類型)的完整清單,請查看我們全面的說明文件。 快速入門:幾秒鐘內從圖像中讀取條碼 只需簡單呼叫 IronBarCode.BarcodeReader.Read() 即可直接從 PNG、JPEG、GIF、BMP 和 TIFF 等影像檔案格式中擷取條碼資料。 立即開始使用-無需複雜設置,即可立即見效。 立即開始使用 NuGet 建立 PDF 檔案: 使用 NuGet 套件管理器安裝 IronBarcode PM > Install-Package BarCode 複製並運行這段程式碼。 var results = IronBarCode.BarcodeReader.Read("path/to/image.png"); 部署到您的生產環境進行測試 立即開始在您的專案中使用 IronBarcode,免費試用! 免費試用30天 ### 最小工作流程(5 個步驟) 下載用於從映像中讀取條碼的 C# 庫 使用Read方法從各種影像格式讀取條碼值 使用BarcodeReaderOptions類別配置讀取設定 使用CropArea屬性指定影像中的條碼區域 透過設定ExpectBarcodeTypes屬性來指定要讀取的條碼類型。 如何直接從影像讀取 BarCode? 以下是如何使用 IronBarcode 進行條碼讀取。 有關 在 C# / .NET 中讀取 BarCode 的全面教學,包括 PDF 處理和批次作業的進階技術,請造訪我們的詳細指南: :path=/static-assets/barcode/content-code-examples/how-to/read-barcodes-from-images-1.cs using IronBarCode; using System; var myBarcode = BarcodeReader.Read(@"image_file_path.jpg"); //image file path foreach (var item in myBarcode) { Console.WriteLine(item.ToString()); } Imports IronBarCode Imports System Private myBarcode = BarcodeReader.Read("image_file_path.jpg") 'image file path For Each item In myBarcode Console.WriteLine(item.ToString()) Next item $vbLabelText $csharpLabel 樣品測試二維碼 樣品測試條碼 想知道樣品中的條碼值是什麼嗎?試試這段程式碼片段吧! 要使用 IronBarcode,首先需要做的是透過 Microsoft Visual Studio NuGet 套件管理器將 IronBarcode 庫安裝到您的專案中,如下圖所示。 這將允許您存取 IronBarcode 的 BarcodeReader.Read() 方法來直接讀取條碼影像。 IronBarcode 提供了簡單易用的功能,使用者只需使用BarcodeReader.Read()即可讀取專案中已包含的映像文件,只需將文件名稱字串或文件路徑字串作為該方法的參數即可。 最佳實踐是在方法中指定檔案路徑時使用字串字面量"@",以避免在檔案路徑字串中新增多個轉義字元"\"。 在BarcodeReader.Read()方法呼叫的末端附加Values()方法,以將條碼值作為System.String[]物件取得。 若要將結果輸出到控制台,可以使用foreach循環遍歷儲存在string[]陣列中的值,並在迴圈區塊內呼叫Console.WriteLine()方法,並將迭代器變數作為參數。 IronBarcode 能夠讀取一維條碼格式 (Codabar, Code128, Code39, Code93, EAN13, EAN18、ITF, MSI, UPCA, UPCE) 以及二維條碼格式 (Aztec, DataMatrix, QRCode) 的各種圖像格式。 如何配置條碼閱讀器選項以獲得更好的性能? 條碼讀取速度太慢? 圖片中的條碼是否太小,導致 IronBarcode 無法讀取? 只想讀取影像的特定區域? 想從包含多種條碼的圖像中只讀取特定類型的條碼嗎? 想提高整體閱讀能力嗎? BarcodeReaderOptions 允許使用者調整或調整條碼讀取器的行為,以解決所有這些問題。 有關 設定 BarCode 閱讀器選項的詳細範例,請查看我們的綜合指南。以下章節將逐一討論 BarcodeReaderOptions 中所有可用的可調整屬性。 如何指定要讀取圖片的哪個區域? CropArea是BarcodeReaderOptions中IronSoftware.Drawing.CropRectangle類型的屬性,允許使用者指定 IronBarcode 應該讀取的影像區域。 這有助於提高讀取效能,因為條碼讀取器無需掃描整個影像來尋找條碼,同時由於讀取區域已指定,讀取精度也得以提高。 進一步瞭解如何指定作物區域以獲得最佳效能。 要設定CropArea屬性,只需實例化新的Rectangle類型對象,並將矩形的座標、寬度和長度指定為參數即可。 接受的測量單位為像素 (px)。 // Example of setting CropArea var cropArea = new IronSoftware.Drawing.Rectangle(x: 100, y: 100, width: 300, height: 300); var options = new BarcodeReaderOptions() { CropArea = cropArea }; // Example of setting CropArea var cropArea = new IronSoftware.Drawing.Rectangle(x: 100, y: 100, width: 300, height: 300); var options = new BarcodeReaderOptions() { CropArea = cropArea }; Imports IronSoftware.Drawing ' Example of setting CropArea Dim cropArea As New Rectangle(x:=100, y:=100, width:=300, height:=300) Dim options As New BarcodeReaderOptions() With { .CropArea = cropArea } $vbLabelText $csharpLabel 為了加快讀取速度,我應該指定哪些 BarCode 類型? 預設情況下,IronBarcode 中所有支援的條碼都會以影像形式掃描。 不過,如果使用者知道有哪些類型的條碼可用,或是想要在影像中讀取,設定此屬性為只讀取特定類型的條碼,將可大幅提升讀取效能與精確度,因為條碼讀取器不需要遍歷條碼集合來解釋和讀取條碼。 要使用此屬性,只需將ExpectBarcodeTypes設定為BarcodeEncoding枚舉的某個欄位即可。 以下是IronBarcode支援的每種條碼類型的範例。 // Example: Expect only QR codes and Code128 var options = new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128 }; // Example: Expect only QR codes and Code128 var options = new BarcodeReaderOptions() { ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128 }; ' Example: Expect only QR codes and Code128 Dim options As New BarcodeReaderOptions() With { .ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128 } $vbLabelText $csharpLabel 以下是條碼類型列表,以及前面提供的範例和解釋。 何時應該停用多重條碼讀取? IronBarcode 預設會掃描影像中所有可用的條碼,包括掃描整個影像檔案並將讀取的條碼值新增至字串陣列中。 但是,如果使用者不希望讀取影像檔案中的多個條碼,使用者可以將此屬性設為false ,這樣條碼讀取器在找到條碼值後就會停止掃描。 這將再次提高 IronBarcode 的效能和讀取速度。 有關 讀取多個 BarCode 的詳細資訊,請參閱我們的專用指南。 影像篩選器如何改善條碼辨識? BarcodeReaderOptions可以新增的屬性之一是影像過濾器集合。 影像濾波器對於預處理輸入到 IronBarcode 的原始影像非常重要。 若要套用BarcodeReaderOptions中的影像濾鏡,使用者必須先初始化並指定要使用的ImageFilter集合。 如需有關 影像修正技術 (包括濾鏡應用程式) 的全面指導,請造訪我們的教學。 如何優化線程以獲得更好的效能? IronBarcode 讓使用者啟用和調整並行執行緒執行的數量,從而提高處理速度和效率。 並行執行緒是指在不同的處理器核心上同時執行多個執行緒。 IronBarcode 中MaxParallelThread屬性的預設值為 4。使用者可以根據機器的性能和資源量進行調整。 我應該啟用多執行緒處理嗎? 此特性使 IronBarcode 能夠並行讀取多個影像。 Multithreaded的預設值為true ,因此會自動管理多個線程,以提高批次條碼讀取任務的效能。 為什麼要移除假陽性讀數? 此屬性可消除所有誤報的條碼讀取結果。 假陽性條碼讀取是指條碼值的錯誤讀取,但卻被辨識為有效。 這可能是由於排序過程中的錯誤,或是條碼標籤或準備過程中的錯誤所造成的。 因此,將RemoveFalsePositive設為 true 將消除誤報的條碼讀數,從而提高條碼讀取準確率。 但是,如果使用者為了追求效能而犧牲準確性,則將此屬性設為false會有所幫助。此屬性的預設值為true 。 哪些掃描模式適用於不同的使用個案? 定義 IronBarcode 如何掃描和偵測影像中的條碼。 -自動:讀取條碼時,會自動進行影像預處理,並配置最佳的讀取器選項。 為獲得最佳效果和性能,建議採用此方法。 OnlyDetectionModel :掃描影像中的條碼,並傳回其位置的IronSoftware.Drawing.PointF陣列。 此模式無法讀取偵測到的條碼; 它只會傳回每個條碼的位置。 MachineLearningScan :使用機器學習偵測掃描影像中的條碼並讀取它們。 OnlyBasicScan :讀取條碼,無需機器學習偵測、自動影像預處理或讀取器選項配置。 This option can be used with IronBarCode.Slim alone. 閱讀速度設定如何影響精確度? 顧名思義, "速度"屬性使用戶能夠進一步優化 IronBarcode 閱讀器的效能。 與RemoveFalsePositive屬性類似,調整此屬性會降低準確性,但會提高效能。 如需深入瞭解 讀取速度選項,包括效能基準,請參閱我們的詳細指南。它接受ReadingSpeed枚數,共有 4 個等級,如下所示: -速度更快:可實現最快的條碼讀取速度,但會降低準確性。 該過程跳過了影像預處理,經常導致條碼結果為空。 僅當輸入影像清晰銳利時才使用此設定。 -平衡:建議將Speed屬性設定為此模式。 它透過嘗試對影像進行輕微處理,使條碼區域更加清晰,從而在準確性和讀取性能之間取得平衡,使條碼讀取器能夠檢測到它。 大多數情況下,這種設定足以讓 IronBarcode 讀取條碼影像並產生準確的輸出。 -詳細:如果使用ReadingSpeed.Balanced設定無法從讀取中產生條碼值,使用者可以選擇使用ReadingSpeed.Detailed 。 IronBarcode 將對影像進行中等處理,以進一步清楚顯示條碼區域,以便條碼閱讀器能夠偵測到條碼。 此設定對於檢測較小或不太清晰的條碼影像非常有用。 ExtremeDetail :由於此設定會佔用大量 CPU 資源,因此最不建議使用。 將對條碼影像進行大量處理,以便閱讀器能夠讀取條碼。 建議使用者在選擇此設定之前,先對影像進行預處理/應用濾鏡。 何時應該使用 Code39 延伸模式? 此設定允許以擴展模式讀取和解釋 Code39 類型的條碼,其中將套用完整的 ASCII 字元集。 將UseCode39ExtendedMode設為true將能夠更準確地讀取 Code39 條碼。 如何使用自訂選項實現進階條碼讀取功能? 現在我們已經瞭解了所有使用者可以調整的選項,不論是為了增加效能或精確度,以下是如何在程式碼中應用這些選項。 下面的程式碼片段展示了 BarcodeReaderOptions 的全面用法: using IronBarCode; using IronSoftware.Drawing; using System; using System.Linq; // Create custom reader options var options = new BarcodeReaderOptions() { // Specify expected barcode types for better performance ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128 | BarcodeEncoding.Code39, // Define specific area to scan (x, y, width, height in pixels) CropArea = new Rectangle(100, 100, 500, 400), // Set reading speed to balance accuracy and performance Speed = ReadingSpeed.Balanced, // Enable multithreading for better performance Multithreaded = true, MaxParallelThreads = 4, // Remove false positives for accuracy RemoveFalsePositive = true, // Enable Code39 extended mode if needed UseCode39ExtendedMode = true, // Set scan mode ScanMode = BarcodeReaderScanMode.Auto, // Add image filters for better recognition ImageFilters = new ImageFilterCollection() { new SharpenFilter(), new InvertFilter(), new ContrastFilter() } }; // Read barcodes with custom options var results = BarcodeReader.Read(@"C:\path\to\your\barcode-image.png", options); // Process results if (results.Any()) { foreach (var barcode in results) { Console.WriteLine($"Barcode Type: {barcode.BarcodeType}"); Console.WriteLine($"Value: {barcode.Value}"); Console.WriteLine($"Confidence: {barcode.Confidence}%"); Console.WriteLine($"Position: X={barcode.X}, Y={barcode.Y}"); Console.WriteLine("---"); } } else { Console.WriteLine("No barcodes found in the image."); } using IronBarCode; using IronSoftware.Drawing; using System; using System.Linq; // Create custom reader options var options = new BarcodeReaderOptions() { // Specify expected barcode types for better performance ExpectBarcodeTypes = BarcodeEncoding.QRCode | BarcodeEncoding.Code128 | BarcodeEncoding.Code39, // Define specific area to scan (x, y, width, height in pixels) CropArea = new Rectangle(100, 100, 500, 400), // Set reading speed to balance accuracy and performance Speed = ReadingSpeed.Balanced, // Enable multithreading for better performance Multithreaded = true, MaxParallelThreads = 4, // Remove false positives for accuracy RemoveFalsePositive = true, // Enable Code39 extended mode if needed UseCode39ExtendedMode = true, // Set scan mode ScanMode = BarcodeReaderScanMode.Auto, // Add image filters for better recognition ImageFilters = new ImageFilterCollection() { new SharpenFilter(), new InvertFilter(), new ContrastFilter() } }; // Read barcodes with custom options var results = BarcodeReader.Read(@"C:\path\to\your\barcode-image.png", options); // Process results if (results.Any()) { foreach (var barcode in results) { Console.WriteLine($"Barcode Type: {barcode.BarcodeType}"); Console.WriteLine($"Value: {barcode.Value}"); Console.WriteLine($"Confidence: {barcode.Confidence}%"); Console.WriteLine($"Position: X={barcode.X}, Y={barcode.Y}"); Console.WriteLine("---"); } } else { Console.WriteLine("No barcodes found in the image."); } Imports IronBarCode Imports IronSoftware.Drawing Imports System Imports System.Linq ' Create custom reader options Dim options As New BarcodeReaderOptions() With { ' Specify expected barcode types for better performance .ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128 Or BarcodeEncoding.Code39, ' Define specific area to scan (x, y, width, height in pixels) .CropArea = New Rectangle(100, 100, 500, 400), ' Set reading speed to balance accuracy and performance .Speed = ReadingSpeed.Balanced, ' Enable multithreading for better performance .Multithreaded = True, .MaxParallelThreads = 4, ' Remove false positives for accuracy .RemoveFalsePositive = True, ' Enable Code39 extended mode if needed .UseCode39ExtendedMode = True, ' Set scan mode .ScanMode = BarcodeReaderScanMode.Auto, ' Add image filters for better recognition .ImageFilters = New ImageFilterCollection() From { New SharpenFilter(), New InvertFilter(), New ContrastFilter() } } ' Read barcodes with custom options Dim results = BarcodeReader.Read("C:\path\to\your\barcode-image.png", options) ' Process results If results.Any() Then For Each barcode In results Console.WriteLine($"Barcode Type: {barcode.BarcodeType}") Console.WriteLine($"Value: {barcode.Value}") Console.WriteLine($"Confidence: {barcode.Confidence}%") Console.WriteLine($"Position: X={barcode.X}, Y={barcode.Y}") Console.WriteLine("---") Next Else Console.WriteLine("No barcodes found in the image.") End If $vbLabelText $csharpLabel 從程式碼片段中我們可以看出,要使用BarcodeReaderOptions我們必須先對其進行初始化,然後根據上面所述的屬性確定並調整BarcodeReaderOptions的屬性。 初始化後的BarcodeReaderOptions可以與圖像檔案一起作為參數傳遞給BarcodeReader.Read()方法。這樣,從影像中讀取條碼時, BarcodeReaderOptions中的所有設定都會被套用。 如果您遇到條碼無法識別的問題,我們的疑難排解指南提供了常見問題的解決方案和提高條碼掃描準確性的技巧。 常見問題解答 C# 中的 BarCode 讀取支援哪些影像格式? IronBarcode 支援從多種影像格式讀取條碼,包括 SVG (可縮放向量圖形)、JPEG、PNG、GIF、TIFF (標籤影像檔案格式),以及 BMP (位圖影像檔案)。此全面的格式支援由 IronDrawing 提供,可從各種影像類型中無縫擷取條碼。 如何只用一行代碼從影像檔案讀取 BarCode? 使用 IronBarcode,您只需一行代碼即可從影像中讀取條碼:`var results = IronBarcode.BarcodeReader.Read("path/to/image.png");`.這個簡單的方法調用可自動檢測並從 PNG、JPEG、GIF、BMP 和 TIFF 檔案中提取條碼資料,而無需任何複雜的設定。 我可以指定在讀取影像時要尋找哪些類型的 BarCode 嗎? 是的,IronBarcode 允許您使用 BarcodeReaderOptions 中的 ExpectBarcodeTypes 屬性指定條碼類型。這種有針對性的方法只針對您期望在圖像中找到的條碼格式,從而提高了讀取性能。 是否可以從影像中的特定區域讀取 BarCode? 絕對可以!IronBarcode 提供了 CropArea 屬性,允許您在圖像中指定條碼所在的特定區域。此功能對於大型圖像或當您知道條碼的大概位置時特別有用,可顯著提高處理速度。 如何設定進階閱讀設定以提高精確度? IronBarcode 提供 BarcodeReaderOptions 類以配置進階讀取設定。這包括自訂條碼偵測靈敏度、影像預處理和其他參數的選項,這些選項可在處理具有挑戰性的影像或特定條碼類型時提高準確性。 是什麼讓 C# 開發人員從影像讀取 BarCode 變得簡單? IronBarcode 通過提供 BarcodeReader.Read() 方法簡化了條碼讀取,該方法開箱即用,可使用多種圖像格式。開發人員只需指定文件名或文件路徑作為參數,這使得將條碼讀取功能集成到任何 C# 應用程序中變得非常簡單直接。 Hairil Hasyimi Bin Omar 立即與工程團隊聊天 軟體工程師 就像所有優秀的工程師一樣,Hairil 也是一位狂熱的學習者。他不斷精進 C#、Python 和 Java 的知識,利用這些知識為整個 Iron Software 的團隊成員增加價值。Hairil 從馬來西亞的 Universiti Teknologi MARA 大學加入 Iron Software 團隊,畢業於該校的化學與流程工程學系,並取得學士學位。 準備好開始了嗎? Nuget 下載 2,070,733 | 版本: 2026.2 剛剛發布 免費 NuGet 下載 總下載量:2,070,733 查看許可證