如何在 C# 中读取二维码
在运行时识别任何扫描到的QR码格式。阅读 QrResult.QrType 以检测符号体系,并针对多种输入源构建类型感知处理逻辑。
当应用程序接收来自多个来源的二维码时,其格式往往难以预测。 物流平台可能会同时接收来自运单的标准二维码以及来自产品标签的紧凑型微型二维码。 文档处理系统既可扫描 PDF 文件中嵌入的条码,也可扫描实体介质上印制的条码。 通过读取 QrResult.QrType,应用程序可以了解检测到的格式,从而能够验证输入、将数据路由到正确的处理程序,或记录不支持的格式以供审查。
本指南演示了如何使用 IronQR库从扫描结果中获取 QR 码格式。 尚未扫描过二维码的开发者,请先阅读《从图像中读取二维码》指南。
快速入门:扫描二维码类型
加载一张图片,使用 QrReader 对其进行扫描,并获取检测到的格式。
最小工作流程(5 个步骤)
- 下载 IronQR C# 库以读取 QR 码类型
- 加载图片并将其封装在
QrImageInput中 - 创建一个
QrReader实例,并使用输入调用Read 方法 - 通过
QrResult.QrType获取检测到的格式 - 使用
QrEncoding中的开关根据格式路由处理
如何读取二维码类型?
要读取QR码的类型,请将图像加载到 QrImageInput 中,将其传递给 QrReader.Read(),然后访问返回的 QrResult 中的 QrType。 该属性返回一个 QrEncoding 枚举值,用于标识检测到的符号体系。
输入
下方的二维码编码了 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}")
QrType 返回一个 QrEncoding 枚举值,例如 QrEncoding.MicroQRCode 或 QrEncoding.RMQRCode。 这使得该代码可在 switch 语句中直接使用,无需进行解析或字符串比较。
输出
如何根据二维码格式进行处理路由?
当应用程序从多个来源接收二维码时,并非所有输入都采用相同的格式。 使用 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
每个 case 对应一个特定的 QrEncoding 值。 QrType 分支会捕获任何未被显式处理的格式,因此循环在遇到意外输入时绝不会静默失败。 随着应用程序需要支持更多格式,请补充更多示例。
输出
QrResult.QrType 返回什么?
QrResult.QrType 是每个 QrEncoding enum 上的一个 QrResult 属性,用于标识扫描仪检测到的符号体系。 该内容在 QrReader.Read() 期间会自动填充,无需额外配置。 添加 using IronQr.Enum; 以在 switch 中直接使用 QrEncoding 的值。
| 价值 | 说明 |
|---|---|
QrEncoding.QRCode |
标准二维码,这是各行业最常用的格式 |
QrEncoding.MicroQRCode |
紧凑型版本,专为打印区域有限的小型表面设计 |
QrEncoding.RMQRCode |
针对狭长标签形状优化的矩形微型二维码 (rMQR) |
QrType 为只读内容,反映了扫描仪在图像中检测到的内容。 其价值并不取决于二维码的生成方式。
QrType 的常见使用场景有哪些?
- 物流与运输:检测标签上携带的是标准二维码还是紧凑型微二维码,并将它们分别路由到正确的解析管道。
- 文档处理:在提取数据用于记录匹配之前,验证扫描文档是否符合预期格式。
- 多格式自助终端:单个终端可接收不同格式的二维码,并自动将每个二维码分发至相应的处理程序,无需人工干预。
- 审计与合规:将符号类型与解码值一同记录,以建立跨批次的输入格式可验证记录。
- 质量保证:验证生成的二维码扫描后是否显示为预期类型,确认输出符合规范。
有关检测类型后读取二维码数据的更多信息,请参阅《读取二维码值指南》以及完整的 IronQR 功能集。
常见问题解答
QrResult.QrType在IronQR中的用途是什么?
IronQR中的QrResult.QrType用于检测扫描QR码的符号学,使您的C#应用程序中实现智能的基于格式的处理。
如何使用IronQR读取QR码类型?
要使用IronQR读取QR码类型,可以在QrImageInput上使用QrReader.Read()方法。这将允许您访问`QrResult.QrType`,它提供有关扫描QR码类型的信息。
在C#应用程序中检测QR码类型为什么很重要?
在C#应用程序中检测QR码类型很重要,因为它使开发人员能够基于其格式智能处理QR码,确保适当的处理和数据提取。
IronQR能够处理不同的QR码符号学吗?
是的,IronQR可以通过使用`QrResult.QrType`来准确识别和处理不同类型的QR码,从而具有处理不同QR码符号学的能力。
QrReader.Read()在扫描QR码中的作用是什么?
QrReader.Read()是IronQR中的一个方法,用于从`QrImageInput`扫描QR码,使您能够检索QR码类型和其他相关信息以进行进一步处理。
IronQR可以处理多种QR码类型吗?
是的,IronQR允许您通过使用`QrResult.QrType`属性识别和处理每种QR码,来处理多种QR码类型。
使用IronQR读取C#中的QR码有什么好处?
在C#中使用IronQR读取QR码的好处包括准确检测QR码类型、智能的基于格式处理以及与C#应用程序的无缝集成。
IronQR如何改善QR码处理效率?
IronQR通过提供工具如QrReader.Read()和QrResult.QrType来提高QR码处理效率,这些工具简化了在C#项目中不同QR码类型的识别和处理。

