如何在 C# 中从图像读取二维码
在 C# 中使用 IronQR,可先用 IronDrawing 加载各种格式的图像,再创建 QrImageInput 对象,最后调用 QrReader.Read 方法高效解码二维码中的数据。
如何从图片中读取二维码
- 下载C#库以从图像中读取二维码
- 使用
IronDrawing导入图像数据 - 从图像数据创建
QrImageInput对象 - 将对象传递给
Read方法 - 遍历每个检测到的 QR 码并查看其信息
如何从不同的图像格式中读取二维码?
IronQR 支持从多种图像格式中读取二维码,并使用先进的机器学习模型对不同来源的图像进行准确解码。支持的格式包括:
- 联合图像专家组(JPEG)
- 便携式网络图形(PNG)
- 图形交换格式 (GIF)
- 标记图像文件格式(TIFF)
- 位图图像文件(BMP)
- WBMP
- WebP
- 图标 (ico)
- WMF
RawFormat(原始数据)
开源库 IronDrawing 提供这些格式的支持,并负责高效处理图像。来自数码相机、扫描仪、移动设备或网络下载的二维码图像都可以直接处理,无需转换格式。

-
1使用 IronQR 安装 NuGet Package Manager
-
2复制并运行这段代码。
// Import necessary IronQR and IronDrawing namespaces using IronSoftware.Drawing; using IronQr; public class QRCodeReader { public static void Main() { // Load an image from a file path using (var inputImage = Image.FromFile("path/to/your/image/file.webp")) { // Create a QrImageInput object from the image var qrImageInput = new QrImageInput(inputImage); // Create a QR Reader and decode the QR code from the image var reader = new QrReader(); var results = reader.Read(qrImageInput); // Iterate through each detected QR code and display its information foreach (var qrResult in results) { Console.WriteLine($"QR Code Data: {qrResult.Value}"); } } } }' Import necessary IronQR and IronDrawing namespaces Imports IronSoftware.Drawing Imports IronQr Public Class QRCodeReader Public Shared Sub Main() ' Load an image from a file path Using inputImage = Image.FromFile("path/to/your/image/file.webp") ' Create a QrImageInput object from the image Dim qrImageInput = New QrImageInput(inputImage) ' Create a QR Reader and decode the QR code from the image Dim reader = New QrReader() Dim results = reader.Read(qrImageInput) ' Iterate through each detected QR code and display its information For Each qrResult In results Console.WriteLine($"QR Code Data: {qrResult.Value}") Next End Using End Sub End Class' Import necessary IronQR and IronDrawing namespaces Imports IronSoftware.Drawing Imports IronQr Public Class QRCodeReader Public Shared Sub Main() ' Load an image from a file path Using inputImage = Image.FromFile("path/to/your/image/file.webp") ' Create a QrImageInput object from the image Dim qrImageInput = New QrImageInput(inputImage) ' Create a QR Reader and decode the QR code from the image Dim reader = New QrReader() Dim results = reader.Read(qrImageInput) ' Iterate through each detected QR code and display its information For Each qrResult In results Console.WriteLine($"QR Code Data: {qrResult.Value}") Next End Using End Sub End Class -
3部署到您的生产环境中进行测试
今天就用免费试用在您的项目中开始使用IronQR
注意:请将“path/to/your/image/file.webp”替换为您的二维码图像文件的实际路径。
想知道示例图片中的二维码值吗?不妨试试这段代码!为什么IronQR支持多种图像格式?
读取二维码是指扫描二维码并解码其中的信息。通常使用相机或扫描仪采集图像,再用软件解码。二维码可以存储文本、URL、联系方式或其他数据。
从营销资料和产品包装到电子文档和网页,二维码会出现在多种场景中。IronQR 支持多种图像格式,帮助开发人员避免格式兼容性问题并构建可靠的应用。详细了解 IronQR 的读取功能,可将这些能力用到开发流程中。
我应该在什么时候使用每种图像格式?
在二维码处理过程中,不同的图像格式有不同的用途:
- PNG:适合需要透明背景或对图像质量要求较高的场景。PNG 采用无损压缩,可让二维码图案保持清晰可读。
- JPEG:适合包含二维码的照片,或需要控制文件大小的场景。请将质量设为 80% 或更高,避免压缩伪影影响识读。
- TIFF:适用于档案保存或在企业环境中处理扫描文档。
- WebP:现代格式提供卓越的压缩效果且保留质量,适用于网络应用。
无论使用哪种格式,都应确保图像具有足够的分辨率和对比度。印刷二维码的分辨率应至少为 300 DPI。有关各图像格式的优化技巧,请参阅高级二维码读取示例。
如果图像质量差会怎样?
IronQR结合了容错功能以应对不完善的图像。 在处理质量较差的图片时,库采用了多种策略:
- 纠错:二维码支持 L、M、Q 和 H 四种纠错级别,即使损坏比例高达 30%,也可恢复数据。
- 机器学习增强:IronQR的ML模型检测并补偿模糊、失真和光照不良等常见问题。
- 预处理:自动图像增强在解码之前提高对比度和清晰度。
遇到难以识读的图像时,可以使用自定义二维码读取模式进一步调整读取过程:
// Example: Reading QR codes from a poor quality image
using IronQr;
public class EnhancedQRReader
{
public static void ReadPoorQualityImage()
{
using (var inputImage = Image.FromFile("blurry_qr_code.jpg"))
{
// Create a QrImageInput and read with a QR Reader
var qrImageInput = new QrImageInput(inputImage);
var reader = new QrReader();
var results = reader.Read(qrImageInput);
foreach (var result in results)
{
Console.WriteLine($"Decoded: {result.Value}");
}
}
}
}// Example: Reading QR codes from a poor quality image
using IronQr;
public class EnhancedQRReader
{
public static void ReadPoorQualityImage()
{
using (var inputImage = Image.FromFile("blurry_qr_code.jpg"))
{
// Create a QrImageInput and read with a QR Reader
var qrImageInput = new QrImageInput(inputImage);
var reader = new QrReader();
var results = reader.Read(qrImageInput);
foreach (var result in results)
{
Console.WriteLine($"Decoded: {result.Value}");
}
}
}
}Imports IronQr
Public Class EnhancedQRReader
Public Shared Sub ReadPoorQualityImage()
Using inputImage = Image.FromFile("blurry_qr_code.jpg")
' Create a QrImageInput and read with a QR Reader
Dim qrImageInput = New QrImageInput(inputImage)
Dim reader = New QrReader()
Dim results = reader.Read(qrImageInput)
For Each result In results
Console.WriteLine($"Decoded: {result.Value}")
Next
End Using
End Sub
End ClassImports IronQr
Public Class EnhancedQRReader
Public Shared Sub ReadPoorQualityImage()
Using inputImage = Image.FromFile("blurry_qr_code.jpg")
' Create a QrImageInput and read with a QR Reader
Dim qrImageInput = New QrImageInput(inputImage)
Dim reader = New QrReader()
Dim results = reader.Read(qrImageInput)
For Each result In results
Console.WriteLine($"Decoded: {result.Value}")
Next
End Using
End Sub
End Class从二维码中获取值
大多数 IronQR 函数返回一个集合,以便返回多个检测结果。results 是一个对象序列,其本身没有 Value 属性。示例从集合中取出第一个 QrResult,并读取其 Value。
using System;
using System.Collections.Generic;
using System.Linq;
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 an 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 System;
using System.Collections.Generic;
using System.Linq;
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 an 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 System
Imports System.Collections.Generic
Imports System.Linq
Imports IronQr
Imports System.Drawing
' 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 System
Imports System.Collections.Generic
Imports System.Linq
Imports IronQr
Imports System.Drawing
' 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}")检测图像中的二维码位置
IronQR 不仅限于简单的解码,还能精确定位二维码在图像中的位置。 此定位使用标准坐标系,其中 PointF (0,0) 代表图像的左上角。 QR 码角落的精确空间坐标可通过 Points[] 数组访问。
示例会获取检测到的二维码的四个角点坐标,并将它们输出到控制台。
using System;
using System.Collections.Generic;
using IronQr;
using System.Drawing;
using System.Linq;
// Import an image containing a QR code
var inputImage = Image.FromFile("urlQr.png");
// Load the asset into a QrImageInput object
var imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
var reader = new QrReader();
// Read the input and get all embedded QR codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// [TL, TR, BL, BR]
string[] labels = { "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right" };
var points = results.First().Points;
for (int i = 0; i < points.Length; i++)
{
Console.WriteLine($"{labels[i]}: {points[i].X}, {points[i].Y}");
}using System;
using System.Collections.Generic;
using IronQr;
using System.Drawing;
using System.Linq;
// Import an image containing a QR code
var inputImage = Image.FromFile("urlQr.png");
// Load the asset into a QrImageInput object
var imageInput = new QrImageInput(inputImage);
// Create a QR Reader object
var reader = new QrReader();
// Read the input and get all embedded QR codes
IEnumerable<QrResult> results = reader.Read(imageInput);
// [TL, TR, BL, BR]
string[] labels = { "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right" };
var points = results.First().Points;
for (int i = 0; i < points.Length; i++)
{
Console.WriteLine($"{labels[i]}: {points[i].X}, {points[i].Y}");
}Imports System
Imports System.Collections.Generic
Imports IronQr
Imports System.Drawing
Imports System.Linq
' Import an image containing a QR code
Dim inputImage As Image = Image.FromFile("urlQr.png")
' 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)
' [TL, TR, BL, BR]
Dim labels As String() = { "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right" }
Dim points = results.First().Points
For i As Integer = 0 To points.Length - 1
Console.WriteLine($"{labels(i)}: {points(i).X}, {points(i).Y}")
NextImports System
Imports System.Collections.Generic
Imports IronQr
Imports System.Drawing
Imports System.Linq
' Import an image containing a QR code
Dim inputImage As Image = Image.FromFile("urlQr.png")
' 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)
' [TL, TR, BL, BR]
Dim labels As String() = { "Top-Left", "Top-Right", "Bottom-Left", "Bottom-Right" }
Dim points = results.First().Points
For i As Integer = 0 To points.Length - 1
Console.WriteLine($"{labels(i)}: {points(i).X}, {points(i).Y}")
Next输入二维码

