跳至頁尾內容
使用IRONBARCODE

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

在當今數位驅動的世界中,QR碼(快速反應碼)已經變得無處不在,無縫地連接了實體和數位領域。 從行銷到物流、金融到醫療保健,QR碼在促進高效資料交換中扮演著關鍵角色。

在本文中,我們將深入探討C#開發領域,探索如何使用IronQR,這是市場上最佳的QR碼程式庫之一,賦能開發者利用QR碼識別功能,輕鬆解碼資料,並在各種領域中創新。

來自Iron Software的IronQR被譽為堅固的.NET QR碼讀取程式庫。 IronQR實施的先進機器學習模型賦予您的應用程式以無與倫比的準確性和效率解碼QR碼,甚至在挑戰性場景中。

如何使用IronQR和C#讀取QR碼

  1. 使用.NET Windows Forms Application模板建立Visual Studio項目。
  2. NuGet包管理器安裝IronQR
  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 Application模板。

Visual Studio安裝

點擊下一步並輸入專案名稱。

專案命名

選擇所需的.NET版本,然後點擊建立按鈕。

建立專案

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

IronQR可以通過NuGet包管理器或Visual Studio包管理器安裝。

NuGet安裝

以下顯示如何通過Visual Studio來完成。

Visual Studio NuGet

步驟3:使用AForge Library將QR碼作為圖像從相機獲取

要從相機裝置掃描QR碼,我們需要安裝AForge.Video.DirectShow程式庫。 這可以通過Visual Studio包管理器完成。 右鍵點擊解決方案資源管理器並開啟包管理器。

AForge安裝步驟1

該程式庫也可以使用NuGet包控制台安裝,如下所示。 點擊安裝按鈕以安裝該程式庫。

AForge安裝步驟2

步驟4:使用IronQR讀取QR碼

下一步是在窗體中建立一個PictureBox組件,用於掃描連接到機器的相機裝置的QR碼圖像。

這可以通過工具箱中的拖放操作完成。 這個PictureBox是用來從相機裝置中讀取QR碼資料的。

新增PictureBox

接下來,拖放一個文字框來顯示讀取的QR碼。

新增TextBox

新增以下程式碼以讀取QR碼並使用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

輸入圖像文件

QR碼中的編碼文字為:I Love IronQR

QR碼範例

輸出

QR碼結果

範例程式碼解釋

  1. 我們在Windows Forms中註冊了兩個事件:Form1_Load and Form1_FormClosing
  2. 我們還將videoSource實例。
  3. 我們從實時影片流中讀取QR碼。
  4. 當檢測到QR碼時,我們會顯示一個含有解碼文字的MessageBox。

授權(提供免費試用)

IronQR需要一個授權密鑰。 可以從網站這裡獲得試用密鑰。 此密鑰需要放置在appsettings.json

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

提供電子郵件ID以獲得試用授權,提交後,密鑰將通過電子郵件發送。

授權密鑰

結論

總而言之,QR碼已超越其最初的用途,在我們的數位生態系統中成為不可或缺的一部分。 使用IronQR,C#開發者可以輕鬆利用QR碼識別功能,解碼各種型別的QR碼資料,並在不同領域中進行創新。

隨著QR碼繼續發展並整合到新技術中,其在促進無縫資料交換和提升使用者體驗方面的重要性將隨之增長。 利用IronQR的潛力,開啟C#開發中的創新與效率之旅。

常見問題

如何設置C#專案以讀取QR碼?

為了設置用於讀取QR碼的C#專案,首先在Visual Studio中建立新的Windows Forms應用程式。從NuGet Package Manager安裝IronQR程式庫,並確保您有AForge.Video.DirectShow程式庫來從相機捕獲影片流。

使用IronQR讀取QR碼有什麼好處?

IronQR通過利用先進的機器學習模型提供強大的QR碼讀取能力。即便在具有挑戰的條件下,它也能確保準確的解碼,並支持多個.NET框架和操作系統。

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

若要在C#應用程式中使用IronQR解碼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碼,還能從靜態圖像中進行掃描,提供對不同應用需求的靈活性。

我該如何獲取IronQR的試用授權?

您可以從Iron Software的網站獲取IronQR的試用授權,這允許您通過將試用金鑰放置在應用程式的appsettings.json文件中來測試該程式庫的功能。

PictureBox組件在QR碼讀取應用程式中起什麼作用?

在QR碼讀取應用程式中,PictureBox組件顯示來自相機的實時影片流,從而使IronQR能夠捕獲並解碼進入幀中的QR碼。

我如何解決C#中QR碼解碼的問題?

如果您在QR碼解碼過程中遇到問題,請確保正確安裝了IronQR和AForge程式庫,檢查相機流是否正確整合,並確認您的應用程式是否正確捕獲和處理影片幀。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話