如何在 C# 中读取二维码
在运行时识别任何扫描QR码的格式。通过读取QrResult.QrType来检测标志并构建类型感知的处理逻辑以适应不同的输入源。
当应用程序接收来自多个来源的二维码时,其格式往往难以预测。 物流平台可能会同时接收来自运单的标准二维码以及来自产品标签的紧凑型微型二维码。 文档处理系统既可扫描 PDF 文件中嵌入的条码,也可扫描实体介质上印制的条码。 通过读取QrResult.QrType,应用程序可以识别所检测的格式,使得验证输入、将数据路由到正确的处理程序或记录不支持的格式以便审查成为可能。
本指南演示了如何使用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().QrType);Dim input = New QrImageInput(AnyBitmap.FromFile("qr-code.png")) Dim results = (New QrReader()).Read(input) Console.WriteLine(results.First().QrType)Dim input = New QrImageInput(AnyBitmap.FromFile("qr-code.png")) Dim results = (New QrReader()).Read(input) Console.WriteLine(results.First().QrType) -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronQR
最小工作流程(5 个步骤)
- 下载IronQR C#库以读取QR码类型
- 加载图片并将其封装在
QrImageInput中 - 创建一个
QrReader实例,并使用输入调用Read方法 - 通过
QrResult.QrType获取检测到的格式 - 对
QrEncoding使用switch根据格式路由处理
如何读取二维码类型?
要读取二维码的类型,将图像加载到QrImageInput,传递给QrReader.Read(),并访问返回的QrResult上的QrType。 该属性返回一个QrEncoding枚举值,用于识别检测到的标志。
输入
下面的QR码编码**https://ironsoftware.com**,将被扫描以提取其类型。

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}");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}")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.QRCode, QrEncoding.MicroQRCode或QrEncoding.RMQRCode。 这使得可以在switch语句中直接安全地使用,而无需解析或字符串比较。
输出

如何根据二维码格式进行处理路由?
当应用程序从多个来源接收二维码时,并非所有输入都采用相同的格式。 在QrResult.QrType上使用switch,根据其QrEncoding值将每个检测到的代码路由到正确的处理程序。 这使得特定格式的逻辑得以隔离,并使添加新的格式分支变得简单直接。
使用上文中的同一张输入二维码:
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;
}
}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
NextImports 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是每个QrResult上的QrEncoding enum属性,用于标识扫描仪检测到的符号。 它在QrReader.Read()期间自动填充,不需要额外配置。 添加using IronQr.Enum;以直接在switch中使用QrEncoding值。
| 价值 | 说明 |
|---|---|
QrEncoding.QRCode | 标准二维码,这是各行业最常用的格式 |
QrEncoding.MicroQRCode | 紧凑型版本,专为打印区域有限的小型表面设计 |
QrEncoding.RMQRCode | 针对狭长标签形状优化的矩形微型二维码 (rMQR) |
QrType是只读的,反映了扫描器在图像中检测到的内容。 其价值并不取决于二维码的生成方式。
QrType的常见使用案例是什么?
- 物流与运输:检测标签上携带的是标准二维码还是紧凑型微二维码,并将它们分别路由到正确的解析管道。
- 文档处理:在提取数据用于记录匹配之前,验证扫描文档是否符合预期格式。
- 多格式自助终端:单个终端可接收不同格式的二维码,并自动将每个二维码分发至相应的处理程序,无需人工干预。
- 审计与合规:将符号类型与解码值一同记录,以建立跨批次的输入格式可验证记录。
- 质量保证:验证生成的二维码扫描后是否显示为预期类型,确认输出符合规范。
有关在检测类型后读取QR码数据的更多信息,请参见读取QR码值指南和完整的IronQR功能集。
常见问题
如何在C#中读取QR码的类型?
您可以使用IronQR库在C#中读取QR码的类型。将图像加载到QrImageInput中,传递给QrReader.Read(),然后访问返回的QrResult中的QrType以识别QR码格式。
QrResult.QrType返回什么?
QrResult.QrType返回一个QrEncoding枚举值,用于识别检测到的符号,如QRCode、MicroQRCode或RMQRCode。在IronQR的扫描过程中,此属性会自动填充。
如何根据QR码格式进行处理路由?
要根据QR码格式进行处理路由,请在QrResult.QrType上使用switch语句。这允许您处理特定的QrEncoding值,如QRCode、MicroQRCode或RMQRCode,将每个值引导到正确的处理逻辑。
检测QR码类型的常见用例有哪些?
常见用例包括物流和运输以正确路由QR类型,文档处理以验证预期格式,多格式亭以处理各种输入,以及审计/合规以记录符号类型。
QrResult.QrType需要任何额外的配置吗?
不需要额外的配置。使用IronQR的QrReader.Read()时,QrResult.QrType会自动填充,直接访问检测到的QrEncoding值。
如何验证QR码是否扫描为预期类型?
使用IronQR扫描QR码,并检查QrResult.QrType值以确认其与预期的QrEncoding类型匹配,确保生成的QR码正确。
我可以用IronQR处理意外的QR码格式吗?
是的,您可以通过在QrResult.QrType上的switch语句中使用默认情况来处理意外格式。这确保了任何未处理的格式都会被捕获并记录以供审查。
不同的QrEncoding值表示什么?
不同的QrEncoding值指定检测到的QR码中的符号类型。例如,QRCode是标准格式,MicroQRCode适合紧凑表面,RMQRCode则针对细长标签进行了优化。
QrResult.QrType如何帮助QR码验证?
QrResult.QrType通过确认扫描的QR码与预期格式匹配在处理数据之前进行QR码验证,以确保数据的完整性和兼容性。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。