如何在 C# 中讀取影像中的二維碼
IronQR 使開發人員能夠透過 C# 讀取各種圖像格式的二維碼,方法是使用 IronDrawing 載入圖像,創建QrImageInput對象,並使用BarcodeReader.Read方法高效地解碼二維碼資料。
快速入門:使用 C# 從影像讀取二維碼
如何從圖片中讀取二維碼
- 下載用於讀取影像中二維碼的 C# 庫
- 使用 IronDrawing 導入影像數據
- Create a `QrImageInput` object from the image data
- Pass the object to the
`Read`method - 遍歷每個檢測到的二維碼並查看其訊息
如何讀取不同影像格式的二維碼?
IronQR 提供內建支持,可讀取各種影像格式的二維碼。 此功能採用先進的機器學習模型,確保對不同媒體類型進行精準解碼。支援的格式包括:
- 聯合影像專家小組 (JPEG)
- 可攜式網路圖形 (PNG)
- 圖形交換格式 (GIF)
- 標籤影像檔案格式 (TIFF) 點陣圖影像檔(BMP)
- WBMP WebP
- 圖示 (ico)
- WMF
- RawFormat(原始格式)
這種格式支援是透過開源程式庫IronDrawing實現的,該程式庫能夠有效率地處理影像。 您可以直接處理來自數位相機、掃描器、行動裝置或網路下載的二維碼,無需進行格式轉換。

