C# 二維碼閱讀器(初學者的逐步教學)
在當今數位化驅動的世界中,二維碼(快速回應碼)已經無所不在,無縫連接了實體世界和數位世界。 從行銷到物流,從金融到醫療保健,二維碼在促進高效數據交換方面發揮關鍵作用。
在本文中,我們將深入探討 C# 開發領域,探索IronQR (市場上最好的二維碼庫之一)如何幫助開發人員利用二維碼識別功能,輕鬆解碼數據,並在各個領域進行創新。
Iron Software的 IronQR 是一款功能強大的 .NET 二維碼閱讀器庫,表現出色。 IronQR 實現的先進機器學習模型使您的應用程式能夠以無與倫比的準確性和效率解碼二維碼,即使在具有挑戰性的場景下也是如此。
如何使用 IronQR 和 C# 讀取二維碼
- 使用 .NET Windows 窗體應用程式範本建立Visual Studio專案。
- 從NuGet套件管理器安裝IronQR 。
- 使用 AForge 函式庫從相機取得二維碼影像。
- 使用 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 等客戶端作業系統的支持,並與開發行動應用程式相容。
先決條件
- Visual Studio:請確保您已安裝 Visual Studio 或任何其他 .NET 開發環境。 2.相容的攝影機:確保攝影機已連接到您的裝置。
- NuGet 套件管理器:驗證您是否可以使用NuGet來管理專案中的套件。
步驟 1:使用 .NET Windows 窗體應用程式範本建立 Visual Studio 項目
讓我們先建立一個 Windows Forms .NET 應用程序,用於從相機視訊串流或映像檔讀取二維碼條碼。 開啟 Visual Studio,選擇"建立新專案",然後選擇".NET Windows 窗體應用程式"範本。
點擊"下一步",然後輸入項目名稱。
選擇所需的 .NET 版本,然後按一下"建立"按鈕。
步驟 2:從 NuGet 套件管理器安裝 IronQR
IronQR可以使用NuGet套件管理器或 Visual Studio 套件管理器進行安裝。
以下展示如何使用 Visual Studio 來實作。
步驟 3:使用 AForge 函式庫從相機取得二維碼影像。
要從相機設備掃描二維碼,我們需要安裝AForge.Video.DirectShow庫。 這可以透過 Visual Studio 套件管理器完成。 右鍵點選解決方案資源管理器,開啟程式包管理器。
也可以使用NuGet套件控制台安裝此程式庫,如下所示。 點選"安裝"按鈕安裝庫檔案。
步驟 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);
}
}
}
}輸入影像檔案
二維碼中編碼的文字是:我愛 IronQR
! QR 圖碼範例
輸出
! QR 圖碼結果
範例程式碼解釋
- 我們在 Windows 表單中註冊了兩個事件:
Form1_Load和Form1_FormClosing。 - 我們也向 AForge.Video.DirectShow 庫中的
videoSource實例註冊了VideoSource_NewFrame。 - 我們讀取即時視訊串流中的二維碼。
- 當偵測到二維碼時,我們會顯示一個包含解碼文字的訊息方塊。
授權(可免費試用)
IronQR需要許可證密鑰。 您可以從此網站取得試用金鑰。 此 key 需要放在 appsettings.json 中。
{
"IronQR.LicenseKey": "MYLICENSE.KEY.TRIAL"
}請提供您的電子郵件地址以取得試用許可證,提交後,金鑰將透過電子郵件發送給您。
結論
總之,二維碼已經超越了其起源,成為我們數位生態系統中不可或缺的一部分。 借助IronQR ,C# 開發人員可以利用二維碼識別的強大功能,輕鬆解碼各種類型的二維碼中的數據,並在各個領域進行創新。
隨著二維碼不斷發展並融入新技術,它們在促進無縫資料交換和提升使用者體驗方面的重要性只會越來越大。 運用IronQR擁抱二維碼的潛力,開啟 C# 開發創新與高效率的旅程。
常見問題解答
如何設定一個C#專案來讀取二維碼?
要設定一個用於讀取二維碼的 C# 項目,請先在 Visual Studio 中建立一個新的 Windows 窗體應用程式。從 NuGet 套件管理器安裝 IronQR 庫,並確保您擁有 AForge.Video.DirectShow 庫以從攝影機擷取視訊串流。
使用 IronQR 進行二維碼讀取有哪些好處?
IronQR 利用先進的機器學習模型,提供強大的二維碼讀取功能。即使在複雜條件下,它也能確保準確解碼,並支援各種 .NET 框架和作業系統。
如何在C#應用程式中使用IronQR解碼二維碼?
若要在 C# 應用程式中使用 IronQR 解碼 QR 碼,請使用 AForge 從相機畫面擷取影像,然後使用 IronQR 庫處理和解碼擷取影像中的 QR 碼。
IronQR能否解碼即時視訊串流中的二維碼?
是的,IronQR 可以透過與 AForge.Video.DirectShow 庫整合來解碼即時視訊串流中的二維碼,從而捕獲幀並即時處理它們。
用 C# 建立二維碼閱讀器需要哪些必備庫?
在 C# 中建立 QR 碼閱讀器所需的基本庫包括用於解碼 QR 碼的 IronQR 和用於從攝影機擷取視訊串流的 AForge.Video.DirectShow。
IronQR 如何確保二維碼解碼過程中的資料完整性?
IronQR 透過準確區分真假二維碼和潛在威脅,增強安全性並確保資料完整性,從而保持解碼資訊的可靠性。
是否可以使用 IronQR 掃描圖片中的二維碼?
是的,IronQR不僅可以掃描即時視訊串流中的二維碼,還可以掃描靜態影像中的二維碼,從而為各種應用需求提供靈活性。
如何取得IronQR的試用授權?
您可以從 Iron Software 網站獲得 IronQR 的試用許可證,透過將試用金鑰放入應用程式的appsettings.json檔案中來測試該程式庫的功能。
PictureBox 元件在二維碼閱讀器應用程式中扮演什麼角色?
在二維碼閱讀器應用程式中,PictureBox 元件顯示來自相機的即時視訊串流,使 IronQR 能夠捕捉和解碼傳入幀中的二維碼。
如何排查C#中二維碼解碼的問題?
如果遇到二維碼解碼問題,請確保 IronQR 和 AForge 庫已正確安裝,檢查攝影機畫面是否已正確集成,並驗證您的應用程式是否已正確擷取和處理視訊畫面。






