Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Camera Focus Issue #395

Closed
moflaherty opened this issue Aug 15, 2016 · 20 comments
Closed

Camera Focus Issue #395

moflaherty opened this issue Aug 15, 2016 · 20 comments
Assignees
Milestone

Comments

@moflaherty
Copy link

Hi! We updated to 2.1.13 in our Xamarin Forms/PCL project and now the camera does not auto focus. Sometimes exiting the scan form and going back in causes it to focus properly. We did not experience this issue with 2.0.4.46. We have tested this on a few devices--mine is a Moto X Gen 2. Any idea what is going on? I can go back to the older version, but that also fixes other issues that caused us grief. Thanks!

@edynamic90
Copy link

Hello, I'm not developper of this great project but I've a little bit information

With the last version 2.1.44, no focus, the preview is blurry

After flash activated and wait 3-5 seconds, focus is triggered and the preview is OK

But it's only after flashlight using, it's a bug

Best regards

@Redth
Copy link
Owner

Redth commented Aug 22, 2016

The auto focus code was changed in this release. I suspect some devices don't do a good job of implementing proper auto focus. You can of course manually invoke AutoFocus ()...

I will test on more of my devices and see if I can find one that has these issues....

@Redth Redth added the bug label Aug 22, 2016
@Redth Redth added this to the 2.2.x milestone Aug 22, 2016
@Redth Redth self-assigned this Aug 22, 2016
@moflaherty
Copy link
Author

Redth, I did make a call to auto focus but that did not seem to help. However, I may not be using the method properly. We have two scenarios in our Xamarin Forms/PCL app: (1) we are using continuous scanning with an overlay, and (2) we acquire a single scan and immediately return. In either scenario, when should the auto focus call be made? I have noticed that when the camera is within range of a valid barcode, it will focus enough to capture the barcode. If the barcode is not in range, it is hit or miss if the camera focuses (sometimes it works.) However, if I hit the back button and then go back in, it seems to focus every time. Not sure if that helps.

@edynamic90
Copy link

edynamic90 commented Aug 24, 2016

Thanks for the answer

I just add this code before start scan to resolve this issue
//--- BEFORE ---
new Task(async () =>
{
await Task.Delay(3000);
oScanner.AutoFocus();
}).Start();
var oResult = await oScanner.Scan (oOptions);
ReadScanResult (oResult);

@Redth
Copy link
Owner

Redth commented Aug 24, 2016

So, previous to 2.1.44 auto focus was done by calling .AutoFocus() on a timer (every few seconds). You could achieve the same pretty easily, but that's obviously not a great solution.

I'm going to to dig into the code a bit more and see if i can detect when a camera doesn't support autofocus and revert back to the 'old' way of calling AutoFocus on a timer. First I need to go through my devices and see if I can find one that doesn't support autofocusing properly...

@bmacadam
Copy link

I'm experiencing exactly what moflaherty and edynamic90 have already described. I'm testing with a Samsung Galaxy S3 (SGH i747) running 4.4 and a HTC One running 5.0.2. I have a client with a Nexus 5x running 6.0.2 who reported the same issue. All of these devices support autofocus.

I improved the situation by modifying the call to AutoFocus within the SetupCamera method in ZXingSurfaceView. I wrapped in a timer with a delay of 800 ms.

@rocuh
Copy link

rocuh commented Aug 27, 2016

Moto G2 and G3 also don't auto focus

@liveoset
Copy link

LG G4 the same problem

@DanielMrowca
Copy link

DanielMrowca commented Oct 3, 2016

        ZXing.Net.Mobile.Forms.Android.Platform.Init();
        SetContentView(Resource.Layout.BarcodeFragment);

        scanFragment = new ZXingScannerFragment();


        SupportFragmentManager.BeginTransaction()
            .Replace(Resource.Id.fragment_container, scanFragment)
            .Commit();

    }

    protected override void OnResume()
    {
        base.OnResume();
        scan(); 

    }

    protected override void OnPause()
    {
        scanFragment.StopScanning();
        base.OnPause();
    }

    void scan()
    {
        var opts = new MobileBarcodeScanningOptions
        {
            AutoRotate = false,
            UseFrontCameraIfAvailable = true,
            TryHarder = true
        };


        scanFragment.StartScanning(result =>
        {
            if (result != null)
            {
                Intent myIntent = new Intent(this, typeof(MainActivity));
                myIntent.PutExtra("code", result.Text);
                SetResult(Result.Ok, myIntent);
                Finish();
            }
        }, opts);
    }

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
    {
        global::ZXing.Net.Mobile.Forms.Android.PermissionsHandler.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

After update to last stable version (2.1.47) auto-focus on continous scanning not working. Using .AutoFocus() method is not good idea. Tested on Samsung SM-A510F (Android API 23). Previous versions (before 2.1) was working very well

@kyluke
Copy link

kyluke commented Oct 12, 2016

J1 Mini is also facing the same problem.

@Mika571
Copy link

Mika571 commented Nov 13, 2016

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;
});

@andrekiba
Copy link

Hi, any news about this problem? The timer solution for trigger autofocus has some problems related to different threads. I also have to call Stop (or cancel the tcs) in UI thread?

@ptsiogas
Copy link

ptsiogas commented Mar 3, 2017

I can confirm that in version (2.1.47) auto-focus on continuous scanning is not working. Using the .AutoFocus on a timer seems to make a minor improvement. Is it suggested to use a version prior to 2.1 until the issue is fixed?

