如何在 C# 中讀取 QR code 值

This article was translated from English: Does it need improvement?
Translated
View the article in English

立即從任何 QR 碼圖像中提取解碼後的文字。 將原始字串值準備好,以便在您的應用程式中顯示、儲存或處理。

讀取 QR 碼的值是任何掃描工作流程的第一步。 支付終端機需要將交易 ID 嵌入 QR 碼中。 倉儲系統需要在標籤上標示產品編號。 驗票機需要活動通行證上印有的預訂代碼。 IronQR 讓這一切變得簡單:載入圖片、將其傳遞給 QrReader,然後直接從結果中讀取解碼後的字串。

本指南將示範如何使用 IronQR程式庫從圖片中擷取 QR 碼的值。 尚未生成 QR 碼的開發人員,請先參閱《將 QR 碼儲存為圖片》指南。

快速入門:讀取 QR 碼值

載入一張圖片,使用 QrReader 進行掃描,並提取解碼後的字串。

  1. 使用NuGet套件管理器安裝https://www.nuget.org/packages/IronQR

    PM > Install-Package IronQR
  2. 複製並運行這段程式碼。

    var input = new QrImageInput("qr-code.png");
    var results = new QrReader().Read(input);
    Console.WriteLine(results.First().Value);
  3. 部署到您的生產環境進行測試

    今天就在您的專案中開始使用免費試用IronQR

    arrow pointer

如何從圖片中讀取 QR 碼的值?

要提取 QR 碼中嵌入的資料,請將圖片載入 QrImageInput,傳遞給 QrReader.Read(),並存取回傳的 QrResult 物件上的 Value 屬性。 此方法會傳回一個集合,其中每個結果對應影像中找到的一個 QR 碼。

輸入

下方的 QR 碼編碼了 https://ironsoftware.com,掃描後將提取其值。

作為掃描輸入的 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}")
$vbLabelText   $csharpLabel

Value 屬性會返回與原始編碼完全一致的解碼原始字串:URL、數字、自由文字或任何其他資料。 Read() 方法總是會傳回 IEnumerable<QrResult>,即使僅存在一個 QR 碼也是如此。 若圖片包含多個 QR 碼,請使用 foreach (var result in results) 進行迭代處理,以處理每個 QR 碼。 在呼叫 .First() 之前,請先使用 results.Any() 進行防護,以處理未找到 QR 碼的圖片。

輸出

顯示解碼後 QR 碼值的控制台輸出 https://ironsoftware.com

如何讀取所有 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
$vbLabelText   $csharpLabel

輸出

顯示從 QR 碼讀取的 Value、Url 和 Points 屬性的主控台輸出

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 功能集

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

準備好開始了嗎?
Nuget 下載 63,625 | 版本: 2026.4 剛剛發布
Still Scrolling Icon

還在捲動嗎?

想要快速證明? PM > Install-Package IronQR
執行範例 觀看您的 URL 變成 QR code。