如何使用C#從影像中讀取條碼
IronBarcode 可在 C# 中透過 BarcodeReader.Read(),以一行程式碼讀取影像中的條碼,支援 PNG、JPEG、GIF、BMP、TIFF 和 SVG 等多種影像格式,並提供可自訂的選項以提升效能和準確度。
IronBarcode 的主要功能之一是無需額外設定,即可讀取多種影像格式中的條碼。目前支援以下影像格式:
- 可縮放向量圖形 (SVG)
- 聯合影像專家小組 (JPEG)
- 可攜式網路圖形 (PNG)
- 圖形交換格式 (GIF)
- 標籤影像檔案格式 (TIFF)
- 點陣圖影像檔案 (BMP)
這項功能由我們的開源程式庫 IronDrawing 提供。 欲查看支援的條碼格式清單,包括1D和2D類型,請參閱我們的全面文件。
快速入門:數秒內從影像讀取條碼只需呼叫 IronBarCode.BarcodeReader.Read(),即可直接從 PNG、JPEG、GIF、BMP 和 TIFF 等影像檔案擷取條碼資料。無需複雜設定,即可立即取得結果。
-
1使用 IronBarcode 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var results = IronBarCode.BarcodeReader.Read("path/to/image.png");Dim results = IronBarCode.BarcodeReader.Read("path/to/image.png")Dim results = IronBarCode.BarcodeReader.Read("path/to/image.png") -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronBarcode,並使用 免費試用
最小工作流程(5 個步驟)

- 下載C#程式庫以從影像中讀取條碼
- 使用
Read方法從各種影像格式讀取條碼值 - 使用BarcodeReaderOptions類別配置讀取設定
- 使用CropArea屬性指定影像中的條碼區域
- 透過設定ExpectBarcodeTypes屬性來指定要讀取的條碼類型。
如何直接從影像中讀取條碼?
以下是如何使用 IronBarcode 進行條碼讀取。 欲了解如何在C# / .NET中讀取條碼的完整教學,包括PDF處理和批次操作的進階技術,請造訪我們的詳細指南:
using IronBarCode;
using System;
var myBarcode = BarcodeReader.Read(@"image_file_path.jpg"); //image file path
foreach (var item in myBarcode)
{
Console.WriteLine(item.ToString());
}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 itemImports 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

