跳至页脚内容
USING IRONBARCODE

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读取QR代码

  1. 使用.NET Windows Forms应用程序模板创建一个Visual Studio项目。
  2. Install IronQR from the NuGet package manager.
  3. 使用AForge库将相机上的QR代码获取为图像。
  4. 使用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等客户端操作系统的支持,同时可用于开发移动应用程序。

前提条件

  1. Visual Studio: 确保您已安装Visual Studio或其他.NET开发环境。
  2. 兼容摄像头: 确保摄像头已连接到您的设备。
  3. NuGet包管理器: 验证您可以使用NuGet来管理项目中的包。

步骤1:使用.NET Windows Forms应用程序模板创建一个Visual Studio项目

让我们通过创建一个Windows Forms .NET应用程序来读取来自相机视频流或图像文件的QR代码条形码开始。 打开Visual Studio,选择创建新项目,然后选择.NET Windows Forms应用程序模板。

Visual Studio设置

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

项目命名

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

创建项目

步骤2:从NuGet包管理器安装IronQR

IronQR can be installed using the NuGet包管理器或Visual Studio包管理器进行安装。

NuGet安装

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

Visual Studio NuGet

步骤3:使用AForge库从相机获取QR代码图像

要从摄像设备扫描QR代码,我们需要安装AForge.Video.DirectShow库。 这可以通过Visual Studio包管理器完成。 右键单击解决方案资源管理器并打开包管理器。

AForge安装步骤1

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

AForge安装步骤2

步骤4:使用IronQR读取QR代码

接下来的步骤是在表单中创建一个PictureBox组件,这对于从连接到机器的相机设备扫描QR代码图像是必要的。

这可以通过从工具箱中拖放来完成。 这个PictureBox是读取来自摄像机设备的QR代码数据所必需的。

添加PictureBox

接下来的步骤是拖放一个文本框以显示读取的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);
            }
        }
    }
}
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 Namespace
$vbLabelText   $csharpLabel

输入图片文件

QR代码中编码的文本是:I Love IronQR

QR代码示例

输出

QR代码结果

示例代码说明

  1. 我们在Windows Forms中注册了两个事件:Form1_LoadForm1_FormClosing
  2. 我们还将VideoSource_NewFrame注册到AForge.Video.DirectShow库的videoSource实例。
  3. 我们从实时视频流中读取QR代码。
  4. 当检测到QR代码时,我们显示一个带有解码文本的消息框。

许可(提供免费试用)

IronQR需要一个许可证密钥。 可以从这里获取试用密钥。 此密钥需要放置在appsettings.json中。

{
    "IronQR.LicenseKey": "MYLICENSE.KEY.TRIAL"
}

提供电子邮件ID以获取试用许可,提交后,密钥将通过电子邮件发送。

许可证密钥

结论

总之,QR代码已经超越了它们的起源,成为我们数字生态系统中不可或缺的一部分。 通过IronQR,C#开发人员可以轻松地利用QR代码识别的力量,轻松解码各种类型的QR代码,并在各个领域进行创新。

随着QR代码不断发展并与新技术相结合,它们在促进无缝数据交换和增强用户体验方面的重要性将只会增加。 利用IronQR的潜力,踏上C#开发中创新与高效的新旅程。

常见问题解答

如何设置一个C#项目来读取二维码?

要设置一个用于读取二维码的C#项目,可以通过在Visual Studio中创建一个新的Windows窗体应用程序开始。通过NuGet包管理器安装IronQR库,并确保拥有AForge.Video.DirectShow库以从相机捕获视频流。

使用IronQR进行二维码读取的好处是什么?

IronQR通过利用先进的机器学习模型提供了用于二维码读取的强大功能。它确保即便在困难条件下也能准确解码,并支持各种.NET框架和操作系统。

如何在C#应用程序中使用IronQR解码二维码?

在C#应用程序中使用IronQR解码二维码,需要使用AForge从相机捕获图像,然后使用IronQR库处理并解码捕获的图像中的二维码。

IronQR能从实时视频流中解码二维码吗?

是的,IronQR可以通过集成AForge.Video.DirectShow库从实时视频流中捕获帧并实时处理它们来解码二维码。

构建C#二维码阅读器所需的基本库有哪些?

构建C#二维码阅读器所需的基本库包括用于解码二维码的IronQR和用于从相机捕获视频流的AForge.Video.DirectShow。

IronQR如何在二维码解码中确保数据完整性?

IronQR通过准确区分真实二维码和潜在威胁来增强安全性和确保数据完整性,从而保证解码信息的可靠性。

使用IronQR扫描图片中的二维码是否可能?

是的,IronQR可以扫描不仅来自实时流的二维码,还可以扫描来自静态图像的二维码,为各种应用需求提供灵活性。

如何获取IronQR的试用许可证?

您可以从Iron Software网站获取IronQR的试用许可证,使您可以通过在应用程序的appsettings.json文件中放置试用密钥来测试库的功能。

PictureBox组件在二维码阅读器应用程序中起到什么作用?

在二维码阅读器应用程序中,PictureBox组件显示来自相机的实时视频流,使IronQR能够从传入的帧中捕获和解码二维码。

如何解决C#中二维码解码问题?

如果遇到二维码解码问题,请确保IronQR和AForge库已正确安装,检查相机流是否正确集成,并验证您的应用程序是否正确捕获和处理视频帧。

Jordi Bardia
软件工程师
Jordi 最擅长 Python、C# 和 C++,当他不在 Iron Software 利用这些技能时,他就在游戏编程。分享产品测试、产品开发和研究的责任,Jordi 在持续的产品改进中增加了巨大的价值。多样的经验使他面临挑战并保持投入,他表示这是在 Iron Software 工作的最喜欢的方面之一。Jordi 在佛罗里达州迈阿密长大,并在佛罗里达大学学习计算机科学和统计学。