0

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

我试着玩弄这些选项,但没有运气。我们将 Prism.Forms 用于 MVVM。

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

  • 在两个平台上都正确设置了权限。
  • NuGets ZXing.Net.Mobile 和 ZXing.Net.Mobile.Forms 也添加了所有三个项目(Android、IOS 和便携式)
  • 我们正在使用 .NET Standard 2.0
  • Xamarin.Forms 是 3.4.0 版

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

4

1 回答 1

0

原因:您将ZXingScannerViewZXingDefaultOverlay放在 grid的同一单元格中。然后将ZXingScannerView设置HorizontalOptions为 。EndAndExpand

解决方案:

HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
于 2019-07-16T14:11:25.797 回答