要使用 IronBarcode,請先透過 Microsoft Visual Studio 的 NuGet 套件管理員,將 IronBarcode 程式庫安裝到您的專案中,如下圖所示。接著即可使用 IronBarcode 的 BarcodeReader.Read() 方法直接讀取條碼影像。
IronBarcode 簡化了影像讀取流程:只需將已加入專案的影像檔案名稱字串或檔案路徑字串,作為 BarcodeReader.Read() 方法的參數即可。指定檔案路徑時,建議使用以「@」為前綴的逐字字串常值,以免在檔案路徑字串中加入多個跳脫字元「\」。
在BarcodeReader.Read()方法呼叫的末尾附加Values()方法,以獲取條碼值作為System.String[]物件。
若要將結果輸出至主控台,您可以使用 foreach 迴圈逐一存取 string[] 陣列中的值,並在迴圈區塊內呼叫 Console.WriteLine() 方法,將迭代變數作為參數。
IronBarcode能夠讀取1維條碼格式(Codabar, Code128, Code39, Code93, EAN13, EAN8, ITF, MSI, UPCA, UPCE) 以及2維條碼格式(Aztec, DataMatrix, QRCode)於各種影像格式中。
如何配置條碼讀取器選項以獲得更佳效能?
條碼讀取速度太慢? 圖片中的條碼是否太小,導致 IronBarcode 無法讀取? 只想讀取影像的特定區域? 想從包含多種條碼的影像中只讀取特定類型的條碼嗎? 想提高整體讀取效能嗎?
BarcodeReaderOptions允許使用者調整或調節條碼讀取器的行為以解決所有這些問題。 有關設定條碼讀取選項的詳細範例,請查看我們的指南。以下部分逐一討論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
}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
}我應該指定哪些條碼類型以加快讀取速度?
預設情況下,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
}' Example: Expect only QR codes and Code128
Dim options As New BarcodeReaderOptions() With {
.ExpectBarcodeTypes = BarcodeEncoding.QRCode Or BarcodeEncoding.Code128
}何時應禁用多條碼讀取?
IronBarcode 預設會掃描影像中所有可用的條碼,包括掃描整個影像檔案並將讀取的條碼值新增至字串陣列中。 但是,如果使用者不希望讀取影像檔案中的多個條碼,使用者可以將此屬性設為false ,這樣條碼讀取器在找到條碼值後就會停止掃描。 這將再次提高 IronBarcode 的效能和讀取速度。 有關同時讀取多條碼的更多資訊,請參閱我們的專用指南。
影像濾鏡如何改善條碼識別?
在BarcodeReaderOptions中可以添加的屬性之一是影像濾鏡的集合。 影像濾鏡對於前處理輸入到 IronBarcode 的原始影像非常重要。 要在BarcodeReaderOptions中應用影像濾鏡,使用者必須首先初始化並指定要使用的ImageFilter集合。 欲獲取關於影像矯正技術的全面指導,包括濾鏡的應用,請參考我們的教學。
如何優化執行緒以獲得更好效能?
IronBarcode 允許使用者啟用平行處理並調整執行緒數量,以提升處理速度和效率。平行執行緒是指在不同的處理器核心上同時執行多個執行緒。IronBarcode 的 MaxParallelThreads 屬性預設值為 4,您可以根據電腦的效能和可用資源調整。
我應該啟用多執行緒處理嗎?
此特性使 IronBarcode 能夠並行讀取多個影像。 Multithreaded的預設值為true,因此將自動管理多執行緒以提高批次條碼讀取任務的效能。
為什麼要去除假陽性讀取?
此屬性可消除所有誤報的條碼讀取結果。 假陽性條碼讀取是指條碼值的錯誤讀取,但卻被辨識為有效。 這可能是由於排序過程中的錯誤,或是條碼標籤或準備過程中的錯誤所造成的。 因此,將RemoveFalsePositive設為true將移除假陽性的條碼讀取,從而提高條碼讀取的準確性。 但是,如果使用者為了追求效能而犧牲準確性,則將此屬性設為false會有所幫助。此屬性的預設值為true 。
MinScanLines 如何影響偵測準確度?
MinScanLines 設定將條碼結果視為有效時,至少需要多少條掃描線取得相同結果。預設值為 2。提高此值可減少誤判,但可能無法偵測較細或品質較差的條碼。將此值降至 1 可提高對難以辨識條碼的靈敏度,但也可能引入雜訊。
有哪些掃描模式適用於不同的使用情境?
定義 IronBarcode 如何掃描和偵測影像中的條碼。
Auto:使用自動影像前處理和配置的最佳讀取器選項讀取條碼。 為獲得最佳效果和效能,建議採用此方法。OnlyDetectionModel:掃描影像中的條碼,並以IronSoftware.Drawing.PointF陣列形式傳回其位置。 此模式無法讀取偵測到的條碼; 它只會傳回每個條碼的位置。MachineLearningScan:利用機器學習偵測掃描影像中的條碼並讀取它們。OnlyBasicScan:讀取條碼時不使用機器學習偵測、自動影像前處理或讀取器選項設定。此選項僅使用IronBarCode.Slim即可運作。
讀取速度設定如何影響準確度?
顧名思義,Speed屬性使使用者能夠進一步優化IronBarcode讀取器的效能。 與RemoveFalsePositive屬性相似,調整此屬性可提高效能,但會以精度為代價。 欲深入了解讀取速度選項,包括效能基準,請參閱我們的詳細指南。它接受ReadingSpeed列舉,其中包含以下4個等級:
Faster:啟用最快的條碼讀取但降低準確性。 該過程跳過了影像前處理,經常導致條碼結果為空。 僅當輸入影像清晰銳利時才使用此設定。Balanced:此設定建議用於Speed屬性。 它透過嘗試對影像進行輕微處理,使條碼區域更加清晰,從而在準確性和讀取效能之間取得平衡,使條碼讀取器能夠偵測到它。 大多數情況下,這種設定足以讓 IronBarcode 讀取條碼影像並產生準確的輸出。Detailed:當使用設定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,
// 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 = BarcodeScanMode.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($"Position: {string.Join(", ", barcode.Points.Select(p => $"({p.X}, {p.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 = BarcodeScanMode.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($"Position: {string.Join(", ", barcode.Points.Select(p => $"({p.X}, {p.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 = 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 = BarcodeScanMode.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($"Position: {String.Join(", ", barcode.Points.Select(Function(p) $"({p.X}, {p.Y})"))}")
Console.WriteLine("---")
Next
Else
Console.WriteLine("No barcodes found in the image.")
End IfImports IronBarCode
Imports IronSoftware.Drawing
Imports System
Imports System.Linq
' Create custom reader options
Dim options = 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 = BarcodeScanMode.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($"Position: {String.Join(", ", barcode.Points.Select(Function(p) $"({p.X}, {p.Y})"))}")
Console.WriteLine("---")
Next
Else
Console.WriteLine("No barcodes found in the image.")
End If從程式碼片段中,我們看到要使用BarcodeReaderOptions,首先必須初始化它,然後根據上述屬性確定並調整BarcodeReaderOptions的屬性。 初始化的BarcodeReaderOptions可以作為BarcodeReader.Read()方法的參數與影像檔案一起使用。這將在從影像中讀取條碼時應用BarcodeReaderOptions中的所有設定。
如果您遇到條碼未被識別的問題,我們的故障排除指南提供了解決常見問題的方案和提高條碼掃描準確度的技巧。
常見問題
如何使用IronBarcode在C#中從圖片中讀取條碼?
您可以使用IronBarcode的BarcodeReader.Read()方法,從圖片中讀取條碼。此方法簡化了條碼閱讀,只需一行代碼,並支持多種圖片格式,如PNG、JPEG、GIF、BMP和TIFF。
IronBarcode 支援哪些圖像格式用於條碼讀取?
IronBarcode支持多種圖片格式來讀取條碼,包括PNG、JPEG、GIF、BMP、TIFF和SVG。
如何在IronBarcode中自訂條碼讀取選項?
您可以使用IronBarcode的BarcodeReaderOptions類來自訂條碼讀取選項。這個類讓您配置如CropArea、ExpectBarcodeTypes、Speed等等設置,以提高性能和準確性。
IronBarcode是否可以讀取1D和2D條碼?
是的,IronBarcode可以讀取1D和2D條碼。支持的格式包括Codabar、Code128、QRCode、DataMatrix等。
`CropArea`屬性在IronBarcode中的用途是什麼?
IronBarcode中的CropArea屬性用於指定圖片中應讀取條碼的特定區域。這有助於透過聚焦在定義的區域來提高讀取性能和準確性。
如何通過IronBarcode提高條碼的讀取速度?
您可以通過調整BarcodeReaderOptions中的Speed屬性來提高IronBarcode中的條碼讀取速度。這讓您可以根據需要平衡性能和準確性。
使用IronBarcode的多執行緒有什麼好處?
IronBarcode中的多執行緒允許同時處理多個條碼,提高條碼讀取的速度和效率,特別是在批量處理任務中。
圖像濾鏡如何在IronBarcode中改善條碼識別?
IronBarcode中的圖像濾鏡通過提高條碼圖像的清晰度來增強預處理效果。像SharpenFilter和ContrastFilter這樣的濾鏡有助於優化圖像質量,以改進識別效果。
在IronBarcode中使用`MinScanLines`屬性的用途是什麼?
MinScanLines屬性設定掃描線的最小數量,必須在條碼結果上達成一致才算有效,這可減少誤判同時保證檢測準確性。
如何使用IronBarcode處理多種條碼類型?
在IronBarcode中,您可以透過在BarcodeReaderOptions中設置ExpectBarcodeTypes屬性來指定要讀取的條碼類型,這有助於透過聚焦於特定條碼類型來提高性能。