跳過到頁腳內容
使用IRONBARCODE

C# QR碼閱讀器(初學者逐步教程)

在當今數位驅動的世界中,QR 代碼(快速響應代碼)已經無處不在,無縫連接物理和數位領域。 從市場營銷到物流,金融到醫療保健,QR 代碼在促進高效數據交換方面發揮著關鍵作用。

在本文中,我們深入探討 C# 開發領域,探索市場上最佳的 QR 代碼庫之一 IronQR 如何讓開發者輕鬆利用 QR 代碼識別的力量,輕鬆解碼數據,並在各個領域創新。

Iron Software 的 IronQR 是一個突出顯著的 .NET QR 代碼讀取庫。 IronQR 實現的先進機器學習模型使您的應用程式能夠在挑戰性環境中,以無與倫比的準確性和效率來解碼 QR 代碼。

如何使用 IronQR 在 C# 中讀取 QR 代碼

  1. 使用 .NET Windows Forms 應用程式範本創建一個 Visual Studio 專案。
  2. Install IronQR from the NuGet package manager.
  3. 使用 AForge 庫從攝像機獲取 QR 代碼作為圖像。
  4. 使用 IronQR 讀取 QR 代碼。

IronQR 是一個專為在 .NET 框架內部掃描和生成 QR 代碼圖像而設計的首選 C# QR 代碼讀取庫。 通過利用尖端的機器學習技術,IronQR 將 QR 代碼讀取提升到前所未有的水準。

無論您是從圖像、影片還是實時攝像頭鏡頭掃描 QR 代碼,這個機器學習驅動的解決方案都保證快速可靠的信息檢索。

這種創新方法不僅簡化了數據提取,而且通過識別真實的 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 代碼。

添加 TextBox

添加以下代碼以使用 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 Forms 應用程式。從 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 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。