Search code examples
c#xamarinxamarin.formszxingzxing.net

Xamarin Forms - Zxing QR Scanner - How can you toggle the camera being used?


When I start scanning for barcodes using a ScannerView in Xamarian forms, it automatically goes to the back camera on the phone.

I would like to make a toggle camera button that toggles the camera from back to front and vice versa.

Is this possible using zxing for xamarin forms?

My options look like this:

code:

//Set the scanner options.
ScannerView.Options = new ZXing.Mobile.MobileBarcodeScanningOptions()
{
    UseNativeScanning = true,
    AutoRotate = true,
    PossibleFormats = new List<ZXing.BarcodeFormat>()
    {
        ZXing.BarcodeFormat.QR_CODE
    },
    TryHarder = true,
    UseFrontCameraIfAvailable = false,
    DelayBetweenContinuousScans = 2000
};

Once I start scanning by setting

ScannerView.IsScanning = true;

Setting:

ScannerView.Options.UseFrontCameraIfAvailable = true;

Does NOT change the camera. I can however do this

ScannerView = null;
CreateScannerView();
ScannerView.Options.UseFrontCameraIfAvailable = true;
ScannerView.IsScanning = true;

And it works. However the camera view completely disappears for about 2 seconds and then comes back. When I did it native Xcode for iOS for another app, the camera view never went away and just flipped cameras. I'm expecting something like that.

Is switching the camera possible?


Solution

  • I don't think switching between cameras itself is possible with Zxing once it has started scanning so the option has to be chosen and set beforehand.

     var options = new MobileBarcodeScanningOptions
     {
         AutoRotate = true,
         UseNativeScanning = true,
         TryHarder = true,
         TryInverted = true,
         UseFrontCameraIfAvailable  = true
     };
    
     var scannedCode = await _scanner.Scan(options);