如何在 C# 中讀取 QR 碼值

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

常見問題

如何在 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 碼讀取功能,以確保此整合方案符合您的專案需求。

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 67,270 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronQR
執行範例 觀看您的 URL 轉為 QR 碼。