立即開始使用 NuGet 建立 PDF 檔案:
使用 NuGet 套件管理器安裝 IronQR
複製並運行這段程式碼。
// Import necessary IronQR and IronDrawing namespaces using IronSoftware.Drawing; using IronBarcode; public class QRCodeReader { public static void Main() { // Load an image from a file path using (var inputImage = Image.FromFile("path/to/your/image/file.webp")) { // Create a QrImageInput object from the image var qrImageInput = new QrImageInput(inputImage); // Decode the QR 圖碼 from the image var result = BarcodeReader.Read(qrImageInput); // Iterate through each detected QR 圖碼 and display its information foreach (var barcodeResult in result.Barcodes) { Console.WriteLine($"QR Code Data: {barcodeResult.Value}"); } } } }部署到您的生產環境進行測試
注意:請將"path/to/your/image/file.webp"替換為您的二維碼圖像檔案的實際路徑。
想知道範例圖片中的二維碼值嗎?不妨試試這段程式碼!
IronQR為何支援多種影像格式?
讀取二維碼是指掃描和解碼二維碼中儲存的資訊。 這通常是使用相機或掃描儀,配合能夠解讀二維碼資料的軟體來完成的。 二維碼中的資訊可以是文字、網址、聯絡資訊或其他形式的資料。
IronQR 的多格式支援對於現實世界的應用至關重要,因為二維碼會出現在各種環境中——從行銷資料和產品包裝到數位文件和網路內容。 IronQR 透過支援多種格式,確保開發人員能夠建立強大的應用程序,而無需擔心影像格式相容性。 了解更多關於 IronQR讀取功能的信息,以了解這種靈活性如何增強您的開發工作流程。
我應該在什麼情況下使用哪種圖像格式?
不同的影像格式在二維碼處理中發揮不同的作用:
- PNG :最適合需要透明度的二維碼或對影像品質要求極高的情況。 PNG 的無損壓縮確保二維碼圖案保持清晰易讀。
- JPEG :非常適合包含二維碼的照片或對檔案大小有要求的情況。 使用更高的品質設定(80%以上)以防止壓縮偽影影響可讀性。
- TIFF :非常適合用於存檔或在企業環境中處理掃描文件。
- WebP :現代格式,提供出色的壓縮效果和品質保持,是 Web 應用程式的理想選擇。
為了獲得最佳效果,請確保您的影像具有足夠的解析度(列印二維碼至少需要 300 DPI)和對比度。請查看我們的進階二維碼讀取範例,以了解針對不同格式的最佳化技巧。
如果影像品質差會怎樣?
IronQR 具有容錯功能,可以處理不完美的影像。 在處理低品質影像時,該庫採用了以下幾種策略:
1.糾錯:二維碼具備糾錯功能(L、M、Q、H 級),即使二維碼損壞高達 30%,也能恢復資料。
2.機器學習增強:IronQR 的機器學習模型能夠偵測並補償模糊、失真和光照不足等常見問題。
3.預處理:自動影像增強功能可在解碼嘗試之前提高對比度和清晰度
對於複雜的場景,可以考慮使用自訂二維碼讀取模式選項來微調讀取過程:
// Example: Reading QR 圖碼s with enhanced error correction
using IronBarcode;
public class EnhancedQRReader
{
public static void ReadPoorQualityImage()
{
// Configure reader with multiple attempts and error correction
var options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed, // More thorough scanning
ExpectMultipleBarcodes = true, // Check for multiple codes
ExpectBarcodeTypes = BarcodeType.QRCode // Focus on QR 圖碼s only
};
using (var inputImage = Image.FromFile("blurry_qr_code.jpg"))
{
var qrImageInput = new QrImageInput(inputImage);
var results = BarcodeReader.Read(qrImageInput, options);
foreach (var result in results.Barcodes)
{
Console.WriteLine($"Decoded: {result.Value}");
Console.WriteLine($"Confidence: {result.Confidence}%");
}
}
}
}// Example: Reading QR 圖碼s with enhanced error correction
using IronBarcode;
public class EnhancedQRReader
{
public static void ReadPoorQualityImage()
{
// Configure reader with multiple attempts and error correction
var options = new BarcodeReaderOptions
{
Speed = ReadingSpeed.Detailed, // More thorough scanning
ExpectMultipleBarcodes = true, // Check for multiple codes
ExpectBarcodeTypes = BarcodeType.QRCode // Focus on QR 圖碼s only
};
using (var inputImage = Image.FromFile("blurry_qr_code.jpg"))
{
var qrImageInput = new QrImageInput(inputImage);
var results = BarcodeReader.Read(qrImageInput, options);
foreach (var result in results.Barcodes)
{
Console.WriteLine($"Decoded: {result.Value}");
Console.WriteLine($"Confidence: {result.Confidence}%");
}
}
}
}從二維碼中取得值
[//]: # (這是一則會被隱藏的評論。 上述範例中已經提到了價值。 我會在再次定義它時採取更簡潔但更具解釋性的方式)
大多數 IronQR 函數傳回一個集合,以支援多種檢測方式。 由於results是一個物件序列,它本身沒有Value屬性。 範例程式碼專門從集合中選擇第一個QrResult並檢索其值。
:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-image-value.csusing System;
using System.Collections.Generic;
using System.Linq;
using IronQr;
using System.Drawing;
// Import image
var inputImage = Image.FromFile("sample.jpg");
// Load the asset into QrImageInput
QrImageInput imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
QrReader reader = new QrReader();
// Read the Input an get all embedded QR Codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// Display the value of the first QR code found
Console.WriteLine($"QR code value is {results.First().Value}");偵測影像中二維碼的位置
IronQR 不僅限於簡單的解碼,還能精確定位二維碼在影像中的位置。 此定位採用標準座標系,其中 PointF (0,0) 表示影像的左上角。 可以透過Points[]陣列存取二維碼角點的確切空間座標。
在這個例子中,偵測到的二維碼的四個點的座標被檢索出來並印到控制台。
此函數傳回的座標以嚴格的"之字形"順序儲存:左上、右上、左下,最後是右下。
:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-image-position.csusing System;
using System.Collections.Generic;
using IronQr;
using System.Drawing;
using System.Linq;
// Import an image containing a QR code
var inputImage = Image.FromFile("urlQr.png");
// Load the asset into a QrImageInput object
var imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
var reader = new QrReader();
// Read the input and get all embedded QR codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// [TL, TR, BL, BR]
string[] labels = { "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right" };
var points = results.First().Points;
for (int i = 0; i < points.Length; i++)
{
Console.WriteLine($"{labels[i]}: {points[i].X}, {points[i].Y}");
}輸入二維碼

輸出
請注意,系統已將每個二維碼角點的精確空間座標記錄到控制台。

支援的二維碼類型
建立和讀取多種類型的二維碼均受支援。 IronQR 提供對各種二維碼格式的全面支持,以滿足不同的應用需求。 請在我們的文件中了解更多關於支援的二維碼格式的資訊。 以下是支援的二維碼類型:
-二維碼:目前最常用的標準二維碼。 它可以儲存多達 7,089 個數字字符或 4,296 個字母數字字符,因此適用於網站 URL、聯絡資訊和其他應用。

-微型二維碼:一種尺寸較小的標準二維碼,專為空間有限而設計。 它可以儲存多達 35 個數字字元或 21 個字母數字字符,非常適合小型包裝或小型印刷標籤。

- RMQRCode :RMQR 碼(矩形微型二維碼)是緊湊的矩形版本,而不是正方形。 此版本允許靈活調整縱橫比,適用於有矩形空間的應用。

如何選擇合適的二維碼類型?
選擇合適的二維碼類型取決於您的特定使用情境和限制條件:
-標準二維碼:適用於空間不受限制且需要最大資料容量的通用應用。 非常適合用於網址、WiFi憑證、vCard聯絡人或詳細的產品資訊。 請參閱我們的二維碼產生範例以了解實作細節。
-微型二維碼:非常適合用於電子元件、珠寶標籤或醫療設備等小型表面。 儘管容量有限,但它非常適合序號、簡單網址或基本追蹤程式碼。
- RMQR 代碼:當可用空間有特定的尺寸限制時,例如圓柱形產品上的窄標籤或包裝邊緣的細長空間,請選擇矩形代碼。
資料儲存有哪些限制?
了解資料容量有助於最佳化二維碼的部署:
| QR 圖碼類型 | 僅限數字 | 字母數字 | 二進位 | 漢字 |
|---|---|---|---|---|
| 標準二維碼 | 7,089 | 4,296 | 2,953 | 1,817 |
| 微型二維碼 | 35 | 21 | 15 | 9 |
| RMQR | 多變的 | 多變的 | 多變的 | 多變的 |
規劃資料儲存時,請考慮以下因素: 使用網址縮短服務來縮短網頁鏈接,以最大限度地利用可用空間
- 實現大型資料集的資料壓縮
- 選擇適當的糾錯等級(糾錯等級越高,處理能力越低)
對於更高級的實現方式,請探索我們的樣式化二維碼生成指南,以平衡美觀性和資料容量。
何時應該使用微碼或RMQR碼?
Micro 和 RMQR 程式碼在特定場景下表現出色:
微型二維碼非常適合用於: 需要元件追蹤的電子電路板 需要病患或藥物識別碼的小型醫療設備
- 珠寶鑑定,但刻字空間有限
- 生產製造中的微型產品標籤
RMQR 程式碼最適用於:
- 管材或管道上的窄型運輸標籤 筆桿或工具上的細長空間
- 橫幅式行銷資料
- 融入現有的矩形設計元素
以下是一個讀取不同類型二維碼的實際範例:
using IronBarcode;
using IronSoftware.Drawing;
public class MultiTypeQRReader
{
public static void ReadVariousQRTypes()
{
// Configure reader to handle all QR 圖碼 types
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeType.QRCode |
BarcodeType.MicroQRCode |
BarcodeType.RectangularMicroQRCode
};
string[] imagePaths = {
"standard_qr.png",
"micro_qr.png",
"rectangular_qr.png"
};
foreach (var path in imagePaths)
{
using (var image = Image.FromFile(path))
{
var qrInput = new QrImageInput(image);
var results = BarcodeReader.Read(qrInput, options);
foreach (var qr in results.Barcodes)
{
Console.WriteLine($"Type: {qr.BarcodeType}");
Console.WriteLine($"Data: {qr.Value}");
Console.WriteLine($"Format: {qr.Format}");
Console.WriteLine("---");
}
}
}
}
}using IronBarcode;
using IronSoftware.Drawing;
public class MultiTypeQRReader
{
public static void ReadVariousQRTypes()
{
// Configure reader to handle all QR 圖碼 types
var options = new BarcodeReaderOptions
{
ExpectBarcodeTypes = BarcodeType.QRCode |
BarcodeType.MicroQRCode |
BarcodeType.RectangularMicroQRCode
};
string[] imagePaths = {
"standard_qr.png",
"micro_qr.png",
"rectangular_qr.png"
};
foreach (var path in imagePaths)
{
using (var image = Image.FromFile(path))
{
var qrInput = new QrImageInput(image);
var results = BarcodeReader.Read(qrInput, options);
foreach (var qr in results.Barcodes)
{
Console.WriteLine($"Type: {qr.BarcodeType}");
Console.WriteLine($"Data: {qr.Value}");
Console.WriteLine($"Format: {qr.Format}");
Console.WriteLine("---");
}
}
}
}
}對於生產環境部署,請查看我們的NuGet 套件指南,以確保您擁有適合您平台的正確套件,並查看API 參考,以取得所有可用方法和屬性的全面文件。
常見問題解答
IronQR 支援哪些影像格式來讀取 QR 碼?
IronQR 支援從多種影像格式讀取 QR 碼,包括 JPEG、PNG、GIF、TIFF、BMP、WBMP、WebP、ICO、WMF 和 RawFormat。這種全面的格式支援由 IronDrawing 提供,讓您無需轉換格式即可處理各種來源的 QR 代碼。
如何從C#的圖像文件中讀取QR碼?
要使用 IronQR 讀取 QR 碼,首先要使用 Image.FromFile() 載入您的影像,從載入的影像建立 QrImageInput 物件,然後再使用 BarcodeReader.Read() 來解碼 QR 資料。該方法會返回結果,您可以遍歷這些結果來存取每個偵測到的 QR 代碼資訊。
什麼技術可以在不同的媒體類型中準確讀取 QR 碼?
IronQR 使用先進的機器學習模型,以確保不同媒體類型和圖像格式的 QR 碼解碼都能準確無誤。這種人工智能驅動的方法有助於在具有挑戰性的圖像條件下保持高準確性。
我可以從單一影像讀取多個 QR 代碼嗎?
是的,IronQR 可以從單一影像偵測並讀取多個 QR 代碼。BarcodeReader.Read 方法會返回一個結果集合,讓您可以使用 foreach 環路遍歷每個偵測到的 QR 碼,以存取個別 QR 碼資料。
IronQR 讀取的 QR 碼可以儲存哪些類型的資料?
IronQR 可以解碼 QR 代碼中儲存的各種類型資料,包括純文字、URL、聯絡資訊和其他形式的結構化資料。解碼後的資訊可透過每個 BarcodeResult 物件的 Value 屬性存取。






