我正在尝试使用来自 https://github.com/Redth/ZXing.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"
x:Class="myapp.Pages.ControleDesBillets"
x:DataType="viewmodel:StandardPageVM"
xmlns:viewmodel="clr-namespace:myapp.ViewModels"
xmlns:Controls="clr-namespace:myapp.Resources.Controls"
xmlns:zxing="clr-namespace:ZXing.Net.Maui.Controls;assembly=ZXing.Net.MAUI">
<Grid RowDefinitions="*" ColumnDefinitions="*">
<zxing:CameraBarcodeReaderView x:Name="cameraBarcodeReaderView" Grid.Column="0" Grid.Row="0" HeightRequest="200" WidthRequest="200"/>
</Grid>
</ContentPage>
当应用程序运行时,它会提示允许相机访问,然后显示黑屏,没有其他内容。
扫描仪在应用程序启动时初始化:
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>().UseMauiCommunityToolkit();
builder.UseBarcodeReader(); // << here
return builder.Build();
}
我正在使用物理设备进行测试。这是我的输出:
Loaded assembly: /data/data/com.companyname.myapp/files/.__override__/Xamarin.Google.Guava.ListenableFuture.dll [External]
[CameraManagerGlobal] Connecting to camera service
[CameraManagerGlobal] [soar.cts] ignore the status update of camera: 2
[CameraManagerGlobal] [soar.cts] ignore the status update of camera: 3
[CameraManagerGlobal] [soar.cts] ignore the status update of camera: 4
[CameraManagerGlobal] [soar.cts] ignore the status update of camera: 5
[CameraManagerGlobal] ignore the torch status update of camera: 3
[CameraManagerGlobal] ignore the torch status update of camera: 4
[CameraMetadataJV] setAppNameAndSensorId all [com.antutu:0,ssize,12032x9024,0,false]
[CameraMetadataJV] setAppNameAndSensorId all [com.antutu:1,ssize,5184x3880,0,false]
[CameraRepository] Added camera: 0
[Camera2CameraInfo] Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_3
[CameraRepository] Added camera: 1
[Camera2CameraInfo] Device Level: INFO_SUPPORTED_HARDWARE_LEVEL_3
[CameraValidator] Verifying camera lens facing on rain, lensFacingInteger: null
Thread finished: <Thread Pool> #2
Thread finished: <Thread Pool> #4
Le thread 0x2 s'est arrêté avec le code 0 (0x0).
Le thread 0x4 s'est arrêté avec le code 0 (0x0).
[CompatibilityChangeReporter] Compat change id reported: 150939131; UID 10378; state: ENABLED
我做错了什么?
这是一个已知问题:https://github.com/Redth/ZXing.Net.Maui/issues/7
Zxing无论是XF还是MAUI一直都是垃圾。它要么速度慢、性能低,要么存在随机问题并且几乎没有好的功能。
还有另一个基于 MLKit 的 BarcodeScanner,它与我上面提到的相反。
安装步骤在这里:https://github.com/JimmyPun610/BarcodeScanner.Mobile/wiki/3.-Installation-for-Maui
安装完毕并准备好基本设置后,您所要做的就是如下所示:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="SampleApp.Maui.Page1"
xmlns:gv="clr-namespace:BarcodeScanner.Mobile.Maui;assembly=BarcodeScanner.Mobile.Maui"
Title="Page1">
<ContentPage.Content>
<!--VirbationOnDetected: Indicate the device will vibrate or not when detected barcode, default is True
TorchOn: Indicate the torch will on or not when the view appear, default is False
IsScanning : Indicate whether the device will start scanning after it is opened, default is True
RequestedFPS: Affect Android only, remove it if you want a default value (https://developers.google.com/android/reference/com/google/android/gms/vision/CameraSource.Builder.html#public-camerasource.builder-setrequestedfps-float-fps)
ScanInterval: Scan interval for iOS, default is 500ms and the minimum is 100ms, please be reminded that double scanning may be occurred if it is too small
-->
<gv:CameraView HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" OnDetected="CameraView_OnDetected" Grid.Row="1"
TorchOn="False" VibrationOnDetected="False" ScanInterval="50" x:Name="Camera"/>
</ContentPage.Content>
</ContentPage>
你就完成了。
当我推送带有摄像头视图的页面(包裹在 NavigationPage 中)时,它会冻结。
await Shell.Current.Navigation.PushModalAsync(new NavigaionPage(new ScannerPage), false);
但是推送页面本身就可以正常工作,没有问题
await Shell.Current.Navigation.PushModalAsync(new ScannerPage(), false);
正如 FreakyAli 提到的,这个问题已被报告。
现在,如果您想坚持使用 ZXing,有一个解决方法(在链接的问题中提到):
protected override void OnNavigatedTo(NavigatedToEventArgs args)
{
RecreateCameraView();
}
private void RecreateCameraView()
{
// Create a new instance for CameraBarcodeReaderView
var newCameraView = new CameraBarcodeReaderView()
{
Options = CameraView.Options,
CameraLocation = CameraLocation.Rear
};
newCameraView.BarcodesDetected += cameraView_BarCodeDetected;
var index = CameraGrid.Children.IndexOf(CameraView);
CameraGrid.Children.Remove(CameraView);
`CameraGrid.Children.Insert(index,newCameraView);`
// Assign your new instance to CameraView.
CameraView = newCameraView;
}
在本例中,CameraGrid
是父容器。
CameraView
,嗯,是你的相机视图。
希望有帮助! 😉