如何在 C# 中读取二维码值

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

即时从任何二维码图像中提取解码后的文本。 获取原始字符串值,以便在您的应用程序中进行显示、存储或处理。

读取二维码的值是任何扫描工作流的第一步。 支付终端需要将交易 ID 嵌入到二维码中。 仓库系统需要在标签上标注产品编号。 票务验证器需要活动通行证上印有的预订代码。 IronQR 让这一过程变得简单:加载图像,将其传递给 QrReader,然后直接从结果中读取解码后的字符串。

本指南演示了如何使用 IronQR库从图像中提取二维码值。 尚未生成二维码的开发者,请先参阅《将二维码生成图片》指南。

快速入门:读取二维码值

加载一张图片,使用 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 码中包含的信息,请将图像加载到 QrImageInput 中,将其传递给 QrReader.Read(),然后访问返回的 QrResult 对象上的 Value 属性。 该方法返回一个集合,其中包含图像中每个被识别的二维码对应的结果。

输入

下方的二维码编码了 https://ironsoftware.com,扫描后将提取其值。

用于扫描的二维码编码 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>,即使仅存在一个二维码也是如此。 如果图片包含多个二维码,请使用 foreach (var result in results) 循环处理每个二维码。 在调用 .First() 处理未发现二维码的图片前,请先使用 results.Any() 进行保护。

输出

显示解码后 QR 码值的控制台输出 https://ironsoftware.com

如何读取所有二维码属性?

每个 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
$vbLabelText   $csharpLabel

输出

控制台输出显示从二维码读取的 Value、Url 和 Points 属性

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

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 63,625 | 版本: 2026.4 刚刚发布
Still Scrolling Icon

还在滚动吗?

想快速获得证据? PM > Install-Package IronQR
运行示例 观看您的 URL 变成 QR 代码。