如何在 C# 中讀取 QR 碼值
立即從任何 QR 碼圖像中提取解碼後的文字。 將原始字串值準備好,以便在您的應用程式中顯示、儲存或處理。
讀取 QR 碼的值是任何掃描工作流程的第一步。 支付終端機需要將交易 ID 嵌入 QR 碼中。 倉儲系統需要在標籤上標示產品編號。 驗票機需要活動通行證上印有的預訂代碼。 IronQR使這一步驟簡單:載入圖片,傳遞到QrReader,然後直接從結果中讀取解碼後的字串。
本指南展示如何使用IronQR程式庫從圖片中提取QR碼的值。 尚未生成QR碼的開發者應該先參考創建QR碼為圖片指南。
快速入門:讀取 QR 碼值載入圖片,用QrReader掃描,並提取解碼後的字串。
-
1使用 IronQR 安裝 NuGet Package Manager
-
2複製並運行這段程式碼。
var input = new QrImageInput(AnyBitmap.FromFile("qr-code.png")); var results = new QrReader().Read(input); Console.WriteLine(results.First().Value);Dim input = New QrImageInput(AnyBitmap.FromFile("qr-code.png")) Dim results = New QrReader().Read(input) Console.WriteLine(results.First().Value)Dim input = New QrImageInput(AnyBitmap.FromFile("qr-code.png")) Dim results = New QrReader().Read(input) Console.WriteLine(results.First().Value) -
3部署到您的生產環境進行測試
立即在您的項目中開始使用 IronQR,並使用 免費試用
最小工作流程(5 個步驟)
- 下載IronQR C#程式庫來讀取QR碼的值
- 載入圖片並將其封裝在
QrImageInput中 - 建立
QrReader實例,並傳入輸入值呼叫Read方法 - 透過
QrResult.Value存取解碼後的字串 - 在存取
.First()之前,請先使用results.Any()進行檢查。
如何從圖片中讀取 QR 碼的值?
若要提取嵌入於QR Code中的值,請將圖像加載到QrImageInput,傳遞給QrReader.Read(),然後訪問返回的QrResult上的Value屬性。 此方法會傳回一個集合,其中每個結果對應影像中找到的一個 QR 碼。
輸入
以下QR碼編碼了**https://ironsoftware.com**,將被掃描以提取其值。

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}");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}")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碼,請使用foreach (var result in results)迭代處理每一個。 在調用.First()之前,請用results.Any()保護,以處理未找到QR Code的圖像。
輸出

如何讀取所有 QR 碼屬性?
每個QrResult展示三個屬性,這些屬性結合起來提供掃描的全貌及在圖片中找到的位置。 使用上方的相同輸入 QR 碼:
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})");
}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 iImports 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是一個有效的絕對URL時,解析的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#中使用IronQR讀取QR碼的值?
要使用IronQR讀取QR碼的值,將圖像加載到QrImageInput中,傳遞給QrReader實例,並調用Read()方法。然後,您可以使用QrResult.Value屬性訪問解碼後的字符串。
QrResult中的`Value`屬性用於什麼?
Value屬性在QrResult中儲存了QR碼中精確編碼的原始解碼字符串。這可能是URL、文本或任何其他數據格式。
IronQR能從一個圖像中讀取多個QR碼嗎?
是的,IronQR可以從單個圖像中讀取多個QR碼。Read()方法返回一個IEnumerable<QrResult>,允許您遍歷圖像中找到的每個QR碼。
如何使用IronQR從QR碼中提取URL?
IronQR允許您使用QrResult.Url屬性提取URL,當Value為有效URL時此屬性被填充。這有助於快速訪問或驗證URL。
QrResult中的`Points`屬性代表什麼?
Points屬性在QrResult中包含QR碼在圖像中的角落座標。這有助於定位和可視化QR碼的位置。
讀取QR碼值的一些常見用例是什麼?
常見的用例包括支付處理、票務驗證、庫存管理、文件驗證和通過提取及使用QR碼編碼數據進行用戶身份驗證。
如何使用IronQR處理沒有QR碼的圖像?
在調用.First()或處理結果之前,用results.Any()來保護讀取過程,以處理圖像中找不到QR碼的情況。
IronQR能處理不同類型的QR碼編碼數據嗎?
是的,IronQR可以解碼各種數據類型,如URL、數字ID、文本和JSON,使其在不同應用程式中通用。
是否可以使用IronQR從即時相機輸入中讀取QR碼?
雖然本指南著重於從圖像中讀取,IronQR可以集成到相機系統中。您需要捕捉畫面作為圖像並使用相似的方法進行處理。
如果我尚未生成QR碼,如何開始使用IronQR?
如果您尚未生成QR碼,您可能想從[Create QR Code as Image]指南開始生成QR碼,然後再嘗試讀取它們。
Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。