Search code examples
xamarinxamarin.formszxingflashlight

ZXing ZXingScannerPage: Torch / Flashlight not working (Xamarin.Forms)


I want to activate the torch/flashlight by starting the scanner page, but its just not working. No error message.

<?xml version="1.0" encoding="utf-8" ?>
<zxing:ZXingScannerPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:zxing="clr-namespace:ZXing.Net.Mobile.Forms;assembly=ZXing.Net.Mobile.Forms"
             x:Class="de.scanner.Pages.ScanPage"
             NavigationPage.HasNavigationBar="True"
             NavigationPage.HasBackButton="True"
             OnScanResult="Handle_OnScanResult"
             IsScanning="False"
             IsTorchOn="True">
</zxing:ZXingScannerPage>

AndroidManifest:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

From another page by using FreshMvvm:

await this.CoreMethods.PushPageModel<ScanPageModel>();

Solution

  • You could use ZXingScannerView instead and use a button to turn on the torches.

    <Button  BackgroundColor="Chocolate" Clicked="Button_Clicked"/>
        <zxing:ZXingScannerView    x:Name="_scanView" OnScanResult="Handle_OnScanResult" IsScanning="true"
                                WidthRequest="200" HeightRequest="200" />
    
     private void Button_Clicked(object sender, EventArgs e)
        {
            _scanView.IsTorchOn = true;
        }