C# QRコードリーダー (初心者向けステップバイステップチュートリアル)
今日のデジタル駆動の世界では、QRコード(クイックレスポンスコード)が至る所にあり、物理的な領域とデジタル領域をシームレスに結びつけています。 マーケティングから物流、金融からヘルスケアに至るまで、QRコードは効率的なデータ交換を促進する上で中心的な役割を果たしています。
[3] この記事では、C#開発の領域に深く入り込み、市場で最高のQRコードライブラリの1つであるIronQRが、開発者がQRコード認識の力を利用し、データを簡単にデコードし、さまざまな領域で革新を促進する方法を探ります。
[4] Iron SoftwareのIronQRは、堅牢な.NET QRコードリーダーライブラリとして際立っています。 IronQRによって実装された高度な機械学習モデルは、チャレンジングなシナリオでも、優れた精度と効率でアプリケーションがQRコードをデコードする力を与えます。
C#とIronQRを使用してQRコードを読み取る方法
[5] 1. Visual Studioプロジェクトを.NET Windowsフォームアプリケーションテンプレートで作成します。 [6] 2. IronQRをNuGetパッケージマネージャーからインストールします。 3. AForgeライブラリを使用してカメラから画像としてQRコードを取得します。 4. IronQRを使用してQRコードを読み取ります。
[7] IronQRは、.NETフレームワーク内でQRコード画像をスキャンおよび生成するために設計された、プレミアムC# 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+)とシームレスに統合されます。 現在 for .NET Coreバージョンは、Linux、Unix、macOSなどのクライアントオペレーティングシステムのサポートを拡張し、モバイルアプリの開発に対応しています。
前提条件
- Visual Studio: Visual Studioまたはその他 for .NET開発環境がインストールされていることを確認してください。
- Compatible Camera: カメラがデバイスに接続されていることを確認してください。
- NuGet Package Manager: プロジェクトでパッケージを管理できるようにNuGetを利用できることを確認してください。
Step 1: .NET Windows Formsアプリケーションテンプレートを使用してVisual Studioプロジェクトを作成する
カメラのビデオストリームや画像ファイルからQRコードバーコードを読み取るためのWindows Forms .NETアプリケーションを作成して始めましょう。 Visual Studioを開き、新しいプロジェクトの作成を選択し、その後.NET Windows Formsアプリケーションテンプレートを選択します。

次へをクリックし、プロジェクト名を入力します。

希望 for .NETバージョンを選択して、作成ボタンをクリックします。

Step 2: NuGetパッケージマネージャーからIronQRをインストールする
[8] IronQRは、NuGetパッケージマネージャーまたはVisual Studioパッケージマネージャーを使用してインストールできます。

以下にVisual Studioを使用してそれを行う方法を示します。

Step 3: AForge Libraryを使用してカメラから画像としてQRコードを取得する
カメラデバイスからQRコードをスキャンするには、AForge.Video.DirectShowライブラリをインストールする必要があります。 これはVisual Studioのパッケージマネージャーを使用して実行できます。 ソリューションエクスプローラーを右クリックして、パッケージマネージャーを開きます。

[9] このライブラリは、下記のようにNuGetパッケージコンソールを使用してインストールすることもできます。 ライブラリのインストールにはインストールボタンをクリックします。

Step 4: IronQRを使用してQRコードを読む
次のステップは、フォームにPictureBoxコンポーネントを作成することです。これは接続されているカメラデバイスからQRコード画像をスキャンするために必要です。
これはツールボックスからドラッグ&ドロップすることで行うことができます。 このPictureBoxは、カメラデバイスからQRコードデータを読み取るために必要です。

次に、読んだQRコードを表示するためのTextBoxをドラッグ&ドロップします。

以下のコードを追加して、IronQRを使ってQRコードを読み解きます。
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 に2つのイベントを登録しました:
Form1_LoadとForm1_FormClosing。 - AForge.Video.DirectShow ライブラリからの
videoSourceのインスタンスにVideoSource_NewFrameも登録しました。 - ライブビデオストリームからQRコードを読み取ります。
- QRコードが検出されると、デコードされたテキストを含むMessageBoxを表示します。
ライセンス (無料トライアル利用可能)
[10] IronQRにはライセンスキーが必要です。 トライアルキーはサイトから取得できますこちら。 このキーは appsettings.json に配置する必要があります。
{
"IronQr.LicenseKey": "MYLICENSE.KEY.TRIAL"
}
トライアルライセンスを取得するためにメールIDを提供し、送信後、キーはメールで届けられます。

結論
結論として、QRコードはその起源を超え、私たちのデジタルエコシステムにおいて不可欠な存在となりました。 [11] IronQRを使用することで、C#開発者はQRコード認識の力を活用し、さまざまなタイプのQRコードのデータを簡単にデコードし、さまざまな領域で革新を進めることができます。
QRコードは進化を続け、新しいテクノロジーに統合されるにつれ、シームレスなデータ交換を促進し、ユーザーエクスペリエンスを向上させる重要性はますます高まるでしょう。 [12] IronQRを活用してQRコードの可能性を受け入れ、C#開発における革新と効率の旅を始めましょう。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。