.NET MAUI QRコードスキャナ

This article was translated from English: Does it need improvement?
Translated
View the article in English

はじめに

.NET MAUI (.NET Multi-platform App UI) は、単一のC# コードベースからネイティブのモバイルおよびデスクトップアプリケーションを構築するためのクロスプラットフォームフレームワークです。 単一のプロジェクトでAndroid、iOS、macOS、およびWindowsをターゲットにでき、すべてのプラットフォームでUIレイアウトとビジネスロジックを共有します。 MAUI for .NETエコシステムとの統合により、開発者は慣れ親しんだツールや言語を放棄することなく、モバイルユーザーにリーチできます。

この記事では、デバイスのフォトライブラリから選択したQRコードをデコードするためにIronQRを使用して、.NET MAUIアプリケーションでネイティブQRコードスキャナを構築する方法を説明します。

IronQR: C# QRコードライブラリ

アプリケーション内でQRコードを読み取るには、IronQR .NET ライブラリを使用します。 このライブラリは、モバイルデバイスで選択されたファイルを含むあらゆる画像ソースからQRコードを検出してデコードするためのシンプルなAPIを提供します。IronQRはすべてのMAUIターゲットプラットフォームで動作し、バーコードのドメイン知識を必要とせずに統合できます。

IronQRは標準のQRコード、Micro QRコード、およびrMQRコードをデコードでき、画像入力をファイル、ストリーム、またはビットマップとして受け入れます。 NuGetパッケージマネージャを通じて数秒でインストールできます。

.NET MAUIでQRコードスキャナを構築する手順

.NET MAUIアプリケーションにQRコードスキャンを追加するために、以下の手順に従ってください。

前提条件

  1. .NET MAUIのワークロードがインストールされたVisual Studio 2022
  2. AndroidまたはiOSをターゲットにした.NET MAUIプロジェクト

IronQRのインストール

Visual StudioのNuGetパッケージマネージャーコンソールを使用してIronQRライブラリをインストールします。 Tools > NuGet Package Manager > Package Manager Consoleに移動して実行します:

Install-Package IronQR

または、NuGetIronQRを検索し、最新バージョンをインストールします。

フロントエンドデザイン

スキャナのUIは画像選択をトリガーするためのボタン、選択したQRコードをプレビューするための画像ビュー、デコードされた結果を表示するためのラベルで構成されます。

MainPage.xamlの内容を次のものに置き換えます:

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiQrScanner.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Button
                x:Name="scanButton"
                Text="Select QR Code Image"
                SemanticProperties.Hint="Select Image"
                Clicked="OnScanButtonClicked"
                HorizontalOptions="Center" />

            <Image
                x:Name="qrImage"
                SemanticProperties.Description="Selected QR Code"
                HeightRequest="200"
                HorizontalOptions="Center" />

            <Label
                x:Name="resultLabel"
                Text="Scanned Text: "
                HorizontalOptions="Center"
                VerticalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiQrScanner.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <Button
                x:Name="scanButton"
                Text="Select QR Code Image"
                SemanticProperties.Hint="Select Image"
                Clicked="OnScanButtonClicked"
                HorizontalOptions="Center" />

            <Image
                x:Name="qrImage"
                SemanticProperties.Description="Selected QR Code"
                HeightRequest="200"
                HorizontalOptions="Center" />

            <Label
                x:Name="resultLabel"
                Text="Scanned Text: "
                HorizontalOptions="Center"
                VerticalOptions="Center" />

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
XML

サンプル入力

以下のQRコードをテスト画像として使用します。 それをデバイスに保存し、アプリのファイルピッカーを通じて選択します。 デコードされた値はhttps://ironsoftware.comとして表示されるべきです。

Sample QR code encoding https://ironsoftware.com for testing the .NET MAUI QR scanner

サンプルQRコード — `https://ironsoftware.com`をエンコード

IronQRによるQRコードスキャン

スキャンボタンがタップされると、FilePickerがデバイスの画像ライブラリを開きます。 ユーザーが写真を選択した後、完全なパスがQrReader.Read()に渡されます。 最初に検出されたQRコードからデコードされた値が結果ラベルに表示されます。

