C# QR碼閱讀器(初學者逐步教程)
在當今數位驅動的世界中,QR碼(快速反應碼)已經變得無處不在,無縫地連接了實體和數位領域。 從行銷到物流、金融到醫療保健,QR碼在促進高效資料交換中扮演著關鍵角色。
在本文中,我們將深入探討C#開發領域,探索如何使用IronQR,這是市場上最佳的QR碼程式庫之一,賦能開發者利用QR碼識別功能,輕鬆解碼資料,並在各種領域中創新。
來自Iron Software的IronQR被譽為堅固的.NET QR碼讀取程式庫。 IronQR實施的先進機器學習模型賦予您的應用程式以無與倫比的準確性和效率解碼QR碼,甚至在挑戰性場景中。
如何使用IronQR和C#讀取QR碼
- 使用.NET Windows Forms Application模板建立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 Application模板。

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

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

步驟2:從NuGet包管理器安裝IronQR
IronQR可以通過NuGet包管理器或Visual Studio包管理器安裝。

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

步驟3:使用AForge Library將QR碼作為圖像從相機獲取
要從相機裝置掃描QR碼,我們需要安裝AForge.Video.DirectShow程式庫。 這可以通過Visual Studio包管理器完成。 右鍵點擊解決方案資源管理器並開啟包管理器。

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

步驟4:使用IronQR讀取QR碼
下一步是在窗體中建立一個PictureBox組件,用於掃描連接到機器的相機裝置的QR碼圖像。
這可以通過工具箱中的拖放操作完成。 這個PictureBox是用來從相機裝置中讀取QR碼資料的。

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

新增以下程式碼以讀取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);
}
}
}
}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_LoadandForm1_FormClosing。 - 我們還將
videoSource實例。 - 我們從實時影片流中讀取QR碼。
- 當檢測到QR碼時,我們會顯示一個含有解碼文字的MessageBox。
授權(提供免費試用)
IronQR需要一個授權密鑰。 可以從網站這裡獲得試用密鑰。 此密鑰需要放置在appsettings.json。
{
"IronQr.LicenseKey": "MYLICENSE.KEY.TRIAL"
}
提供電子郵件ID以獲得試用授權,提交後,密鑰將通過電子郵件發送。

結論
總而言之,QR碼已超越其最初的用途,在我們的數位生態系統中成為不可或缺的一部分。 使用IronQR,C#開發者可以輕鬆利用QR碼識別功能,解碼各種型別的QR碼資料,並在不同領域中進行創新。
隨著QR碼繼續發展並整合到新技術中,其在促進無縫資料交換和提升使用者體驗方面的重要性將隨之增長。 利用IronQR的潛力,開啟C#開發中的創新與效率之旅。
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。