如何在 C# 中读取二维码值
即时从任何二维码图像中提取解码后的文本。 获取原始字符串值,以便在您的应用程序中进行显示、存储或处理。
读取二维码的值是任何扫描工作流的第一步。 支付终端需要将交易 ID 嵌入到二维码中。 仓库系统需要在标签上标注产品编号。 票务验证器需要活动通行证上印有的预订代码。 IronQR使这一过程变得简单:加载图像,将其传递给QrReader,然后直接从结果中读取解码后的字符串。
本指南演示如何使用IronQR库从图像中提取QR码值。 还未生成QR码的开发者应首先查阅创建QR码作为图像指南。
快速入门:读取二维码值加载图像,用QrReader扫描,并提取解码后的字符串。
-
1使用 IronQR 安装 NuGet Package Manager
-
2复制并运行这段代码。
var input = new QrImageInput(AnyBitmap.FromFile("qr-code.png")); var results = new QrReader().Read(input); Console.WriteLine(results.First().Value);Dim input = New QrImageInput(AnyBitmap.FromFile("qr-code.png")) Dim results = New QrReader().Read(input) Console.WriteLine(results.First().Value)Dim input = New QrImageInput(AnyBitmap.FromFile("qr-code.png")) Dim results = New QrReader().Read(input) Console.WriteLine(results.First().Value) -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronQR
最小工作流程(5 个步骤)
- 下载IronQR C#库以读取QR码值
- 加载图片并将其封装在
QrImageInput中 - 创建一个
QrReader实例,并使用输入调用Read方法 - 通过
QrResult.Value访问解码后的字符串 - 在调用
.First()之前,请使用results.Any()进行检查
如何从图像中读取二维码的值?
要提取嵌入在QR码中的值,请将图像加载到QrImageInput,传递给QrReader.Read(),并访问返回的QrResult上的Value属性。 该方法返回一个集合,其中包含图像中每个被识别的二维码对应的结果。
输入
下面的QR码编码了**https://ironsoftware.com**,将被扫描以提取其值。

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}");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}")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暴露三个属性,它们共同提供了扫描到的内容和在图像中发现的位置的全貌。 使用上文中的同一张输入二维码:
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})");
}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 iImports 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 或哈希值。
- 用户身份验证:从二维码中解码一次性令牌,以完成双因素登录步骤。
有关更多QR码读取模式,请参阅从图像读取QR码指南和完整的IronQR功能集。
常见问题
如何在C#中使用IronQR读取QR码值?
要使用IronQR读取QR码值,需将图像加载到一个QrImageInput中,将其传递给QrReader实例,然后调用Read()方法。随后可以使用QrResult.Value属性访问解码后的字符串。
QrResult中的Value属性有什么用途?
QrResult中的Value属性保存了QR码中编码的原始解码字符串。它可以是一个URL、文本或任何其他数据格式。
IronQR支持从一张图像中读取多个QR码吗?
是的,IronQR可以从单个图像中读取多个QR码。Read()方法返回一个IEnumerable
如何使用IronQR从QR码中提取URL?
IronQR允许您使用QrResult.Url属性提取URL,当Value是有效URL时,该属性会被填充。这有助于快速访问或验证URL。
QrResult中的Points属性代表什么?
QrResult中的Points属性包含QR码在图像中的角点坐标。这有助于定位并可视化QR码的位置。
读取QR码值的常见用例有哪些?
常见用例包括支付处理、票证验证、库存管理、文件验证和通过提取和使用QR码中编码的数据进行用户身份验证。
如何使用IronQR处理无QR码的图像?
在调用.First()或处理结果之前,用results.Any()来保护读取过程,以处理图像中未找到任何QR码的情况。
IronQR能处理QR码中编码的不同类型数据吗?
是的,IronQR可以解码如URL、数值ID、文本和JSON等多种数据类型,使其在不同应用中具备多功能性。
是否可以使用IronQR从实时摄像头输入读取QR码?
虽然指南专注于从图像读取,但IronQR可以集成到摄像系统中。您可以将帧捕获为图像并使用类似的方法处理它们。
如果我还未生成QR码,如何开始使用IronQR?
如果您还未生成QR码,可以先参考《创建QR码作为图像》指南,生成QR码后再尝试读取。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。