次のメソッドをMainPage.xaml.csに追加します:

using IronQr;
using IronSoftware.Drawing;

private async void OnScanButtonClicked(object sender, EventArgs e)
{
    // Start scanning QR codes
    var images = await FilePicker.Default.PickAsync(new PickOptions
    {
        PickerTitle = "Pick image",
        FileTypes = FilePickerFileType.Images
    });
    var imageSource = images.FullPath.ToString();

    var inputBmp = AnyBitmap.FromFile(imageSource);

    // Load the asset into QrImageInput
    QrImageInput imageInput = new QrImageInput(inputBmp);

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

    // Read the input and get all embedded QR Codes
    IEnumerable<QrResult> results = reader.Read(imageInput);

    // Display the first result
    resultLabel.Text = "Scanned Text: " + results.First().Value;
}
using IronQr;
using IronSoftware.Drawing;

private async void OnScanButtonClicked(object sender, EventArgs e)
{
    // Start scanning QR codes
    var images = await FilePicker.Default.PickAsync(new PickOptions
    {
        PickerTitle = "Pick image",
        FileTypes = FilePickerFileType.Images
    });
    var imageSource = images.FullPath.ToString();

    var inputBmp = AnyBitmap.FromFile(imageSource);

    // Load the asset into QrImageInput
    QrImageInput imageInput = new QrImageInput(inputBmp);

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

    // Read the input and get all embedded QR Codes
    IEnumerable<QrResult> results = reader.Read(imageInput);

    // Display the first result
    resultLabel.Text = "Scanned Text: " + results.First().Value;
}
Imports IronQr
Imports IronSoftware.Drawing

Private Async Sub OnScanButtonClicked(sender As Object, e As EventArgs)
    ' Start scanning QR codes
    Dim images = Await FilePicker.Default.PickAsync(New PickOptions With {
        .PickerTitle = "Pick image",
        .FileTypes = FilePickerFileType.Images
    })
    Dim imageSource = images.FullPath.ToString()

    Dim inputBmp = AnyBitmap.FromFile(imageSource)

    ' Load the asset into QrImageInput
    Dim imageInput As New QrImageInput(inputBmp)

    ' Create a QR Reader object
    Dim reader As New QrReader()

    ' Read the input and get all embedded QR Codes
    Dim results As IEnumerable(Of QrResult) = reader.Read(imageInput)

    ' Display the first result
    resultLabel.Text = "Scanned Text: " & results.First().Value
End Sub
$vbLabelText   $csharpLabel

FilePicker.Default.PickAsyncはMAUIプラットフォームの抽象レイヤーによって提供されており、Android、iOS、Windowsでプラットフォーム固有のコードなしで動作します。 IEnumerable<QrResult>を返します。

出力

QRコード画像を選択すると、スキャンがトリガーされます。 デコードされた値が画像プレビューの下の結果ラベルに表示されます。

.NET MAUI QR Code Scanner using IronQR — app selecting a QR code image and displaying the decoded value

QRコードを選択して、デコードされた値を結果ラベルに表示します

プロジェクトをダウンロードする

完全なMauiQrScannerプロジェクトをダウンロードするにはこちらをクリックしてください。

結論

この記事では、.NET MAUIアプリケーションでIronQRを使用してネイティブのQRコードスキャナを構築する方法を示しました。 FilePicker APIはAndroid、iOS、Windowsでのプラットフォームネイティブな画像選択を提供し、IronQRのQrReader.Readが単一の呼び出しでデコードを処理します。 同じアプローチが、画像ごとに複数のQRコードにスケールし、.First()を呼び出すのではなく、完全な結果コレクションを反復処理します。

IronQRは開発および商用利用のためにライセンスが必要です。 ライセンスの詳細はこちらでご覧いただけます。

値を超えてQRコードのプロパティを読み取る詳しい説明は、QRコードの値を読むおよびQRコードのタイプを読むガイドをご覧ください。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 63,625 | バージョン: 2026.4 リリース
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package IronQR
サンプルを実行する URL が QR コードになるのを見る。