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

