Closed Mike-tech closed 4 years ago
Hi, I had the same problem as you describe it. Problem is when you're using AutoFocus() it is not actully option. This method just tries autofocus camera when its called.
So used solution from https://forums.xamarin.com/discussion/72077/zxing-barcode-reader-autofocus
Make Thread, that until scanning is finished, calls in intervals AutoFocus() on scanner component. You have to find optional interval. if it will be too fast, autofocus isn't able to focus in time that next Autofocus is called -> and over and over. Its not clean solution, but its working for me.
Thanks. Yes that seems to be a workaround for now. I hope they have this resolve in a next release. Glad to know someone else experience this issue.
+1 also is not working properly for me. It seems like it tries to focus once and then remains at that focal point even if the barcode is moved further away.
... and the workaround was? (link is dead)
I have big problems too. Tried on p9, s7 and g3.
I think I found the cause for this issue. The detection of if AutoFocus is supported depends on an instance of ScanPage. When this is called before the scanning starts, the ScanPage does not exist. You cannot call it properly afterwards from code either, because your code won't get there before the first barcode is detected.
I solved it by simply inserting:
AutoFocus();
... just after the rootFrame.Navigate in the Scan and ScanContinuous functions in MobileBarcodeScanner.cs.
I tried this on both a camera that supports AutoFocus and one that doesn't on regular Windows 10 machines and this solves the issue there. It may well be that the issue is similar on other platforms?
EDIT: and then it stopped working again despite these changes ... hmmm. It looks like it's only after the first time I start the scanning process that AutoFocus starts working.
EDIT:
found this: && mediaCapture.VideoDeviceController.FocusControl != null && mediaCapture.VideoDeviceController.FocusControl.Supported;
but this should be:
&& mediaCapture.VideoDeviceController.Focus != null
&& mediaCapture.VideoDeviceController.Focus.Capabilities.Supported;
When stepping through I can see that's what reports the correct values. That means other references to FocusControl are probably incorrect too.
EDIT: no, I can't figure it out, sorry. The combination of the above did seem to enable auto-focus, but it works badly. I also set this up using UWP native barcode detection and that works great on this auto-focus camera, but not at all on non-auto-focus cameras. Weird stuff.
I guess MikyCrazy is probably right.
EDIT: the async autofocus that is setup in here though happens before the media is present. So that's probably why it fails. Then later autofocus is configured after the media is present, but then no async scanning is repeated. Or something. It's still not entirely clear.
Experiencing same issue on the Galaxy S7 I see some people have said they created a thread to call AutoFocus manually. I didnt think it was possible to use Thread in PCL projects?
For anyone using Xamarin Forms the following works really well
ZXingScannerView zxing;
...
TimeSpan ts = new TimeSpan(0, 0, 0, 3, 0);
Device.StartTimer(ts, () => {
if (zxing.IsScanning)
zxing.AutoFocus();
return true;
});
As Mikycrazy said, AutoFocus() it is not actually an option. It just tries to autofocus the camera when its called.
The camera should focus automatically if the device supports it, but versions from 2.1.12 to 2.1.47 have a bug (#395) that prevents it for some devices.
When do the developer(s) post new updates? I use the ZXing.Net.Mobile in my Xamarin android application.
When I watch the version in the xamarin store it is 2.1.12 (July 2016). Nugget version is 2.1.47 (August 2016).
I see new commits but when to they release a new version?
Thanks to @Mika571 for the pointer. Implementing a 1 sec interval makes scanning super quick on my device. I'm also controlling flash light within this scope.
It will be nice to get an acknowledgement from ZXing if this is a known issue or not. And what the official method of calling the Auto focus so i can close this issue.
`async public Task
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 found there was issue with the code above. This is specific to Android implementation. Device.StartTimer never stop even after the scan had stopped or cancelled. The solution was to stop DeviceStartTimer. This was the solution to resolve it:
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(); });
}
For more explanation see my comment at https://forums.xamarin.com/discussion/comment/245684/#Comment_245684
Hello I am not sure about Xamarin android , but I am using this in xamarin iOS and it is working perfectly fine for me, kind of mimic the TPL way as TPL won't work correctly with async tasks so here is my solution
scanner.UseCustomOverlay = false; //options MobileBarcodeScanningOptions options = new MobileBarcodeScanningOptions() {AutoRotate = false,TryHarder = false}; // bug is in zxing current library for auto focus not working, patch for auto focus var scanTask = scanner.Scan(options,true).ContinueWith((arg) => { Console.WriteLine("ScanTask and autofocus should be finsihed"); HandleScanResult(arg.Result); }); var autoFocus = new Task(async () => { while (!scanTask.IsCompleted) { await Task.Delay(1000); scanner.AutoFocus(); }); // this will do the magic autoFocus.Start(); await Task.WhenAll(scanTask, autoFocus);
Thanks for reporting this issue! Unforunately it took me way too long to respond ðŸ˜. Sorry, I apologize! Recently the source code for this project was completely refactored to modernize it. Many PR's were included in this effort, and many bugs were hopefully fixed. Please try out the latest 3.x series of NuGet packages (currently in prerelease). To try and make the project more maintainable in my spare time going forward, I've decided to close all existing issues to start with a clean slate. If you're still experiencing this issue on the newest version, please open a new issue with as much detail as possible. Thank you for your patience and understanding! Happy scanning!
Hello I am not sure about Xamarin android , but I am using this in xamarin iOS and it is working perfectly fine for me, kind of mimic the TPL way as TPL won't work correctly with async tasks so here is my solution
scanner.UseCustomOverlay = false; //options MobileBarcodeScanningOptions options = new MobileBarcodeScanningOptions() {AutoRotate = false,TryHarder = false}; // bug is in zxing current library for auto focus not working, patch for auto focus var scanTask = scanner.Scan(options,true).ContinueWith((arg) => { Console.WriteLine("ScanTask and autofocus should be finsihed"); HandleScanResult(arg.Result); }); var autoFocus = new Task(async () => { while (!scanTask.IsCompleted) { await Task.Delay(1000); scanner.AutoFocus(); }); // this will do the magic autoFocus.Start(); await Task.WhenAll(scanTask, autoFocus);
The result is amazing. but sometimes it crashes..
Java.Lang.RuntimeException: 'Camera is being used after Camera.release() was called' i dont know, why it happens...
Hi there
I recently updated my ZXing.Net.Mobile and ZXing.Net.Mobile.Forms to v2.1.47 in my Xamarin project. Version was downloed using Nuget. Since doing this the auto focus stop working as expected. It doesn't do anything when you moved the camera towards a barcode.
The only way to make it work in this new version is to ensure the camera is locked facing the barcode when you initiate the camera, then the auto focus works.
I even setup a brand new vanilla Xamarin project and installed this version and the problem exists. The workaround in the link below seems to make the auto focus works as expected. But i rather not use this work around: https://forums.xamarin.com/discussion/72077/zxing-barcode-reader-autofocus
I am using a Samsung Galaxy S7 and i've added all camera permission and used the AutoFocus() option with no success. I Is this a known issue or something i haven't done?
Thanks in advance