跳過到頁腳內容
使用IRONBARCODE

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

在當今數位化驅動的世界中,二維碼(快速回應碼)已經無所不在,無縫連接了實體世界和數位世界。 從行銷到物流,從金融到醫療保健,二維碼在促進高效數據交換方面發揮關鍵作用。

在本文中,我們將深入探討 C# 開發領域,探索IronQR (市場上最好的二維碼庫之一)如何幫助開發人員利用二維碼識別功能,輕鬆解碼數據,並在各個領域進行創新。

Iron Software的 IronQR 是一款功能強大的 .NET 二維碼閱讀器庫,表現出色。 IronQR 實現的先進機器學習模型使您的應用程式能夠以無與倫比的準確性和效率解碼二維碼,即使在具有挑戰性的場景下也是如此。

如何使用 IronQR 和 C# 讀取二維碼

  1. 使用 .NET Windows 窗體應用程式範本建立Visual Studio專案。
  2. NuGet套件管理器安裝IronQR
  3. 使用 AForge 函式庫從相機取得二維碼影像。
  4. 使用 IronQR 讀取二維碼。

IronQR是一款出色的 C# 二維碼閱讀器庫,旨在 .NET 框架內掃描和產生二維碼影像。 IronQR 利用尖端的機器學習技術,將二維碼讀取提升到了前所未有的水平。

無論您是掃描影像、視訊還是即時相機畫面中的二維碼,這款基於機器學習的解決方案都能保證快速可靠的資訊檢索。

這種創新方法不僅簡化了資料擷取流程,而且透過區分真實的二維碼和潛在的威脅來增強安全性。 憑藉其直覺的 API,開發人員可以在幾分鐘內將二維碼功能無縫整合到他們的 .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.相容的攝影機:確保攝影機已連接到您的裝置。
  2. NuGet 套件管理器:驗證您是否可以使用NuGet來管理專案中的套件。

步驟 1:使用 .NET Windows 窗體應用程式範本建立 Visual Studio 項目

讓我們先建立一個 Windows Forms .NET 應用程序,用於從相機視訊串流或映像檔讀取二維碼條碼。 開啟 Visual Studio,選擇"建立新專案",然後選擇".NET Windows 窗體應用程式"範本。

Visual Studio 安裝

點擊"下一步",然後輸入項目名稱。

項目命名

選擇所需的 .NET 版本,然後按一下"建立"按鈕。

創建專案

步驟 2:從 NuGet 套件管理器安裝 IronQR

IronQR可以使用NuGet套件管理器或 Visual Studio 套件管理器進行安裝。

NuGet 安裝

以下展示如何使用 Visual Studio 來實作。

Visual Studio NuGet

步驟 3:使用 AForge 函式庫從相機取得二維碼影像。

要從相機設備掃描二維碼,我們需要安裝AForge.Video.DirectShow庫。 這可以透過 Visual Studio 套件管理器完成。 右鍵點選解決方案資源管理器,開啟程式包管理器。

AForge 安裝步驟 1

也可以使用NuGet套件控制台安裝此程式庫,如下所示。 點選"安裝"按鈕安裝庫檔案。

AForge 安裝步驟 2

步驟 4:使用 IronQR 讀取二維碼

下一步是在窗體中建立一個 PictureBox 元件,該元件用於掃描連接到機器的相機裝置中的二維碼影像。

這可以透過從工具箱中拖放來完成。 此 PictureBox 用於讀取相機設備中的二維碼資料。

新增圖片框

接下來,拖放一個文字方塊來顯示讀取的二維碼。

新增文字框

加入以下程式碼,即可使用IronQR讀取和解碼二維碼。

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

輸入影像檔案

二維碼中編碼的文字是:我愛 IronQR

QR 圖碼範例

輸出

QR 圖碼結果

範例程式碼解釋

  1. 我們在 Windows 表單中註冊了兩個事件: Form1_LoadForm1_FormClosing
  2. 我們也向 AForge.Video.DirectShow 庫中的videoSource實例註冊了VideoSource_NewFrame
  3. 我們讀取即時視訊串流中的二維碼。
  4. 當偵測到二維碼時,我們會顯示一個包含解碼文字的訊息方塊。

