Q&A
前提
.NET MAUI でQRcodeを読み取るandroidアプリを作りたいです。
実現したいこと
androidの実機を持っていないので、androidエミュレータを使用し、pc のフロントカメラでqrcodeが読み取れるか試したい。
仮想デバイスはpixel 5 api 33 backのカメラをwebcom0にしています。
発生している問題・エラーメッセージ
カメラは起動するが、qrcodeを読み取ってくれない。
該当のソースコード
MainPage.xaml
XAML
1<?xml version="1.0" encoding="utf-8" ?> 2<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" 3 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 4 xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI" 5 x:Class="BlazorApp.MainPage"> 6 7 <ScrollView> 8 <VerticalStackLayout 9 Spacing="25" 10 Padding="30,0" 11 VerticalOptions="Center"> 12 13 < n zxing:CameraBarcodeReaderView x:Name="barcodeReader" 14 WidthRequest="300" 15 HeightRequest="300" 16 IsDetecting="True" IsTorchOn="False" 17 BarcodesDetected="CameraBarcodeReaderView_BarcodesDetected"/> 18 19 <Label 20 x:Name="barcodeResult" 21 Text="Hello, .NET MAUI!" 22 SemanticProperties.HeadingLevel="Level1" 23 FontSize="32" 24 HorizontalOptions="Center" /> 25 26 <Label 27 Text="Welcome to .NET Multi-platform App UI" 28 SemanticProperties.HeadingLevel="Level2" 29 SemanticProperties.Description="Welcome to dot net Multi platform App U I" 30 FontSize="18" 31 HorizontalOptions="Center" /> 32 33 <Button 34 x:Name="CounterBtn" 35 Text="Click me" 36 SemanticProperties.Hint="Counts the number of times you click" 37 Clicked="OnCounterClicked" 38 HorizontalOptions="Center" /> 39 40 </VerticalStackLayout> 41 </ScrollView> 42 43</ContentPage> 44
MainPage.xaml.cs
C#
1namespace BlazorApp; 2 3public partial class MainPage : ContentPage 4{ 5 int count = 0; 6 7 public MainPage() 8 { 9 InitializeComponent(); 10 } 11 12 private void OnCounterClicked(object sender, EventArgs e) 13 { 14 count += 20; 15 16 if (count == 1) 17 CounterBtn.Text = $"Clicked {count} time"; 18 else 19 CounterBtn.Text = $"Clicked {count} times"; 20 21 SemanticScreenReader.Announce(CounterBtn.Text); 22 } 23 24 private void CameraBarcodeReaderView_BarcodesDetected(object sender, ZXing.Net.Maui.BarcodeDetectionEventArgs e) 25 { 26 Dispatcher.Dispatch(() => 27 { 28 barcodeResult.Text = $"{e.Results[0].Value} {e.Results[0].Format}"; 29 }); 30 } 31}
AndroidManifest.xmlには以下を追加
<uses-permission android:name="android.permission.CAMERA" />
試したこと
https://www.youtube.com/watch?v=ostgj2xB_ok
この動画を参考にしました。
補足情報(FW/ツールのバージョンなど)
visual studio community 2022