Forum Xamarin.Android

ZXing Barcode Scanner does not activate Flash LED

I have the following Code to Scan Barcodes with ZXing:

           mscanner.UseCustomOverlay = false;

            MobileBarcodeScanningOptions scanoptions = new MobileBarcodeScanningOptions();

            scanoptions.TryInverted = true;
            //mscanner.ToggleTorch();

            mscanner.TopText = "Barcode Scanner";
            mscanner.Torch(true);
            mscanner.FlashButtonText = "Blitz";


            var result = await mscanner.Scan(scanoptions);

            DisplayResult(result);

I tried to activate the LED Flash of the Android Device with ToggleTorch() and with Torch(true). But the Flash Light does not turn on.

How can i activate the Flashlight in ZXing?

The required permissions are Camera and Flashlight.

Posts

  • HorstKirchbergHorstKirchberg USMember ✭✭

    Does nobody use the ZXing Barcode Component?

  • Johan_SchoemanJohan_Schoeman ZAMember ✭✭

    I'm trying to get it to work on XF iOS...When I have some success I post here to share.

  • YkshLeoYkshLeo USMember ✭✭✭✭

    Hi @HorstKirchberg & @Johan_Schoeman ,

    Please go through this answer by @Redth at stackoverflow : http://stackoverflow.com/a/17367507/3891036.

    It may help you :blush: .

  • anandjitbanerjee.5636anandjitbanerjee.5636 USUniversity ✭✭

    Any workable solution?

  • MichaelKayMichaelKay GBMember ✭✭

    Yes, i got both flash and auto focus working in the latest version of ZXing, my code look like this:

    async public Task<string> Scanner(bool flashOn)
        {
            var scanner = new MobileBarcodeScanner();
            scanner.BottomText = "Ensure the barcode is upright and inside the viewfinder.";
    
            ZXing.Result result = null;
    
            TimeSpan ts = new TimeSpan(0, 0, 0, 1, 0);
            Device.StartTimer(ts, () =>
            {
                if (result == null)
                {
                    scanner.AutoFocus();
                    if (flashOn)
                    {
                        scanner.Torch(true);
                    }
                    return true;
                }
                return false;
            });
    
            result = await scanner.Scan();
    
            if (result != null)
            {
                return result.Text;
            }
    
            return string.Empty;
        }
    

    I use a Switch toggle to control the flash light before scanning. Just remove the if statement and the boolean constructor if you want the flash on at all times. I guess one could extend the button control as an overlay on the camera. But this also gets the job done.

    Remember to enable permission:
    [assembly: Android.App.UsesPermission(Android.Manifest.Permission.Flashlight)]

    Thanks to @MichaelVowles and @TonyCelia from https://forums.xamarin.com/discussion/comment/218638 that assist in coming up with above solution.

  • MichaelKayMichaelKay GBMember ✭✭

    I found there was a slight bug with my code above when doing a more detailed testing. Thanks for the flash light i was using that made me spot this issue otherwise you wouldn't know.

    The issue was with Device.StartTimer. Once i switch the flash (Torch) to true it works fine. When i then switch it off (using my Switch control) then try to scan again, the light still comes back on. It turns out that once you start timer it never stop, even when you cancel scan. Even though i explicitly put return false within the timer. I found out by putting a break point just after Device.StartTimer(ts, () =, it kept being hit even after the scan has stopped or cancelled.

    To solve this you need to ensure you stop Device.StartTimer on each scan or when you've cancelled - you should be doing this even if you're not using the touch:

    private CancellationTokenSource cancelTimer;
    
    async public Task<string> Scanner(bool flashOn)
        {
            this.cancelTimer = new CancellationTokenSource();
    
            var scanner = new MobileBarcodeScanner();
            scanner.BottomText = "Ensure the barcode is upright and inside the viewfinder.";
    
            ZXing.Result result = null;
    
            CancellationTokenSource cts = this.cancelTimer;
            TimeSpan ts = new TimeSpan(0, 0, 0, 2, 0);
            Device.StartTimer(ts, () =>
            {
                if (cts.IsCancellationRequested)
                {
                    return false;
                }
    
                if (result == null)
                {
                    scanner.AutoFocus();
                    if (flashOn)
                    {
                        scanner.Torch(true);
                    }
                    return true;
                }
                return false;
            });
    
            result = await scanner.Scan();
    
            if (result != null)
            {
                await Stop();
                return result.Text;
            }
    
            await Stop();
            return string.Empty;
        }
    
    async private Task Stop()
        {
            await Task.Run(() => { Interlocked.Exchange(ref this.cancelTimer, new CancellationTokenSource()).Cancel(); });
        }
    

    Many thanks to @ylemsoul for the pointer at https://forums.xamarin.com/discussion/49492/how-can-i-stop-device-starttimer.

  • MomenMagdiMomenMagdi USMember

    @Redth in my zxing barcode scanner it's too slow at scan any help

  • TonyCeliaTonyCelia USMember ✭✭

    I've created a custom ZXing ScannerView in Xamarin Forms and ZXing.Net.Mobile.Forms 2.4.1 for an updated app that runs on both Android and iOS. I'm using @MichaelKay's AutoFocus timer implementation from above in the OnAppearing() callback of the ScannerView. Has anyone else implemented a barcode scanner using ZXing.Net.Mobile.Forms 2.4.1 with a custom ScannerView and if so:

    1. Did you need an autofocus "thread" (to trigger ZXing.AutoFocus until scan complete) for Andorid and iOS?
    2. If you needed an autofocus thread, did you start your AutoFocus thread in OnAppear()? If not, where?

    Thank you for any help/advice.

Sign In or Register to comment.