C# QR Code Reader (Step by Step Tutorial for Beginner)
在当今数字驱动的世界中,QR代码(快速响应代码)已变得无处不在,无缝连接了物理和数字领域。 从营销到物流,从金融到医疗保健,QR代码在促进高效数据交换中发挥了关键作用。
在本文中,我们深入探讨了C#开发领域,探索了市场上最好的QR代码库之一IronQR如何使开发人员能够利用QR代码识别的力量,轻松解码数据,并在各个领域创新。
Iron Software的IronQR脱颖而出,是一个强大的.NET QR代码读取库。 IronQR实现的高级机器学习模型使您的应用程序能够以无与伦比的准确性和效率解码QR代码,即使在具有挑战性的场景中也是如此。
如何使用C#和IronQR读取二维码
- 使用.NET Windows Forms应用程序模板创建一个Visual Studio项目。
- 从NuGet包管理器安装IronQR。
- 使用AForge库将相机上的QR代码获取为图像。
- 使用IronQR读取QR代码。
IronQR作为领先的C# QR代码读取库,可设计用于在.NET框架内扫描和生成QR代码图像。 通过采用尖端的ML技术,IronQR将QR代码读取提升到了前所未有的水平。
无论您是从图像、视频还是实时摄像机获取QR代码,这个ML驱动的解决方案都能保证快速可靠的信息检索。
这种创新方法不仅简化了数据提取过程,还通过区分真实的QR代码和潜在的威胁来增强安全性。 通过直观的API,开发人员可以在几分钟内将QR代码功能无缝集成到他们的.NET项目中。
IronQR与.NET Core (8, 7, 6, 5 和 3.1+)、.NET Standard (2.0+) 和 .NET Framework (4.6.2+) 无缝集成。 目前的.NET Core版本扩展了其对Linux、Unix和macOS等客户端操作系统的支持,同时可用于开发移动应用程序。
前提条件
- Visual Studio: 确保您已安装Visual Studio或其他.NET开发环境。
- 兼容摄像头: 确保摄像头已连接到您的设备。
- NuGet包管理器: 验证您可以使用NuGet来管理项目中的包。
步骤1:使用.NET Windows Forms应用程序模板创建一个Visual Studio项目
让我们通过创建一个Windows Forms .NET应用程序来读取来自相机视频流或图像文件的QR代码条形码开始。 打开Visual Studio,选择创建新项目,然后选择.NET Windows Forms应用程序模板。

点击下一步并输入项目名称。

选择所需的.NET版本,然后点击创建按钮。

步骤2:从NuGet包管理器安装IronQR
IronQR可以使用NuGet包管理器或Visual Studio包管理器进行安装。

下图展示了如何使用Visual Studio进行安装。

步骤3:使用AForge库从相机获取QR代码图像
要从相机设备扫描二维码,我们需要安装AForge.Video.DirectShow库。 这可以通过Visual Studio包管理器完成。 右键单击解决方案资源管理器并打开包管理器。

此库也可以使用NuGet包控制台安装,如下所示。 点击安装按钮安装库。

步骤4:使用IronQR读取QR代码
接下来的步骤是在表单中创建一个PictureBox组件,这对于从连接到机器的相机设备扫描QR代码图像是必要的。
这可以通过从工具箱中拖放来完成。 这个PictureBox是读取来自摄像机设备的QR代码数据所必需的。

接下来的步骤是拖放一个文本框以显示读取的QR代码。

