Redth / ZXing.Net.Mobile

Barcode Scanner for Xamarin.iOS, Xamarin.Android, UWP and Tizen
MIT License
1.07k stars 703 forks source link

ZXing.Net.Forms: White background on IOS camera preview (scanning works though) #826

Closed mitti2000 closed 5 years ago

mitti2000 commented 5 years ago

Hi there

We have a problem with the ZXing Barcodescanner for Xamarin.Forms and I thought somebody here might be able to help. The scanner works perfectly on Android, but on IOS I can't see the camera image (preview). The scanner does scan barcodes on IOS if I hold them in front of the camera but the camera preview is just a white background. I tried playing around with the options but without luck. We are using Prism.Forms for MVVM.

As I mentioned, my code works well on android. Here are some details:

Here is our code (View and view model):

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);
                });
    }
}

Does anyone see an issue at my code or has some suggestions on how to do it better or at least get it to work? This is our first Xamarin.Forms app so I would really appreciate the help.

Here is a link to my repo to reproduce the problem: https://gitlab.com/mitti2000/zxingtest

Thank you so much, Regards mitti2000

mitti2000 commented 5 years ago

Through an answer from Lucas Zhang and ColeX I found the problem:

Cause: I put the ZXingScannerView and ZXingDefaultOverlay in the same cell of grid .Then I set the HorizontalOptions of ZXingScannerView as EndAndExpand .

Solution: HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"