Redth / ZXing.Net.Mobile

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

Zxing Xamarin-forms Autofocus #976

Open Souhail-maatouk opened 3 years ago

Souhail-maatouk commented 3 years ago

I am using Zxing to scan barcode in Xamarin forms App , The problem is Autofocus works only once when the scanpage Appeared , I tried to add timer as below Code also not worked , and I noted when I change the application and return to it (in scan page ) Auto focus works perfect , I also added Button to Autofocus to overlay , it's fired but camera didn't focus any advise ? scanPage.AutoFocus(); TimeSpan ts = new TimeSpan(0, 0, 0, 10, 0); Device.StartTimer(ts, () => { if (scanPage.IsScanning) { scanPage.AutoFocus(); } return true; });

josipduvnjak commented 3 years ago

Try using Focus() before AutoFocus()

kevintw86 commented 3 years ago

Have the same issue. AutoFocus does not work until the user taps the screen (as you commonly do while trying to switch focus on some object in camera view).

The log as follow:

[0:] AutoFocus_Timer.Elapsed... [ZXing.Net.Mobile] AutoFocus Requested [0:] ScannerView forced to AutoFocus... [ZXing.Net.Mobile] AutoFocus Failed

Tried to ZXingScannerView.Focus() before AutoFocus(), but this also does not help.

duncancole commented 3 years ago

I've tried a similar solution as the OP and I'm also having no luck. I can see in this in the output logs: 07-13 08:49:17.202 D/ZXing.Net.Mobile( 6996): AutoFocus Requested 07-13 08:49:17.207 D/ZXing.Net.Mobile( 6996): AutoFocus Failed but no more detail that would help solve this issue

duncancole commented 3 years ago

Looking into the code for the ZXingSurfaceView on Android and I can see some gestures also trigger the Autofocus. If I use these (Swipe up/down) the Autofocus works so with that in mind instead of just calling Autofocus() in my timer I called Autofocus(x,y) with the coordinates for the center of the screen. This works and I see AutoFocus Succeeded in the logs.

volkan-akkus-tr commented 2 years ago

Maybe, don't use a ScannerPage. Use their MobileBarcodeScanner. As Igor Kravchenko stated at https://stackoverflow.com/a/66296505/20395535

ZXing.Mobile.MobileBarcodeScanner scanner = new();
ZXing.Result result = await scanner.Scan().ConfigureAwait(false);

if (result != null && !string.IsNullOrWhiteSpace(result.Text))
{
    // result.Text is ready.
}

Kudos to Igor.