Search Unity

Camera focus in Vuforia (Android)

Discussion in 'AR/VR (XR) Discussion' started by VCC_Geek, Sep 18, 2018.

  1. VCC_Geek

    VCC_Geek

    Joined:
    Apr 30, 2013
    Posts:
    29
    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:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Vuforia;
    6.  
    7. public class CameraFocusFix : MonoBehaviour {
    8.  
    9.     private bool             vuforiaStarted;
    10.  
    11.  
    12.     private void Start() {
    13.  
    14.         if (VuforiaARController.Instance != null)
    15.             VuforiaARController.Instance.RegisterVuforiaStartedCallback(fixFocus);
    16.  
    17.     }
    18.  
    19.     private void fixFocus() {
    20.  
    21.         vuforiaStarted = true;
    22.         setAF();
    23.  
    24.     }
    25.  
    26.     private void onAppPause(bool paused) {
    27.  
    28.         if (!paused) {
    29.  
    30.             if (vuforiaStarted) {
    31.                 setAF();
    32.             }
    33.  
    34.         }
    35.  
    36.     }
    37.  
    38.     private void setAF() {
    39.  
    40.         Debug.Log("Fixing focus");
    41.         CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    42.  
    43.     }
    44.  
    45. }
    46.  
    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?
     
    Last edited: Sep 19, 2018
    mtessmann and kvanderd like this.
  2. kvanderd

    kvanderd

    Joined:
    Dec 23, 2016
    Posts:
    1
    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?



    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using Vuforia;
    4.  
    5. public class CameraFocusControllerAndriod : MonoBehaviour
    6. {
    7.  
    8.     private bool mVuforiaStarted = false;
    9.  
    10.     void Start()
    11.     {
    12.         VuforiaARController vuforia = VuforiaARController.Instance;
    13.  
    14.         if (vuforia != null)
    15.             vuforia.RegisterVuforiaStartedCallback(StartAfterVuforia);
    16.     }
    17.  
    18.     private void StartAfterVuforia()
    19.     {
    20.         mVuforiaStarted = true;
    21.         SetAutofocus();
    22.     }
    23.  
    24.     void OnApplicationPause(bool pause)
    25.     {
    26.         if (!pause)
    27.         {
    28.             // App resumed
    29.             if (mVuforiaStarted)
    30.             {
    31.                 // App resumed and vuforia already started
    32.                 // but lets start it again...
    33.                 SetAutofocus(); // This is done because some android devices lose the auto focus after resume
    34.                 // this was a bug in vuforia 4 and 5. I haven't checked 6, but the code is harmless anyway
    35.             }
    36.         }
    37.     }
    38.  
    39.     private void SetAutofocus()
    40.     {
    41.         if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO))
    42.         {
    43.             Debug.Log("Autofocus set");
    44.         }
    45.         else
    46.         {
    47.             // never actually seen a device that doesn't support this, but just in case
    48.             Debug.Log("this device doesn't support auto focus");
    49.         }
    50.     }
    51. }
    52.  
     
  3. giusti

    giusti

    Joined:
    Sep 15, 2019
    Posts:
    5
    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
    Code (CSharp):
    1. using Vuforia;
    2.  
    3. public class AutoFocusCam : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         Focus();
    8.     }
    9.  
    10.     void Focus()
    11.     {
    12.         CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    13.         Invoke("Focus", 1);
    14.     }
    15. }
     
    unity_zFXZPrrcQEU5Uw likes this.