如何在 C# 中讀取 QR Code 內容
即時提取任何 QR Code 圖像中的解碼文字。 獲取即時值,用於顯示、儲存或在您的應用程式中處理。
讀取 QR Code 的值是任何掃描工作流程中的第一步。 支付終端需要 QR Code 中嵌入的交易 ID。 倉庫系統需要標籤上的產品參考。 票券驗證需要印在活動通行證上的訂單程式碼。 IronQR 使這一過程變得簡單:載入圖像,將其傳遞給 QrReader,並直接從結果中讀取解碼字串。
本指南示範如何使用 IronQR程式庫從圖像中提取 QR Code 值。 尚未生成 QR Code 的開發者應首先參考 將 QR Code 建立為圖像 指南。
快速開始:讀取 QR Code 值
載入圖像,用 QrReader 掃描,並提取解碼的字串。
最小化工作流程(共5步)
- 下載 IronQR C# 程式庫來讀取 QR Code 值
- 載入圖像並將其包裝在
QrImageInput中 - 建立一個
QrReader實例,並使用輸入呼叫Read - 通過
QrResult.Value存取解碼字串 - 在存取
.First()之前使用results.Any()進行保護
如何從圖像中讀取 QR Code 值?
要提取 QR Code 中嵌入的值,請將圖像載入到 QrImageInput,將其傳遞給 QrReader.Read(),然後存取返回的 QrResult 上的 Value 屬性。 該方法返回一個集合,每個在圖像中找到的 QR Code 對應一個結果。
輸入
以下的 QR Code 編碼了 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 Code。 如果圖像中包含多個 QR Code,請使用 foreach (var result in results) 迭代以處理每一個。 在調用 .First() 之前使用 results.Any() 進行保護,以處理未找到 QR Code 的圖像。
輸出
如何讀取所有 QR Code 屬性?
每個 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 |
原始解碼字串,完全如編碼時一樣。 可以包含 URL、純文字、數字 ID、JSON 或任何其他資料。 這是大多數應用程式的主要屬性。 |
Url |
Uri |
當 Value 是有效的絕對 URL 時,解析的 Uri 物件將被填充。 用於打開連結、驗證域或提取 URL 組件。 如果值不是 URL,則返回 null。 |
Points |
PointF[] |
四個角點坐標標記 QR Code 在源圖像中的位置,按 [Top-Left, Top-Right, Bottom-Left, Bottom-Right] 排序。 可用於繪製邊界框、裁剪區域或計算掃描範圍。 |
QR Code 值讀取的常見用例有哪些?
- 支付終端: 從客戶的 QR Code 中解碼交易 URL 或參考 ID 以啟動支付流程。
- 票券驗證: 從印刷或螢幕 QR Code 中提取訂位參考以驗證活動入場。
- 庫存管理: 從倉庫標籤中讀取產品序列號或 SKU 以更新庫存記錄。
- 文件驗證: 從加蓋在法律或政府文件上的 QR Code 中提取記錄 ID 或哈希值。
- 使用者身份驗證: 從 QR Code 解碼一次性令牌以完成雙因素登錄步驟。
有關更多 QR Code 讀取模式,請探索 從圖像讀取 QR Code 指南和完整的 IronQR 功能集。
常見問題
我可以如何在C#中讀取QR Code值?
您可以在C#中使用IronQR讀取QR Code值。QrReader.Read()方法允許您使用QrResult.Value從QR Code中提取解碼字串。
IronQR中用於解碼QR Code的方法是什麼?
IronQR使用QrReader.Read()方法解碼QR Code,允許您提取例如文字和URL等資料。
IronQR可以從QR Code中提取URL嗎?
是的,IronQR可以在解碼QR Code後使用QrResult.Url屬性解析URL。
使用IronQR是否可以獲取QR Code的角落座標?
IronQR提供了QrResult.Points屬性,可以檢索QR Code的角落座標,給您精確的位置資訊。
IronQR中的QrResult.Value是什麼?
QrResult.Value是IronQR中的一個屬性,當QrReader.Read()處理完QR Code時,它持有了解碼後的字串值。
IronQR是否支持從QR Code中讀取多種型別的資料?
是的,IronQR支持讀取包括文字、URL和座標在內的各種資料型別,為不同應用提供多功能選擇。
IronQR的QR Code解碼功能有多準確?
IronQR的設計目的是提供高精度的QR Code解碼,能有效提取如值、URL和角落點等詳細資訊。
IronQR可以用於靜態和動態QR Code嗎?
是的,IronQR可以解碼靜態和動態的QR Code,使它成為不同QR Code應用的靈活工具。
IronQR相容什麼程式語言?
IronQR相容C#,允許開發人員輕鬆整合QR Code讀取功能到他們的.NET應用中。
有什麼方法可以測試IronQR的QR Code讀取功能?
您可以使用範例QR Code和文件中提供的範例程式碼測試IronQR的QR Code讀取功能,確保整合符合您的專案需求。