添加以下代码以使用IronQR读取QR代码并解码它们。
using AForge.Video.DirectShow;
using AForge.Video;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using IronQR;
namespace ReadQR
{
public partial class Form1 : Form
{
// Declare a video capture device
private VideoCaptureDevice videoSource;
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
this.FormClosing += Form1_FormClosing;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Stop the video source when the form is closing
if (videoSource != null && videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource.WaitForStop();
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Retrieve video input devices connected to the machine
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count > 0)
{
// Use the first available video device
videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
videoSource.NewFrame += VideoSource_NewFrame;
videoSource.Start();
}
else
{
MessageBox.Show("No video devices found.");
Close();
}
}
private void VideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
// Update the PictureBox with the new frame from the camera
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();
var image = (Bitmap)eventArgs.Frame.Clone();
// Set the license key for IronQR. Replace "YourKey" with your actual key
License.LicenseKey = "YourKey";
// Prepare the image for QR code reading
QrImageInput imageInput = new QrImageInput(image);
// Create a QR reader object
QrReader reader = new QrReader();
// Read QR codes from the image
IEnumerable<QrResult> results = reader.Read(imageInput);
// Display the first QR code result in a MessageBox
if (results.Any())
{
MessageBox.Show(results.First().Value);
}
}
}
}Imports AForge.Video.DirectShow
Imports AForge.Video
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Linq
Imports System.Windows.Forms
Imports IronQR
Namespace ReadQR
Partial Public Class Form1
Inherits Form
' Declare a video capture device
Private videoSource As VideoCaptureDevice
Public Sub New()
InitializeComponent()
AddHandler Me.Load, AddressOf Form1_Load
AddHandler Me.FormClosing, AddressOf Form1_FormClosing
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
' Stop the video source when the form is closing
If videoSource IsNot Nothing AndAlso videoSource.IsRunning Then
videoSource.SignalToStop()
videoSource.WaitForStop()
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
' Retrieve video input devices connected to the machine
Dim videoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice)
If videoDevices.Count > 0 Then
' Use the first available video device
videoSource = New VideoCaptureDevice(videoDevices(0).MonikerString)
AddHandler videoSource.NewFrame, AddressOf VideoSource_NewFrame
videoSource.Start()
Else
MessageBox.Show("No video devices found.")
Close()
End If
End Sub
Private Sub VideoSource_NewFrame(ByVal sender As Object, ByVal eventArgs As NewFrameEventArgs)
' Update the PictureBox with the new frame from the camera
pictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
Dim image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
' Set the license key for IronQR. Replace "YourKey" with your actual key
License.LicenseKey = "YourKey"
' Prepare the image for QR code reading
Dim imageInput As New QrImageInput(image)
' Create a QR reader object
Dim reader As New QrReader()
' Read QR codes from the image
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
' Display the first QR code result in a MessageBox
If results.Any() Then
MessageBox.Show(results.First().Value)
End If
End Sub
End Class
End NamespaceImports AForge.Video.DirectShow
Imports AForge.Video
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Linq
Imports System.Windows.Forms
Imports IronQR
Namespace ReadQR
Partial Public Class Form1
Inherits Form
' Declare a video capture device
Private videoSource As VideoCaptureDevice
Public Sub New()
InitializeComponent()
AddHandler Me.Load, AddressOf Form1_Load
AddHandler Me.FormClosing, AddressOf Form1_FormClosing
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
' Stop the video source when the form is closing
If videoSource IsNot Nothing AndAlso videoSource.IsRunning Then
videoSource.SignalToStop()
videoSource.WaitForStop()
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
' Retrieve video input devices connected to the machine
Dim videoDevices As New FilterInfoCollection(FilterCategory.VideoInputDevice)
If videoDevices.Count > 0 Then
' Use the first available video device
videoSource = New VideoCaptureDevice(videoDevices(0).MonikerString)
AddHandler videoSource.NewFrame, AddressOf VideoSource_NewFrame
videoSource.Start()
Else
MessageBox.Show("No video devices found.")
Close()
End If
End Sub
Private Sub VideoSource_NewFrame(ByVal sender As Object, ByVal eventArgs As NewFrameEventArgs)
' Update the PictureBox with the new frame from the camera
pictureBox1.Image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
Dim image = DirectCast(eventArgs.Frame.Clone(), Bitmap)
' Set the license key for IronQR. Replace "YourKey" with your actual key
License.LicenseKey = "YourKey"
' Prepare the image for QR code reading
Dim imageInput As New QrImageInput(image)
' Create a QR reader object
Dim reader As New QrReader()
' Read QR codes from the image
Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)
' Display the first QR code result in a MessageBox
If results.Any() Then
MessageBox.Show(results.First().Value)
End If
End Sub
End Class
End Namespace输入图片文件
QR代码中编码的文本是:I Love IronQR

输出

示例代码说明
- 我们在 Windows Forms 中注册了两个事件:
Form1_Load和Form1_FormClosing。 - 我们还从 AForge.Video.DirectShow 库注册了
videoSource的一个实例。 - 我们从实时视频流中读取QR代码。
- 当检测到QR代码时,我们显示一个带有解码文本的消息框。
许可(提供免费试用)
IronQR需要一个许可证密钥。 可以从这里获取试用密钥。 此密钥需要放置在appsettings.json。
{
"IronQr.LicenseKey": "MYLICENSE.KEY.TRIAL"
}
提供电子邮件ID以获取试用许可,提交后,密钥将通过电子邮件发送。

结论
总之,QR代码已经超越了它们的起源,成为我们数字生态系统中不可或缺的一部分。 通过IronQR,C#开发人员可以轻松地利用QR代码识别的力量,轻松解码各种类型的QR代码,并在各个领域进行创新。
随着QR代码不断发展并与新技术相结合,它们在促进无缝数据交换和增强用户体验方面的重要性将只会增加。 利用IronQR的潜力,踏上C#开发中创新与高效的新旅程。
Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。