Camera focus in Vuforia (Android)

So we have a total of three Android devices that we’re testing with. One of them, a Galaxy S8, has the camera out of focus and refuses to focus properly. The other two are working fine, including a Galaxy S9+ and a Samsung Galaxy Tab A 10.1. I added a camera focus fix, just to make sure the camera was in autofocus mode, but it didn’t help. I saw that in some other forum posts. Here’s the code that I used for that:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class CameraFocusFix : MonoBehaviour {

    private bool             vuforiaStarted;


    private void Start() {

        if (VuforiaARController.Instance != null)
            VuforiaARController.Instance.RegisterVuforiaStartedCallback(fixFocus);

    }

    private void fixFocus() {

        vuforiaStarted = true;
        setAF();

    }

    private void onAppPause(bool paused) {

        if (!paused) {

            if (vuforiaStarted) {
                setAF();
            }

        }

    }

    private void setAF() {

        Debug.Log("Fixing focus");
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);

    }

}

The debug statement on line 41 shows up, so the code is running, but the one device is still out of focus. The camera works fine when using the built-in camera app - just not in AR. We have another app that works perfectly on all three devices, so I also tried making all of the player settings and Vuforia settings identical. That didn’t do it either. And yes, we tried restarting the phone, too. It’s the same APK on all three devices.

Can anyone help?

2 Likes

I am also not able to get this working on Android Galaxy phones. This script works for other Android devices but I can not get it working on Galaxy. Where you able to solve this?

using UnityEngine;
using System.Collections;
using Vuforia;

public class CameraFocusControllerAndriod : MonoBehaviour
{

    private bool mVuforiaStarted = false;

    void Start()
    {
        VuforiaARController vuforia = VuforiaARController.Instance;

        if (vuforia != null)
            vuforia.RegisterVuforiaStartedCallback(StartAfterVuforia);
    }

    private void StartAfterVuforia()
    {
        mVuforiaStarted = true;
        SetAutofocus();
    }

    void OnApplicationPause(bool pause)
    {
        if (!pause)
        {
            // App resumed
            if (mVuforiaStarted)
            {
                // App resumed and vuforia already started
                // but lets start it again...
                SetAutofocus(); // This is done because some android devices lose the auto focus after resume
                // this was a bug in vuforia 4 and 5. I haven't checked 6, but the code is harmless anyway
            }
        }
    }

    private void SetAutofocus()
    {
        if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO))
        {
            Debug.Log("Autofocus set");
        }
        else
        {
            // never actually seen a device that doesn't support this, but just in case
            Debug.Log("this device doesn't support auto focus");
        }
    }
}

Hey, sorry this is a few years late but to anyone still looking I was able to get it to work by calling the focus method once per second

using Vuforia;

public class AutoFocusCam : MonoBehaviour
{
    void Start()
    {
        Focus();
    }

    void Focus()
    { 
        CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
        Invoke("Focus", 1);
    }
}
1 Like