如何在 C# 中读取二维码

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

在运行时识别任何扫描到的二维码格式。阅读 QrResult.QrType 以检测符号体系,并针对多种输入源构建类型感知处理逻辑。

当应用程序接收来自多个来源的二维码时,其格式往往难以预测。 物流平台可能会同时接收来自运单的标准二维码以及来自产品标签的紧凑型微型二维码。 文档处理系统既可扫描 PDF 文件中嵌入的条码,也可扫描实体介质上印制的条码。 通过读取 QrResult.QrType,应用程序可以了解检测到的格式,从而能够验证输入、将数据路由到正确的处理程序,或记录不支持的格式以便后续审查。

本指南演示了如何使用 IronQR库从扫描结果中获取 QR 码格式。 尚未扫描过二维码的开发者,请先阅读《从图像中读取二维码》指南。

快速入门:读取二维码类型

加载一张图片,使用 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().QrType);
  3. 部署到您的生产环境中进行测试

    通过免费试用立即在您的项目中开始使用IronQR

    arrow pointer

如何读取二维码类型?

要读取二维码的类型,请将图像加载到 QrImageInput 中,将其传递给 QrReader.Read(),然后在返回的 QrResult 上访问 QrType。 该属性返回一个 QrEncoding 枚举值,用于标识检测到的符号体系。

输入

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

用于类型检测的二维码编码 https://ironsoftware.com
:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-type.cs
using IronQr;
using System.Drawing;
using System.Linq;

// Import an image containing a QR code
var inputImage = Image.FromFile("sample.jpg");

// Load the asset into a QrImageInput object
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 type of the first QR code found
Console.WriteLine($"The QR type is {results.First().QrType}");
Imports IronQr
Imports System.Drawing
Imports System.Linq

' Import an image containing a QR code
Dim inputImage As Image = Image.FromFile("sample.jpg")

' Load the asset into a QrImageInput object
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 type of the first QR code found
Console.WriteLine($"The QR type is {results.First().QrType}")
$vbLabelText   $csharpLabel

QrType 返回一个 QrEncoding 枚举值,例如 QrEncoding.MicroQRCodeQrEncoding.RMQRCode。 这使得该代码可在 switch 语句中直接使用,无需进行解析或字符串比较。

输出

显示检测到的 QR 码类型的控制台输出

如何根据二维码格式进行处理路由?

当应用程序从多个来源接收二维码时,并非所有输入都采用相同的格式。 在 switch 处使用 QrResult.QrType,根据其 QrEncoding 值将每个检测到的代码路由到正确的处理程序。 这使得特定格式的逻辑得以隔离,并使添加新的格式分支变得简单直接。

使用上文中的同一张输入二维码:

:path=/static-assets/qr/content-code-examples/how-to/read-qr-code-type-all.cs
using IronQr;
using IronQr.Enum;
using System.Drawing;

// Import an image containing QR codes
var inputImage = Image.FromFile("sample.jpg");

// Load the asset into a QrImageInput object
QrImageInput imageInput = new QrImageInput(inputImage);

// Create a QR Reader object
QrReader reader = new QrReader();

// Read all embedded QR codes from the image
IEnumerable<QrResult> results = reader.Read(imageInput);

// Route processing based on the detected QR code format
foreach (QrResult result in results)
{
    switch (result.QrType)
    {
        case QrEncoding.QRCode:
            Console.WriteLine($"Standard QR Code: {result.Value}");
            break;
        case QrEncoding.MicroQRCode:
            Console.WriteLine($"Micro QR Code: {result.Value}");
            break;
        case QrEncoding.RMQRCode:
            Console.WriteLine($"RMQR Code: {result.Value}");
            break;
        default:
            Console.WriteLine($"Other format ({result.QrType}): {result.Value}");
            break;
    }
}
Imports IronQr
Imports IronQr.Enum
Imports System.Drawing

' Import an image containing QR codes
Dim inputImage As Image = Image.FromFile("sample.jpg")

' Load the asset into a QrImageInput object
Dim imageInput As New QrImageInput(inputImage)

' Create a QR Reader object
Dim reader As New QrReader()

' Read all embedded QR codes from the image
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)

' Route processing based on the detected QR code format
For Each result As QrResult In results
    Select Case result.QrType
        Case QrEncoding.QRCode
            Console.WriteLine($"Standard QR Code: {result.Value}")
        Case QrEncoding.MicroQRCode
            Console.WriteLine($"Micro QR Code: {result.Value}")
        Case QrEncoding.RMQRCode
            Console.WriteLine($"RMQR Code: {result.Value}")
        Case Else
            Console.WriteLine($"Other format ({result.QrType}): {result.Value}")
    End Select
Next
$vbLabelText   $csharpLabel

每个 case 对应一个特定的 QrEncoding 值。 default 分支会捕获任何未被显式处理的格式,因此循环在遇到意外输入时绝不会静默失败。 随着应用程序需要支持更多格式,请补充更多示例。

输出

控制台输出显示每个检测到的二维码的基于格式的路由结果

QrResult.QrType 返回什么?

QrType 是每个 QrResult 上的 QrEncoding 枚举属性,用于标识扫描仪检测到的条码符号。 该内容会在 QrReader.Read() 期间自动填充,无需额外配置。 添加 using IronQr.Enum; 以在 QrEncoding 中直接使用 switch 的值。

价值 说明
QrEncoding.QRCode 标准二维码,这是各行业最常用的格式
QrEncoding.MicroQRCode 紧凑型版本,专为打印区域有限的小型表面设计
QrEncoding.RMQRCode 针对狭长标签形状优化的矩形微型二维码 (rMQR)

QrType 为只读内容,反映了扫描仪在图像中检测到的内容。 其价值并不取决于二维码的生成方式。


QrType 的常见应用场景有哪些?

  • 物流与运输:检测标签上携带的是标准二维码还是紧凑型微二维码,并将它们分别路由到正确的解析管道。
  • 文档处理:在提取数据用于记录匹配之前,验证扫描文档是否符合预期格式。
  • 多格式自助终端:单个终端可接收不同格式的二维码,并自动将每个二维码分发至相应的处理程序,无需人工干预。
  • 审计与合规:将符号类型与解码值一同记录,以建立跨批次的输入格式可验证记录。
  • 质量保证:验证生成的二维码扫描后是否显示为预期类型,确认输出符合规范。

有关检测类型后读取二维码数据的更多信息,请参阅《读取二维码值指南》以及完整的 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 代码。