【问题标题】:ZXing Xamarin forms white background on IOSZXing Xamarin在IOS上形成白色背景
【发布时间】:2019-07-16 05:28:36
【问题描述】:

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

我尝试使用这些选项,但没有运气。 我们正在为 MVVM 使用 Prism.Forms。

正如我所提到的,我的代码在 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

【问题讨论】:

  • 虽然它在 Android 上运行良好。我不认为原因是你的代码。你在 info.plist 中添加了隐私吗?
  • @LucasZhang-MSFT 感谢您的评论。我不确定你所说的“隐私”是什么意思。我的 info.plist NSCameraUsageDescriptionBarcode-Scanner 所需的相机权限 中有这个
  • 您能否分享包含问题的样本,以便我可以在我身边进行测试。别忘了删除您的个人信息。
  • 我需要一些时间,但我会创建一个您可以测试的示例。
  • 您是否initialize iOS 特定代码中的插件?在您的 AppDelegate 的 FinishedLaunching (..) 实现中,调用:ZXing.Net.Mobile.Forms.iOS.Platform.Init();

标签: c# ios xamarin.forms barcode-scanner zxing.net


【解决方案1】:

原因:你把ZXingScannerViewZXingDefaultOverlay放在grid的同一个单元格中。然后你设置了HorizontalOptions ZXingScannerViewEndAndExpand

解决方案:

HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 1970-01-01
    • 2018-04-19
    • 2011-12-30
    • 2017-11-24
    • 1970-01-01
    • 2017-02-06
    相关资源
    最近更新 更多