Estou implementando um scanner de código QR em um aplicativo MAUI, executado no .NET 8.
O aplicativo está atualmente disponível para Android e depois será portado para iOS.
Como resultado, decidi usar ZXing.Net.Maui
e seguir a documentação aqui: https://github.com/Redth/ZXing.Net.Maui#barcode-scanning
Meu problema é que os códigos QR permanecem indetectáveis e BarcodesDetected()
não estão sendo acessados se testados com um ponto de interrupção.
Estou esquecendo de alguma coisa?
Eu encontrei esta postagem ( ZXing para MAUI (MobileBarcodeScanner) ), que parece estar implementando o mesmo processo que eu, mas não obtive sucesso.
MauiProgram.cs
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
)
.UseMauiCommunityToolkit()
.UseFFImageLoading()
.UseBarcodeReader()
var app = builder.Build();
return app;
}
AndroidManifest.xaml
:
<uses-permission android:name="android.permission.CAMERA"/>
View.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"
xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI.Controls"
x:Class="Project.Views.View">
<Grid>
<zxing:CameraBarcodeReaderView
x:Name="cameraBarcodeReaderView"
BarcodesDetected="BarcodesDetected"
IsDetecting="True"
CameraLocation="Rear"
IsEnabled="True"/>
</Grid>
</ContentPage>
view.xaml.cs
:
public partial class View : ContentPage
{
public View()
{
InitializeComponent();
CheckCameraPermission();
cameraBarcodeReaderView.Options = new BarcodeReaderOptions
{
Formats = BarcodeFormats.OneDimensional,
AutoRotate = true,
Multiple = true
};
}
private void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
{
foreach (var barcode in e.Results)
Console.WriteLine($"Barcodes: {barcode.Format} -> {barcode.Value}");
}
}