ZXing Xamarin 在 IOS 上形成白色背景

ZXing Xamarin forms white background on IOS

Xamarin.Forms 的 ZXing Barcodescanner 有问题。 扫描仪在 Android 上运行完美,但在 IOS 上我看不到相机图像(预览)。如果我将条码放在相机前,扫描仪会扫描 IOS 上的条码,但相机预览只是白色背景。

我试过各种选项,但没有成功。 我们正在为 MVVM 使用 Prism.Forms。

正如我提到的,我的代码在 android 上运行良好。 以下是一些详细信息:

ScannerView.xaml

<forms:ZXingScannerPage xmlns="http://xamarin.com/schemas/2014/forms"
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                    xmlns:forms="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
                    x:Class="App.Portable.View.ScannerView">
<ContentPage.Content>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <forms:ZXingScannerView x:Name="scanner" Grid.Column="0" Grid.Row="0" HorizontalOptions="EndAndExpand" VerticalOptions="FillAndExpand"
                                IsScanning="{Binding IsScanning}"
                                IsAnalyzing="{Binding IsAnalyzing}"
                                Result="{Binding Result, Mode=TwoWay}"
                                ScanResultCommand="{Binding CmdScanResult}"
                                Options="{Binding ScannerOptions}"
        />

        <forms:ZXingDefaultOverlay Grid.Column="0" Grid.Row="0"
                                   TopText="Some title"
                                   ShowFlashButton="False"
                                   BottomText="Some bottom text"
                                   Opacity="0.9"/>
    </Grid>
</ContentPage.Content>

ScannerViewModel.cs

public class ScannerViewModel : ViewModelBase
{
    //Initializing variables

    public ScannerViewModel()
    {
        var options = new MobileBarcodeScanningOptions();
        options.TryHarder = true;
        options.InitialDelayBeforeAnalyzingFrames = 300;
        options.DelayBetweenContinuousScans = 100;
        options.DelayBetweenAnalyzingFrames = 200;
        options.AutoRotate = false;

        ScanningOptions = options;
        Title = "Barcode-Scanner";
        CmdScanResult = new DelegateCommand(OnCmdScanResult);
        IsScanning = true;
        IsAnalyzing = true;
    }

    public MobileBarcodeScanningOptions ScanningOptions
    {
        get => _scanningOptions;

        set => SetProperty(ref _scanningOptions, value);
    }

    public bool IsScanning
    {
        get => _isScanning;

        set => SetProperty(ref _isScanning, value);
    }

    public bool IsAnalyzing
    {
        get => _isAnalyzing;

        set => SetProperty(ref _isAnalyzing, value);
    }

    public Result Result
    {
        get => _result;

        set => SetProperty(ref _result, value);
    }

    public DelegateCommand CmdScanResult { get; }

    private void OnCmdScanResult()
    {
        IsAnalyzing = false;
        IsScanning = false;
        Device.BeginInvokeOnMainThread(
            async () =>
                {
                    IsAnalyzing = false;

                    var parameters = new NavigationParameters();
                    parameters.Add(CodeConstants.BARCODE, Result);
                    await NavigationService.GoBackAsync(parameters);
                });
    }
}

有没有人看到我的代码有问题,或者有一些关于如何做得更好或至少让它工作的建议?

编辑: 我将一个 Testproject 上传到我的 repo 以重现错误。扫描仪适用于 iPhone 但不显示相机预览: https://gitlab.com/mitti2000/zxingtest

原因: 您将 ZXingScannerViewZXingDefaultOverlay 放在同一个网格单元格中。然后将 ZXingScannerViewHorizontalOptions 设置为 EndAndExpand

解法:

HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"