如何在 C# 中读取二维码值
即时从任何二维码图像中提取解码后的文本。 获取原始字符串值,以便在您的应用程序中进行显示、存储或处理。
读取二维码的值是任何扫描工作流的第一步。 支付终端需要将交易 ID 嵌入到二维码中。 仓库系统需要在标签上标注产品编号。 票务验证器需要活动通行证上印有的预订代码。 IronQR 让这一过程变得简单:加载图像,将其传递给 QrReader,然后直接从结果中读取解码后的字符串。
本指南演示了如何使用 IronQR库从图像中提取二维码值。 尚未生成二维码的开发者,请先参阅《将二维码生成图片》指南。
快速入门:读取二维码值
加载一张图片,使用 QrReader 对其进行扫描,并提取解码后的字符串。
最小工作流程(5 个步骤)
- 下载 IronQR C# 库以读取二维码值
- 加载图片并将其封装在 `QrImageInput` 中
- 创建一个 `QrReader` 实例,并使用输入调用 `Read`
- 通过 `QrResult.Value` 访问解码后的字符串
- 在调用 `.First()` 之前,请使用 `results.Any()` 进行检查
如何扫描二维码并从图像中读取其值?
要提取 QR 码中包含的信息,请将图像加载到 QrImageInput 中,将其传递给 QrReader.Read(),然后访问返回的 QrResult 对象上的 Value 属性。 该方法返回一个集合,其中包含图像中每个被识别的二维码对应的结果。
输入
下方的二维码编码了 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>,即使仅存在一个二维码也是如此。 如果图片包含多个二维码,请使用 foreach (var result in results) 循环处理每个二维码。 在调用 .First() 处理未发现二维码的图片前,请先使用 results.Any() 进行保护。
输出
如何读取所有二维码属性?
每个 QrResult 都暴露了三个属性,这些属性共同呈现了被扫描内容的完整信息及其在图像中的位置。 使用上文中的同一张输入二维码:
: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[] |
标记源图像中二维码位置的四个角坐标,顺序为 [Top-Left, Top-Right, Bottom-Left, Bottom-Right]。 使用它来绘制边界框、裁剪区域或计算扫描面积。 |
二维码值读取有哪些常见应用场景?
- 支付终端:通过解析客户二维码中的交易 URL 或参考 ID 来启动支付流程。
- 票务验证:从纸质或屏幕上的二维码中提取预订编号,以验证活动入场资格。
- 库存管理:读取仓库标签上的产品序列号或 SKU 以更新库存记录。
- 文件验证:从法律或政府文件上加盖的二维码中提取记录 ID 或哈希值。
- 用户身份验证:从二维码中解码一次性令牌,以完成双因素登录步骤。
如需了解更多的二维码读取模式,请参阅《从图像读取二维码》指南以及完整的 IronQR 功能集。

