I am trying to use a ZXing Maui control to read a barcode using a mobile’s camera. It is activating the camera correctly but it is not reading the QR Code. My code is as follows:
using Avalonia.Controls;
using Avalonia.Maui.Controls;
using ZXing.Net.Maui;
using ZXing.Net.Maui.Controls;
namespace QRPayment
{
public partial class QRPaymentView : UserControl
{
private string barcode;
public QRPaymentView()
{
InitializeComponent();
CameraBarcodeReaderView cameraBarcodeReaderView = (CameraBarcodeReaderView)this.Get<MauiControlHost>("CameraReader").Content!;
cameraBarcodeReaderView.Options = new BarcodeReaderOptions
{
Formats = BarcodeFormats.OneDimensional,AutoRotate = true, Multiple = false
};
cameraBarcodeReaderView.CameraLocation = cameraBarcodeReaderView.CameraLocation == CameraLocation.Rear ? CameraLocation.Rear : CameraLocation.Front;
}
protected void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
{
BarcodeResult res = e.Results[0];
barcode = res.Value;
}
}
}
The XAML file is as follows:
<UserControl
x:Class="Screens.QRPayment.QRPaymentView"
xmlns="https://github.com/avaloniaui"
xmlns:controls="using:Avalonia.Maui.Controls"
xmlns:mauiControls="using:Microsoft.Maui.Controls"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Screens.ZimQRPayment"
xmlns:zxing="using:ZXing.Net.Maui.Controls"
d:DesignHeight="450"
d:DesignWidth="400"
x:DataType="vm:QRPaymentViewModel"
mc:Ignorable="d">
<DockPanel
Margin="2"
LastChildFill="True"
RenderOptions.BitmapInterpolationMode="HighQuality">
<StackPanel DockPanel.Dock="Bottom">
<Button
Name="Cancel"
Height="50"
Margin="5"
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Background="Red"
Command="{Binding CancelCommand}"
CornerRadius="10"
Foreground="White">
Cancel
</Button>
</StackPanel>
<StackPanel>
<StackPanel Margin="5">
<controls:MauiControlHost x:Name="CameraReader" Width="300" Height="400">
<zxing:CameraBarcodeReaderView
BarcodesDetected="BarcodesDetected" />
</controls:MauiControlHost>
</StackPanel>
<StackPanel Margin="5">
<TextBlock
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="PLease Scan QR Code" />
</StackPanel>
</StackPanel>
</DockPanel>
</UserControl>
The event for a detected barcode (BarcodesDetected) is never called. Or the control is not detecting the barcode.
What am I missing here?
New contributor
user24981182 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.