我想通过 .NET MAUI 应用程序读取一维条形码。我使用了“ZXing.Net.Maui”库。
我将组件插入到页面中
<?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="MauiApp2.CameraPage"
Title="QR Code reader">
<VerticalStackLayout>
<zxing:CameraBarcodeReaderView x:Name="cameraBarcodeReaderView"
WidthRequest="300" HeightRequest="200" Margin="20"
BarcodesDetected="BarcodesDetected" />
并初始化
public CameraPage()
{
InitializeComponent();
cameraBarcodeReaderView.Options = new BarcodeReaderOptions
{
Formats = BarcodeFormats.OneDimensional,
TryInverted = true,
AutoRotate = true
};
}
扫描条形码时,应调用以下函数。当我在 Windows 和 Android 上启动应用程序时,我可以正确看到相机图像,但从未调用条形码识别功能。我做错了什么?
private void BarcodesDetected(object sender, BarcodeDetectionEventArgs e)
{
var first = e.Results?.FirstOrDefault();
if (first != null)
return;
Dispatcher.DispatchAsync(async () =>
{
await DisplayAlert("Barcode detected", first.Value, "OK");
});
}
正如朱利安建议的那样
cameraBarcodeReaderView.Options = new BarcodeReaderOptions
{
Formats = BarcodeFormat.Code128,
AutoRotate = true,
Multiple = true
};