如何在 C# 中讀取 QR 碼值
立即從任何 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 功能集。
常見問題
如何在 C# 中讀取 QR 碼的值?
您可以在 C# 中使用 IronQR 讀取 QR 碼的值。透過 QrReader.Read() 方法,您可以利用 QrResult.Value 從 QR 碼中提取解碼後的字串。
IronQR 採用何種方法解碼 QR 碼?
IronQR 利用 QrReader.Read() 方法解碼 QR 碼,讓您能夠擷取文字和 URL 等資料。
IronQR 能否從 QR 碼中提取 URL?
是的,IronQR 可以在解碼 QR 碼後,透過 QrResult.Url 屬性解析 QR 碼中的 URL。
是否可以使用 IronQR 取得 QR 碼的角落座標?
IronQR 提供 QrResult.Points 屬性,可擷取 QR 碼的角落座標,為您提供精確的位置資料。
IronQR 中的 QrResult.Value 是什麼?
QrResult.Value 是 IronQR 中的一個屬性,用於儲存經由 QrReader.Read() 處理後,QR 碼所解碼的字串值。
IronQR 是否支援從 QR 碼讀取多種資料類型?
是的,IronQR 支援從 QR 碼讀取各種資料類型,包括文字、URL 和座標,為不同應用場景提供多樣化的功能。
IronQR 的 QR 碼解碼功能精準度如何?
IronQR 旨在提供高度精準的 QR 碼解碼功能,能高效地擷取數值、URL 及角點等詳細資訊。
IronQR 是否可用於靜態和動態 QR 碼?
是的,IronQR 能夠解碼靜態與動態 QR 碼,使其成為適用於各種 QR 碼應用的靈活工具。
IronQR 相容於哪些程式語言?
IronQR 與 C# 相容,讓開發人員能輕鬆將 QR 碼讀取功能整合至其 .NET 應用程式中。
是否有方法可以測試 IronQR 的 QR 碼讀取功能?
您可以使用範例 QR 碼以及文件中提供的範例程式碼,測試 IronQR 的 QR 碼讀取功能,以確保此整合方案符合您的專案需求。

