IronBarcode 操作指南 .NET 從影像讀取條碼 如何使用 C# 從影像讀取 BarCode Hairil Hasyimi Bin Omar 更新:2026年3月29日 下載 IronBarcode NuGet 下載 DLL 下載 開始免費試用 LLM副本 LLM副本 將頁面複製為 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 透過 BarcodeReader.Read(),僅需一行 C# 程式碼即可從圖片中讀取 BARCODE,支援多種圖片格式,包括 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. 如需 支援的條碼格式(包括一維和二維類型)的完整清單,請查看我們全面的說明文件。 快速入門:數秒內從圖片讀取BarCode 只需簡單呼叫 IronBarCode.BarcodeReader.Read(),即可直接從 PNG、JPEG、GIF、BMP 和 TIFF 等圖像檔案格式中擷取 BARCODE 資料。 立即開始使用-無需複雜設置,即可立即見效。 使用NuGet套件管理器安裝https://www.nuget.org/packages/BarCode PM > Install-Package BarCode 複製並運行這段程式碼。 var results = IronBarCode.BarcodeReader.Read("path/to/image.png"); 部署到您的生產環境進行測試 今天就在您的專案中開始使用免費試用IronBarcode Free 30 Day Trial 最小工作流程(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() 方法直接讀取 BARCODE 影像。 IronBarcode 提供簡便的操作方式,使用者只需透過 BarcodeReader.Read() 方法,並指定已包含在專案中的影像檔案名稱字串或檔案路徑字串作為參數,即可讀取該檔案。 最佳實踐是在方法中指定檔案路徑時使用字串字面量"@",以避免在檔案路徑字串中新增多個轉義字元"\"。 在 BarcodeReader.Read() 方法呼叫的末尾附加 Values() 方法,即可將 BARCODE 值取得為 System.String[] 物件。 若要將結果輸出至控制台,您可以使用 foreach 迴圈來遍歷 string[] 陣列中儲存的值,並在迴圈區塊內,將迭代器變數作為參數傳入 Console.WriteLine() 方法中。 IronBarcode 能夠讀取一維 BarCode 格式 (Codabar, Code128, Code39, Code93, UPCE) 以及各種圖像格式的二維BARCODE格式 (Aztec, DataMatrix, QRCode)。 如何配置條碼閱讀器選項以獲得更好的性能? 條碼讀取速度太慢? 圖片中的條碼是否太小,導致 IronBarcode 無法讀取? 只想讀取影像的特定區域? 想從包含多種條碼的圖像中只讀取特定類型的條碼嗎? 想提高整體閱讀能力嗎? BarcodeReaderOptions 允許使用者調整 BarCode 讀取器的行為,以解決所有這些問題。 有關設定BarCode讀取器選項的詳細範例,請參閱我們的完整指南。以下各節將逐一說明 BarcodeReaderOptions 中所有可調整的屬性。 如何指定要讀取圖片的哪個區域? CropArea 是 IronSoftware.Drawing.CropRectangle 類型且位於 BarcodeReaderOptions 中的屬性,可讓使用者指定 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,因此系統將自動管理多執行緒,以提升批次 BarCode 讀取任務的效能。 為什麼要移除假陽性讀數? 此屬性可消除所有誤報的條碼讀取結果。 假陽性條碼讀取是指條碼值的錯誤讀取,但卻被辨識為有效。 這可能是由於排序過程中的錯誤,或是條碼標籤或準備過程中的錯誤所造成的。 因此,將 RemoveFalsePositive 設為 true 將移除錯誤判讀的 BARCODE,從而提升 BARCODE 讀取的準確性。 但是,如果使用者為了追求效能而犧牲準確性,則將此屬性設為false會有所幫助。此屬性的預設值為true 。 MinScanLines 如何影響偵測準確度? MinScanLines 設定了 BarCode 結果被視為有效的最低掃描線數門檻。 預設值為 2。增加此數值可減少誤判,但可能會導致無法偵測到較細或品質較差的 BARCODE。 將數值調低至 1 可提高對難以辨識 BARCODE 的敏感度,但可能導致雜訊增加。 哪些掃描模式適用於不同的使用個案? 定義 IronBarcode 如何掃描和偵測影像中的條碼。 -自動:讀取條碼時,會自動進行影像預處理,並配置最佳的讀取器選項。 為獲得最佳效果和性能,建議採用此方法。 OnlyDetectionModel :掃描影像中的條碼,並傳回其位置的IronSoftware.Drawing.PointF陣列。 此模式無法讀取偵測到的條碼; 它只會傳回每個條碼的位置。 MachineLearningScan :使用機器學習偵測掃描影像中的條碼並讀取它們。 OnlyBasicScan :讀取條碼,無需機器學習偵測、自動影像預處理或讀取器選項配置。 This option can be used with IronBarCode.Slim alone. 閱讀速度設定如何影響精確度? 顧名思義, "速度"屬性使用戶能夠進一步優化 IronBarcode 閱讀器的效能。 與RemoveFalsePositive屬性類似,調整此屬性會降低準確性,但會提高效能。 如需深入瞭解 讀取速度選項,包括效能基準,請參閱我們的詳細指南。它接受ReadingSpeed枚數,共有 4 個等級,如下所示: -速度更快:可實現最快的條碼讀取速度,但會降低準確性。 該過程跳過了影像預處理,經常導致條碼結果為空。 僅當輸入影像清晰銳利時才使用此設定。 平衡:建議將 Speed 屬性設定為此選項。 它透過嘗試對影像進行輕微處理,使條碼區域更加清晰,從而在準確性和讀取性能之間取得平衡,使條碼讀取器能夠檢測到它。 大多數情況下,這種設定足以讓 IronBarcode 讀取條碼影像並產生準確的輸出。 詳細說明:若使用 ReadingSpeed.Balanced 設定無法成功從讀取結果產生 BarCode 值,使用者可選擇使用 ReadingSpeed.Detailed。 IronBarcode 將對影像進行中等處理,以進一步清楚顯示條碼區域,以便條碼閱讀器能夠偵測到條碼。 此設定對於檢測較小或不太清晰的條碼影像非常有用。 ExtremeDetail :由於此設定會佔用大量 CPU 資源,因此最不建議使用。 將對條碼影像進行大量處理,以便閱讀器能夠讀取條碼。 建議使用者在選擇此設定之前,先對影像進行預處理/應用濾鏡。 何時應該使用 Code39 延伸模式? 此設定允許以擴展模式讀取和解釋 Code39 類型的條碼,其中將套用完整的 ASCII 字元集。 將 UseCode39ExtendedMode 設定為 true,將能更精確地讀取 Code39 BARCODE。 如何使用自訂選項實現進階條碼讀取功能? 現在我們已經瞭解了所有使用者可以調整的選項,不論是為了增加效能或精確度,以下是如何在程式碼中應用這些選項。 以下程式碼片段示範了 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, // Minimum scan lines that must agree for a valid result (default 2) MinScanLines = 1, // 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, // Minimum scan lines that must agree for a valid result (default 2) MinScanLines = 1, // 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, ' Minimum scan lines that must agree for a valid result (default 2) .MinScanLines = 1, ' 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() 方法的參數使用。這將在從圖像讀取 BARCODE 時,套用 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,143,620 | 版本: 2026.4 剛剛發布 開始免費試用 免費 NuGet 下載 總下載量:2,143,620 查看許可證 還在捲動嗎? 想要快速證明? PM > Install-Package BarCode 執行範例 看您的字串變成 BarCode。 免費 NuGet 下載 總下載量:2,143,620 查看許可證