如何在 C# 中讀取 QR code 值
立即從任何 QR 碼圖像中提取解碼後的文字。 將原始字串值準備好,以便在您的應用程式中顯示、儲存或處理。
讀取 QR 碼的值是任何掃描工作流程的第一步。 支付終端機需要將交易 ID 嵌入 QR 碼中。 倉儲系統需要在標籤上標示產品編號。 驗票機需要活動通行證上印有的預訂代碼。 IronQR 讓這一切變得簡單:載入圖片、將其傳遞給 QrReader,然後直接從結果中讀取解碼後的字串。
本指南將示範如何使用 IronQR程式庫從圖片中擷取 QR 碼的值。 尚未生成 QR 碼的開發人員,請先參閱《將 QR 碼儲存為圖片》指南。
快速入門:讀取 QR 碼值
載入一張圖片,使用 QrReader 進行掃描,並提取解碼後的字串。
最小工作流程(5 個步驟)
- 下載 IronQR C# 程式庫以讀取 QR 碼值
- 載入圖片並將其封裝在 `QrImageInput` 中
- 建立 `QrReader` 實例,並傳入輸入值呼叫 `Read` 方法
- 透過 `QrResult.Value` 存取解碼後的字串
- 在存取 `.First()` 之前`,`請先使用 `Any()` 進行檢查`。`
如何從圖片中讀取 QR 碼的值?
要提取 QR 碼中嵌入的資料,請將圖片載入 QrImageInput,傳遞給 QrReader.Read(),並存取回傳的 QrResult 物件上的 Value 屬性。 此方法會傳回一個集合,其中每個結果對應影像中找到的一個 QR 碼。
輸入
下方的 QR 碼編碼了 https://ironsoftware.com,掃描後將提取其值。
:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-value.cs
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 and 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}");
Imports IronQr
Imports System.Drawing
Imports System.Linq
' Import image
Dim inputImage As Image = Image.FromFile("sample.jpg")
' Load the asset into QrImageInput
Dim imageInput As New QrImageInput(inputImage)
' Create a QR Reader object
Dim reader As New QrReader()
' Read the input and get all embedded QR codes
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
' Display the value of the first QR code found
Console.WriteLine($"QR code value is {results.First().Value}")
Value 屬性會返回與原始編碼完全一致的解碼原始字串:URL、數字、自由文字或任何其他資料。 Read() 方法總是會傳回 IEnumerable<QrResult>,即使僅存在一個 QR 碼也是如此。 若圖片包含多個 QR 碼,請使用 foreach (var result in results) 進行迭代處理,以處理每個 QR 碼。 在呼叫 .First() 之前,請先使用 results.Any() 進行防護,以處理未找到 QR 碼的圖片。
輸出
如何讀取所有 QR 碼屬性?
每個 QrResult 會揭露三個屬性,這些屬性共同呈現了掃描內容的完整樣貌,以及該內容在圖像中的位置。 使用上方的相同輸入 QR 碼:
:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-value-properties.cs
using IronQr;
using IronSoftware.Drawing;
AnyBitmap inputImage = AnyBitmap.FromFile("sample.jpg");
QrImageInput imageInput = new QrImageInput(inputImage);
QrReader reader = new QrReader();
IEnumerable<QrResult> results = reader.Read(imageInput);
QrResult result = results.First();
// Decoded text content of the QR code
Console.WriteLine($"Value: {result.Value}");
// Parsed URL — populated when Value is a valid URL, null otherwise
Console.WriteLine($"Url: {result.Url}");
// Corner coordinates of the QR code in the image [TL, TR, BL, BR]
string[] labels = { "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right" };
for (int i = 0; i < result.Points.Length; i++)
{
Console.WriteLine($"{labels[i]}: ({result.Points[i].X}, {result.Points[i].Y})");
}
Imports IronQr
Imports IronSoftware.Drawing
Dim inputImage As AnyBitmap = AnyBitmap.FromFile("sample.jpg")
Dim imageInput As New QrImageInput(inputImage)
Dim reader As New QrReader()
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
Dim result As QrResult = results.First()
' Decoded text content of the QR code
Console.WriteLine($"Value: {result.Value}")
' Parsed URL — populated when Value is a valid URL, Nothing otherwise
Console.WriteLine($"Url: {result.Url}")
' Corner coordinates of the QR code in the image [TL, TR, BL, BR]
Dim labels As String() = {"Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right"}
For i As Integer = 0 To result.Points.Length - 1
Console.WriteLine($"{labels(i)}: ({result.Points(i).X}, {result.Points(i).Y})")
Next i
輸出
QrResult 公開了哪些屬性?
QrResult 在掃描成功後會公開以下屬性:
| 屬性 | 類型 | 說明 |
|---|---|---|
Value |
string |
原始解碼字串與編碼字串完全一致。 可儲存網址、純文字、數值 ID、JSON 或任何其他資料。 這是大多數應用程式的主要屬性。 |
Url |
Uri |
當 Value 為有效的絕對網址時,會建立一個已解析的 Uri 物件。 可用於開啟連結、驗證網域或擷取 URL 組成部分。 若值非 URL,則返回 null。 |
Points |
PointF[] |
標示 QR 碼在原始圖片中位置的四個角座標,順序為 [Top-Left, Top-Right, Bottom-Left, Bottom-Right]。 用於繪製邊界框、裁切區域或計算掃描範圍。 |
QR 碼值讀取有哪些常見的應用情境?
- 支付終端機:解碼客戶 QR 碼中的交易 URL 或參考 ID,以啟動支付流程。
- 票券驗證:從紙本或螢幕上的 QR 碼中提取預訂編號,以驗證活動入場資格。
- 庫存管理:讀取倉儲標籤上的產品序號或 SKU,以更新庫存紀錄。
- 文件驗證:從法律或政府文件上加蓋的 QR 碼中提取記錄 ID 或雜湊值。
- 使用者驗證:從 QR 碼解碼一次性憑證,以完成雙因素驗證步驟。
如需更多 QR 碼讀取模式,請參閱《從圖片讀取 QR 碼》指南及完整的 IronQR 功能集。

