使用 IRONBARCODE

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

已更新 2024年8月18日
分享:

介紹

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

在本文中,我們深入探討了C#開發領域,探索如何 IronQR, 市場上最好的 QR 碼庫之一,使開發人員能夠利用 QR 碼識別的力量,輕鬆解碼數據,並在各個領域進行創新。

IronQR 來自 Iron Software 卓越的 .NET 二維碼讀取庫。IronQR 採用先進的機器學習模型,使您的應用程式在即使在困難的情境下,也能以無與倫比的準確性和效率解碼二維碼。

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

  1. 建立 Visual Studio 專案使用 .NET Windows 表單應用程式模板

  2. 安裝 IronQR 來自 NuGet 程序管理器。

  3. 從 AForge 程式庫將相機的 QR 碼作為圖像獲取

  4. 使用 IronQR 讀取 QR 碼。

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. 兼容攝像頭: 確保攝像頭已連接到您的裝置。

  3. NuGet Package Manager: 確認您能使用 NuGet 在您的專案中管理套件。

第 1 步:使用 .NET Windows 表單應用程式範本創建 Visual Studio 專案

讓我們開始創建一個 Windows 表單 .NET 應用程式,用於從相機視頻流或圖像檔案中讀取 QR 碼。 打開 Visual Studio,選擇創建新專案,然後選擇 .NET Windows 表單應用程式範本。

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

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

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

IronQR 可以使用 NuGet 包管理器或Visual Studio 包管理器

以下顯示如何使用 Visual Studio 完成此操作

第三步:從 AForge Library 的相機獲取 QR 碼影像

要從相機裝置掃描 QR 碼,我們需要安裝 AForgeCore.Video.DirectShow 庫。這可以通過 Visual Studio 的套件管理器來完成,如下所示。在解決方案總管上右鍵單擊,然後打開套件管理器。

此函式庫也可以使用安装 NuGet 在下面的控制台打包。點擊安裝按鈕來安裝此庫。

第四步:使用IronQR讀取QR碼。

接下來的步驟是在表單中創建一個PictureBox元件,這是從連接到機器的相機設備掃描QR碼圖像所必需的。

這可以通過從工具箱中拖放來完成。這個PictureBox是從相機設備讀取QR碼數據所必需的。

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

將以下代碼添加到 使用中來讀取和解碼 QR 碼 IronQR.

using AForge.Video.DirectShow;
using AForge.Video;
using System.Drawing;
using IronQr;

namespace ReadQR
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
            this.FormClosing += Form1_FormClosing;

        }

        private VideoCaptureDevice videoSource;
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (videoSource != null && videoSource.IsRunning)
            {
                videoSource.SignalToStop();
                videoSource.WaitForStop();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count > 0)
            {
                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 picture box with the new frame from the camera.
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();

            var image = (Bitmap)eventArgs.Frame.Clone();

            // Set the license key
            License.LicenseKey = "YourKey";

            // Load  QrImageInput
            QrImageInput imageInput = new QrImageInput(image);

            // QR Reader object
            QrReader reader = new QrReader();

            // Read QR Codes
            IEnumerable<QrResult> results = reader.Read(imageInput);

            // display results
            MessageBox.Show(results.First().Value);
        }
    }
}
using AForge.Video.DirectShow;
using AForge.Video;
using System.Drawing;
using IronQr;

namespace ReadQR
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
            this.FormClosing += Form1_FormClosing;

        }

        private VideoCaptureDevice videoSource;
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (videoSource != null && videoSource.IsRunning)
            {
                videoSource.SignalToStop();
                videoSource.WaitForStop();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count > 0)
            {
                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 picture box with the new frame from the camera.
            pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();

            var image = (Bitmap)eventArgs.Frame.Clone();

            // Set the license key
            License.LicenseKey = "YourKey";

            // Load  QrImageInput
            QrImageInput imageInput = new QrImageInput(image);

            // QR Reader object
            QrReader reader = new QrReader();

            // Read QR Codes
            IEnumerable<QrResult> results = reader.Read(imageInput);

            // display results
            MessageBox.Show(results.First().Value);
        }
    }
}
IRON VB CONVERTER ERROR developers@ironsoftware.com
VB   C#

載入影像檔案

QR code 中編碼的文字是:我愛 IronQR

輸出

範例代碼說明

  1. 我們在 Windows Forms 中註冊了兩個事件:Form1_Load 和 Form1_FormClosing

  2. 我們還在 AForgeCore.Video.DirectShow 庫的 videoSource 實例中註冊了 VideoSource_NewFrame

  3. 然後我們從直播視頻流中讀取 QR 碼

  4. 當檢測到 QR 碼時,我們會顯示一個包含解碼文本的 MessageBox

授權 (免費試用)

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

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

提供電子郵件以獲取試用許可證,提交後,金鑰將通過電子郵件發送。

結論

總之,QR 碼已經超越了其起源,在我們的數位生態系統中變得不可或缺。 IronQRC# 開發人員可以利用 QR 碼識別的強大功能,輕鬆解碼各種類型的 QR 碼數據,並在各個領域創新。

隨著 QR 碼不斷發展和融入新技術,它們在促進無縫數據交換和提升用戶體驗方面的重要性只會越來越大。利用 QR 碼的潛力 IronQR 踏上 C# 開發創新與效率之旅。

< 上一頁
如何在 ASP.NET 中使用 C# 生成條碼

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 1,203,227 查看許可證 >