
如何在 C# 中从图像读取 QR
QR码(快速响应码)无处不在——产品包装、活动门票、菜单,甚至名片上。 作为.NET开发人员,能够快速可靠地从图像中读取QR码可以开启强大的自动化和用户交互功能之门。 在本指南中,我们将指导您如何使用IronQR,一个专为.NET构建的高性能QR码库,用几行C#代码从图像中读取QR码。
无论您是在构建库存管理软件、集成双因素认证,还是只是从截图中解码URL,IronQR都让这一切变得简单。
什么是IronQR?
IronQR是一个强大的C# QR码库,专为.NET开发人员创建了一个强大的QR码读取器和写入器。 IronQR既支持QR码的生成也支持扫描,支持从多种图像格式中读取,使其非常适合用于桌面、网络或服务器应用。 通过这个库,您可以创建准确的QR码读取工具,以自动化QR码识别和读取的整个过程。
主要功能
- 轻松读取和生成QR码。
- 支持JPEG、PNG、BMP及其他图像格式。
- 高速性能和精确检测,轻松提取QR码数据。
- 支持.NET Framework、.NET Core、.NET Standard(2.0+)和.NET 6/7+项目。
- 提供跨平台支持,您可以在您喜欢的应用环境和操作系统中工作,无论是Windows、Linux或任何其他支持的环境。
与开源替代品不同,IronQR专注于企业级稳定性,商业授权和专业支持,非常适合关键业务应用。
在您的C#项目中设置IronQR
在您开始扫描QR码之前,让我们来看看如何在您的.NET应用中设置IronQR。
通过 NuGet 安装
您可以直接从NuGet包管理控制台安装IronQR:
或者,使用Visual Studio中的NuGet GUI,搜索IronQR并点击"安装":

添加命名空间和基本设置
安装后,在您的C#文件中包含以下命名空间:
using IronSoftware.Drawing;
using IronQR;Imports IronSoftware.Drawing
Imports IronQR注意: IronSoftware.Drawing用于以跨平台方式处理图像格式。
从图像中读取QR码
让我们深入了解如何实际从文件中读取QR码,使用IronQR。
支持的图像格式
IronQR支持多种图像格式,包括:
- PNG
- JPG/JPEG
- BMP
- GIF
- TIFF
这种灵活性允许您使用几乎任何图像来源,从相机快照到扫描文档。
基本代码示例
让我们仔细看看如何使用这个库轻松解码QR码。 这是一个最小的示例,从图像文件中读取具有文本值"Hello World!"的单个QR码,使用Bitmap类和文件流加载图像:
using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image using a file stream
using var stream = File.OpenRead("sample-qr.png");
var bitmapImage = AnyBitmap.FromStream(stream);
QrImageInput qrImageInput = new QrImageInput(bitmapImage );
// Read the QR code
QrReader qrReader = new QrReader();
try
{
// Use the QR read method to read the values within your QR code(s)
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}
}
}Imports IronQr
Imports IronSoftware.Drawing
Imports System
Friend Class Program
Shared Sub Main()
' Load the image using a file stream
Dim stream = File.OpenRead("sample-qr.png")
Dim bitmapImage = AnyBitmap.FromStream(stream)
Dim qrImageInput As New QrImageInput(bitmapImage)
' Read the QR code
Dim qrReader As New QrReader()
Try
' Use the QR read method to read the values within your QR code(s)
Dim results As IEnumerable(Of QrResult) = qrReader.Read(qrImageInput)
' Output the decoded value
For Each result In results
Console.WriteLine("QR Code Value: " & result.Value)
Next result
Catch ex As Exception
Console.WriteLine("Error reading QR code: " & ex.Message)
End Try
End Sub
End Class控制台输出

此代码加载QR码图像,读取第一个检测到的QR码,并打印解码内容。 简单而有效。从这里,您可以保存QR码的值以供进一步使用。
处理多个QR码
如果您的图像包含多个QR码(例如,一张产品标签表),您可以提取所有这些。 在这个例子中,我们将通过程序运行以下QR码图像:

using IronQr;
using IronSoftware.Drawing;
using System;
class Program
{
static void Main()
{
// Load the image from file
var inputImage = AnyBitmap.FromFile("SampleCodes.png");
QrImageInput qrImageInput = new QrImageInput(inputImage);
// Read the QR code
QrReader qrReader = new QrReader();
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
}Imports IronQr
Imports IronSoftware.Drawing
Imports System
Friend Class Program
Shared Sub Main()
' Load the image from file
Dim inputImage = AnyBitmap.FromFile("SampleCodes.png")
Dim qrImageInput As New QrImageInput(inputImage)
' Read the QR code
Dim qrReader As New QrReader()
Dim results As IEnumerable(Of QrResult) = qrReader.Read(qrImageInput)
' Output the decoded value
For Each result In results
Console.WriteLine("QR Code Value: " & result.Value)
Next result
End Sub
End Class输出

QR码扫描的常见用例
以下是几个从图像中读取QR码显得有价值的实际场景:
- **库存与资产管理:**通过扫描包装图片中的QR码,自动识别物品。
- **双因素认证 (2FA):**解析 2FA 设置界面中的 QR 码,以协助安全配置。
- **移动应用集成:**通过扫描共享的 QR 截图,打开移动端 URL 或应用专属的深度链接。
- **活动票务:**验证通过电子邮件发送或显示在屏幕上的QR码。
- **支付网关:**为金融科技应用提取嵌入QR码中的支付数据。
在所有这些用例中,快速准确的识别是关键,这正是IronQR所能轻松处理的。
故障排除提示
如果您在读取QR码时遇到问题,请考虑以下几点:
图像质量差
模糊或低分辨率的图像可能会导致难以检测到QR码。 尽可能使用高质量的输入。
QR码未检测到
确保图像不是太暗,有强烈的对比度,并且QR码没有被遮挡。 尝试裁剪图像以聚焦QR区域。
异常处理
始终将您的QR读取逻辑包装在try-catch块中,以优雅地处理损坏的文件或意外的格式:
try
{
IEnumerable<QrResult> results = qrReader.Read(qrImageInput);
// Output the decoded value
foreach (var result in results)
{
Console.WriteLine("QR Code Value: " + result.Value);
}
}
catch (Exception ex)
{
Console.WriteLine("Error reading QR code: " + ex.Message);
}Try
Dim results As IEnumerable(Of QrResult) = qrReader.Read(qrImageInput)
' Output the decoded value
For Each result In results
Console.WriteLine("QR Code Value: " & result.Value)
Next result
Catch ex As Exception
Console.WriteLine("Error reading QR code: " & ex.Message)
End Try最后的想法
在C#中从图像读取QR码不必是困难的。 使用IronQR,您可以用几行代码将QR码扫描集成到您的.NET应用中。 其简单性、跨平台兼容性和出色的性能使其成为开发现代QR启用系统的首选工具。
如果您希望扩展此功能,请考虑探索:
- QR码生成 用于创建您自己的可扫描代码。
- 集成 IronBarcode 或 IronOCR 以获得更广泛的扫描功能。 准备好开始了吗?
安装IronQR免费试用版 立即开始使用,了解这个库如何提升您的QR码任务。

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