授權(可免費試用)

IronQR需要許可證密鑰。 您可以從此網站取得試用金鑰。 此 key 需要放在 appsettings.json 中。

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

請提供您的電子郵件地址以取得試用許可證,提交後,金鑰將透過電子郵件發送給您。

許可證密鑰

結論

總之,二維碼已經超越了其起源,成為我們數位生態系統中不可或缺的一部分。 借助IronQR ,C# 開發人員可以利用二維碼識別的強大功能,輕鬆解碼各種類型的二維碼中的數據,並在各個領域進行創新。

隨著二維碼不斷發展並融入新技術,它們在促進無縫資料交換和提升使用者體驗方面的重要性只會越來越大。 運用IronQR擁抱二維碼的潛力,開啟 C# 開發創新與高效率的旅程。

常見問題解答

如何設定 C# 專案來讀取 QR 碼?

若要建立一個讀取 QR 碼的 C# 專案,首先要在 Visual Studio 中建立一個新的 Windows Forms 應用程式。從 NuGet 套件管理員安裝 IronQR 函式庫,並確保您擁有 AForge.Video.DirectShow 函式庫,以便從攝影機擷取視訊訊源。

使用 IronQR 讀取 QR 碼有哪些好處?

IronQR 利用先進的機器學習模型,為 QR 碼讀取提供強大的功能。即使在具有挑戰性的條件下,它也能確保精確的解碼,並支援各種 .NET Framework 和作業系統。

如何在 C# 應用程式中使用 IronQR 解碼 QR 碼?

若要使用 IronQR 在 C# 應用程式中解碼 QR 碼,請使用 AForge 從攝影機饋送中擷取影像,然後使用 IronQR 函式庫從擷取的影像中處理並解碼 QR 碼。

IronQR 可以解碼即時視訊訊號中的 QR 碼嗎?

是的,IronQR 可透過與 AForge.Video.DirectShow 函式庫整合,從即時視訊來源解碼 QR 碼,以即時擷取畫面並進行處理。

要以 C# 建立 QR 碼閱讀器,哪些函式庫是不可或缺的?

在 C# 中建立 QR 碼閱讀器的基本函式庫包括用於解碼 QR 碼的 IronQR 和用於擷取攝影機視訊訊源的 AForge.Video.DirectShow。

IronQR 如何確保 QR 碼解碼過程中的資料完整性?

IronQR 可準確區分真實的 QR 碼與潛在的威脅,進而維持解碼資訊的可靠性,藉此增強安全性並確保資料完整性。

是否可以使用 IronQR 從影像掃描 QR 碼?

是的,IronQR 不僅可以掃描即時訊息中的 QR 碼,也可以掃描靜態影像中的 QR 碼,為各種應用程式需求提供彈性。

如何取得 IronQR 的試用授權?

您可以從 Iron Software 網站取得 IronQR 的試用授權,只要將試用金鑰放在應用程式的 appsettings.json 檔案中,就可以測試函式庫的功能。

PictureBox 元件在 QR 閱讀器應用程式中扮演什麼角色?

在 QR 碼閱讀器應用程式中,PictureBox 元件會顯示攝影機的即時影像畫面,讓 IronQR 可以從傳入的畫面中擷取並解碼 QR 碼。

如何排除 C# 中 QR 碼解碼的問題?

如果您遇到 QR 碼解碼的問題,請確認已正確安裝 IronQR 和 AForge 函式庫、檢查攝影機訊號是否已正確整合,並驗證您的應用程式是否正確擷取和處理視訊畫格。

Jordi Bardia
軟體工程師
Jordi 在 Python、C# 和 C++ 上最得心應手,當他不在 Iron Software 展現技術時,便在做遊戲編程。在分担产品测测试,产品开发和研究的责任时,Jordi 为持续的产品改进增值。他说这种多样化的经验使他受到挑战并保持参与, 而这也是他与 Iron Software 中工作一大乐趣。Jordi 在佛罗里达州迈阿密长大,曾在佛罗里达大学学习计算机科学和统计学。