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

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:

  • The permissions are properly set on both platforms.
  • The NuGets ZXing.Net.Mobile and ZXing.Net.Mobile.Forms are added too all three projects (Android, IOS and portable)
  • We are using .NET Standard 2.0
  • Xamarin.Forms is version 3.4.0

Here is our code (View and view model):

ScannerView.xaml

<forms:ZXingScannerPage xmlns="xamarin,com/schemas/2014/forms"
                    xmlns:x="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.

Thank you so much,
Regards
mitti2000

Best Answer

Answers

  • mitti2000mitti2000 Member

    Here is a link to a repo to reproduce the problem: gitlab(dot)com/mitti2000/zxingtest (not allowed to post links yet)

  • mitti2000mitti2000 Member
    That worked!! Thanks so much!!!
Sign In or Register to comment.