输出
请注意,系统已将每个二维码角点的精确空间坐标记录到控制台。

支持的二维码类型
IronQR 支持生成和读取多种二维码,可满足不同应用的需求。有关详细信息,请参阅支持的二维码格式文档。以下是支持的类型:
QRCode:目前最常用的标准二维码,最多可存储 7,089 个数字字符或 4,296 个字母数字字符,适合存储网站 URL、联系方式等信息。

MicroQRCode:为有限空间设计的微型二维码,最多可存储 35 个数字字符或 21 个字母数字字符,适合小型包装或小尺寸的印刷标签。

RMQRCode:矩形微型二维码(RMQR 码)采用紧凑的矩形布局,可灵活选择长宽比,适用于可用空间呈矩形的场景。

我如何选择合适的QR码类型?
选择合适的 QR 代码类型取决于您的具体使用情况和限制条件:
- 标准二维码:适用于空间充足且需要最大数据容量的通用场景,例如存储 URL、WiFi 凭据、vCard 联系人信息或详细的产品信息。实现细节请参阅二维码生成示例。
- 微型二维码:适用于电子元件、珠宝标签或医疗设备等表面面积较小的物品。虽然容量有限,但适合存储序列号、简单的 URL 或基本跟踪代码。
- RMQR 码:当空间尺寸受限时,可选用矩形二维码,例如圆柱形产品上的窄标签或包装边缘的狭长区域。
数据存储限制是什么?
了解数据容量有助于优化 QR 代码的实施:
| 二维码类型 | 仅限数字 | 字母数字 | 二进制 | 日文汉字 |
|---|---|---|---|---|
| 标准 QR | 7,089 | 4,296 | 2,953 | 1,817 |
| 微型 QR | 35 | 21 | 15 | 9 |
| RMQR | 因版本而异 | 因版本而异 | 因版本而异 | 因版本而异 |
在规划数据存储时要考虑这些因素:
- 使用短网址服务缩短网页链接,节省存储空间
- 对大型数据集进行数据压缩
- 选择适当的纠错级别(纠错级别越高,容量越小)
如需在外观和数据容量之间取得平衡,请参阅生成自定义样式的二维码指南。
什么时候应该使用微型或RMQR码?
微型二维码和 RMQR 码分别适用于以下场景:
微型二维码非常适合:
- 需要跟踪元件的电子电路板
- 需要患者或药物标识符的小型医疗设备
- 雕刻空间有限的珠宝鉴定
- 制造业中的微型产品标签
RMQR 码适合以下场景:
- 管子或管道上的窄幅运输标签
- 笔杆或工具上的狭长区域
- 横幅式营销材料
- 融入现有的矩形设计元素
以下示例演示如何读取不同类型的二维码:
using IronQr;
using IronSoftware.Drawing;
public class MultiTypeQRReader
{
public static void ReadVariousQRTypes()
{
string[] imagePaths = {
"standard_qr.png",
"micro_qr.png",
"rectangular_qr.png"
};
// Create a QR Reader that handles all QR code types
var reader = new QrReader();
foreach (var path in imagePaths)
{
using (var image = Image.FromFile(path))
{
var qrInput = new QrImageInput(image);
var results = reader.Read(qrInput);
foreach (var qr in results)
{
Console.WriteLine($"Type: {qr.QrType}");
Console.WriteLine($"Data: {qr.Value}");
Console.WriteLine("---");
}
}
}
}
}using IronQr;
using IronSoftware.Drawing;
public class MultiTypeQRReader
{
public static void ReadVariousQRTypes()
{
string[] imagePaths = {
"standard_qr.png",
"micro_qr.png",
"rectangular_qr.png"
};
// Create a QR Reader that handles all QR code types
var reader = new QrReader();
foreach (var path in imagePaths)
{
using (var image = Image.FromFile(path))
{
var qrInput = new QrImageInput(image);
var results = reader.Read(qrInput);
foreach (var qr in results)
{
Console.WriteLine($"Type: {qr.QrType}");
Console.WriteLine($"Data: {qr.Value}");
Console.WriteLine("---");
}
}
}
}
}Imports IronQr
Imports IronSoftware.Drawing
Public Class MultiTypeQRReader
Public Shared Sub ReadVariousQRTypes()
Dim imagePaths As String() = {
"standard_qr.png",
"micro_qr.png",
"rectangular_qr.png"
}
' Create a QR Reader that handles all QR code types
Dim reader = New QrReader()
For Each path In imagePaths
Using image = Image.FromFile(path)
Dim qrInput = New QrImageInput(image)
Dim results = reader.Read(qrInput)
For Each qr In results
Console.WriteLine($"Type: {qr.QrType}")
Console.WriteLine($"Data: {qr.Value}")
Console.WriteLine("---")
Next
End Using
Next
End Sub
End ClassImports IronQr
Imports IronSoftware.Drawing
Public Class MultiTypeQRReader
Public Shared Sub ReadVariousQRTypes()
Dim imagePaths As String() = {
"standard_qr.png",
"micro_qr.png",
"rectangular_qr.png"
}
' Create a QR Reader that handles all QR code types
Dim reader = New QrReader()
For Each path In imagePaths
Using image = Image.FromFile(path)
Dim qrInput = New QrImageInput(image)
Dim results = reader.Read(qrInput)
For Each qr In results
Console.WriteLine($"Type: {qr.QrType}")
Console.WriteLine($"Data: {qr.Value}")
Console.WriteLine("---")
Next
End Using
Next
End Sub
End Class对于生产部署,请查看我们的NuGet包指南以确保您拥有适合您平台的正确包,并查阅API参考以获取所有可用方法和属性的完整文档。
常见问题
如何使用 IronQR 从不同的图像格式中读取 QR 码?
IronQR 支持从包括 JPEG、PNG、GIF 在内的一系列图像格式中读取 QR 码。这是通过先进的机器学习模型实现的,确保在各种媒体类型中的准确解码,并由 IronDrawing 库支持高效的图像处理。
使用 IronQR 在 C# 中从图像读取 QR 码需要哪些步骤?
要使用 IronQR 从图像读取 QR 码,您需要下载 IronQR 库,使用 IronDrawing 导入图像数据,创建 QrImageInput 对象,将其传递给 QrReader.Read 方法,并遍历每个检测到的 QR 码以查看相关信息。
为什么 IronQR 支持多种图像格式很重要?
支持多种图像格式使 IronQR 能在实际应用中更加灵活,其中 QR 码可以出现在不同的环境中,如市场营销材料、产品包装、数字文档和网络内容。这确保了开发人员可以构建没有格式兼容性问题的应用程序。
使用 IronQR 读取 QR 码我应该使用哪种图像格式?
图像格式的选择取决于您的需求。需要透明度的 QR 码使用 PNG,包含 QR 码的照片使用 JPEG,存档用途使用 TIFF,网络应用使用 WebP 因其出色的压缩效果。确保图像具有足够的分辨率和对比度以获得最佳效果。
IronQR 如何处理质量不佳的图像?
IronQR 使用故障容错功能如错误校正和机器学习模型来补偿模糊或光线不足等问题。它自动增强图像对比度和清晰度,并为挑战性环境提供自定义 QR 读取模式选项。
IronQR 可以读取哪些类型的 QR 码?
IronQR 可以读取各种类型的 QR 码,包括标准 QR 码、微型 QR 码和 RMQR 码,以支持需要紧凑或矩形格式的应用程序的不同用例和数据容量。
我什么时候该选择微型或 RMQR 码?
对于电路板或医疗设备等小表面选择微型 QR 码,对于需要装入狭长空间的窄运输标签或营销材料等矩形标签选择 RMQR 码。
IronQR 如何在图像中定位 QR 码?
IronQR 使用标准坐标系统来精确定位图像中的 QR 码所在位置。它使用 Points[] 数组检索并输出 QR 码角落的准确空间坐标。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。