@spurdow
Copy link

spurdow commented Oct 19, 2017

I can confirm this issue on Samsung J5 Prime, Sometimes preview autofocus works but when user tap the screen it blurred.

@dsuarezv
Copy link

dsuarezv commented Nov 17, 2017

I can confirm this on nexus 5x. Any idea what code introduced this problem?

Update: seems to be working fine on master. The problem I had was in 2.1 xamarin component.

Update 2: It's also working with latest nuget package (2.3.2). It seems that my mistake was to use the outdated xamarin component, still on version 2.1. The focus mode set on nexus 5x is FocusModeContinuousPicture (on the CameraController.ApplyCameraSettings method).

@gaborremenyi
Copy link

gaborremenyi commented Nov 20, 2017

I'm also having auto focus issues on Samsung Galaxy Xcover 3 (G389F) using Xamarin Forms 2.3.4.270 and ZXing.Net.Mobile 2.3.2. As some people already suggested, adding a timer that calls AutoFocus periodically helps somewhat.

On the debub console I keep getting these messages:
11-20 09:22:14.746 D/ZXing.Net.Mobile(14566): AutoFocus Requested
11-20 09:22:15.636 D/ZXing.Net.Mobile(14566): AutoFocus Failed

(I didn't have any issues on Sony Xperia Z2, Samsung Galaxy S5 and Moto G5.)

Update: I managed to fix the auto focus issue for the above mentioned phone. I've been doing some digging in ZXing.Net.Mobile.Android to see how the camera is set up and noticed that it sets the preview FPS in CameraController.cs using the following logic:

// This will make sure we select a range with the lowest minimum FPS
// and maximum FPS which still has the lowest minimum
// This should help maximize performance / support for hardware
foreach (var fpsRange in parameters.SupportedPreviewFpsRange)
{
     if (fpsRange[0] <= selectedFps[0] && fpsRange[1] > selectedFps[1])
          selectedFps = fpsRange;
}
parameters.SetPreviewFpsRange(selectedFps[0], selectedFps[1]);

This logic was selecting the lowest FPS rate supported by the phone:
11-22 10:11:24.443 D/ZXing.Net.Mobile(26006): Supported FPS: min 7000, max 7000
11-22 10:11:24.443 D/ZXing.Net.Mobile(26006): Supported FPS: min 15000, max 15000
11-22 10:11:24.443 D/ZXing.Net.Mobile(26006): Supported FPS: min 15000, max 30000
11-22 10:11:24.443 D/ZXing.Net.Mobile(26006): Supported FPS: min 30000, max 30000
11-22 10:11:24.443 D/ZXing.Net.Mobile(26006): Selected FPS: min 7000, max 7000

Setting the FPS explicitly to the maximum supported value (30fps) seems to be the solution for me:
parameters.SetPreviewFpsRange(30000, 30000);

Now the camera preview on the phone is much more responsive (before it was sluggish) and the auto focus works like a charm. I do believe that the logic for selecting the FPS rate is flawed and should not be applied to every phone automatically. Alternatively it would be nice to have to ability to overwrite this setting through the MobileBarcodeScanningOptions, just like you can for the resolution.

Update 2:
I think the way it should pick the FPS is picking the highest FPS possible which still has the lowest minimum.

foreach (var fpsRange in parameters.SupportedPreviewFpsRange)
{
     if (fpsRange[1] > selectedFps[1] || fpsRange[1] == selectedFps[1] && fpsRange[0] < selectedFps[0])
          selectedFps = fpsRange;
}
parameters.SetPreviewFpsRange(selectedFps[0], selectedFps[1]);

@Strifex
Copy link
Contributor

Strifex commented Mar 15, 2020

@Redth now that you're working on 3.0+ would you consider re-lookong at this? The issues with the preview framerate being arbitrarily set to maximum on some devices is the reason my team had to abandon this projects stable releases a few years back since it never got solved - would love to move back to using your 3.0+ versions if this is resolved! :)

@Redth Redth added the keep label Mar 15, 2020
@Redth Redth added this to the 3.0 milestone Mar 15, 2020
@Redth
Copy link
Owner

Redth commented Mar 15, 2020

Yes!

@Redth
Copy link
Owner

Redth commented Mar 16, 2020

@Strifex i'm going to call this one fixed. If you look in the source, I did actually merge or incorporate better logic around this some time ago, so it's part of the 3.0 beta releases already: https://github.com/Redth/ZXing.Net.Mobile/blob/master/ZXing.Net.Mobile/Android/CameraAccess/CameraController.android.cs#L245-L253

I just stepped through it again, and given my phone which supports: { { 15000, 15000}, {15000, 30000}, {30000, 30000} } it successfully picks {15000, 30000} to use.

@Redth Redth closed this as completed Mar 16, 2020
@MStrannik
Copy link

Unfortunately, a bug with autofocus was not fixed in version 3.0.1

I use ZXing.Net.Mobile.Forms v.3.0.1
Xamarin.Forms 5.0.0.2012
Samsung SM-G925F(API24)

II tried to use a workaround, but autofocus steel does not work.

ZXingScannerView zxing;
...
TimeSpan ts = new TimeSpan(0, 0, 0, 3, 0);
Device.StartTimer(ts, () => {
if (zxing.IsScanning)
zxing.AutoFocus();
return true;
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests