Search Unity

Camera Focus BUG, Focus is not working in IOS Vuforia: 7.0.47 [SOLVED]

Discussion in 'Vuforia' started by skdev3, Mar 7, 2018.

  1. skdev3

    skdev3

    Joined:
    Jul 15, 2015
    Posts:
    64
    Unity: 2017.3.1p2
    Vuforia: 7.0.47
    Xcode: 9.2

    Test on devices: Iphone SE, Iphone X, Iphone 6s, Iphone 8, Iphone 5.

    this Code not work (((
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Vuforia;
    6. public class CameraFocusController : MonoBehaviour {
    7. // Use this for initialization
    8. void Start ()
    9. {
    10.   VuforiaARController.Instance.RegisterVuforiaStartedCallback(OnVuforiaStarted);
    11.   VuforiaARController.Instance.RegisterOnPauseCallback(OnPaused);
    12. }
    13. private void OnVuforiaStarted()
    14. {
    15.   bool focusSet = CameraDevice.Instance.SetFocusMode(
    16.    CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    17.   Debug.Log ("Camera Focus is set: " + focusSet);
    18. }
    19. private void OnPaused(bool paused)
    20. {
    21.   if (!paused) // resumed
    22.   {
    23.    // Set again autofocus mode when app is resumed
    24.    CameraDevice.Instance.SetFocusMode(
    25.     CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    26.   }
    27. }
    28. }
    29.  
    Methods
    CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_MACRO);
    CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_INFINITY);
    do not work too...

    How to fix this problem?
     
    Last edited: Mar 7, 2018
  2. skdev3

    skdev3

    Joined:
    Jul 15, 2015
    Posts:
    64
    SOLVED:
    1. Added standart CameraSettings script on ARCamera
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using Vuforia;
    5.  
    6. public class CameraSettings : MonoBehaviour
    7. {
    8.     #region PRIVATE_MEMBERS
    9.     private bool mVuforiaStarted = false;
    10.     private bool mAutofocusEnabled = true;
    11.     private bool mFlashTorchEnabled = false;
    12.     private CameraDevice.CameraDirection mActiveDirection = CameraDevice.CameraDirection.CAMERA_DEFAULT;
    13.     #endregion //PRIVATE_MEMBERS
    14.  
    15.  
    16.     #region MONOBEHAVIOUR_METHODS
    17.     void Start()
    18.     {
    19.         var vuforia = VuforiaARController.Instance;
    20.         vuforia.RegisterVuforiaStartedCallback(OnVuforiaStarted);
    21.         vuforia.RegisterOnPauseCallback(OnPaused);
    22.     }
    23.     #endregion // MONOBEHAVIOUR_METHODS
    24.  
    25.  
    26.     #region PUBLIC_METHODS
    27.     public bool IsFlashTorchEnabled()
    28.     {
    29.         return mFlashTorchEnabled;
    30.     }
    31.  
    32.     public void SwitchFlashTorch(bool ON)
    33.     {
    34.         if (CameraDevice.Instance.SetFlashTorchMode(ON))
    35.         {
    36.             Debug.Log("Successfully turned flash " + ON);
    37.             mFlashTorchEnabled = ON;
    38.         }
    39.         else
    40.         {
    41.             Debug.Log("Failed to set the flash torch " + ON);
    42.             mFlashTorchEnabled = false;
    43.         }
    44.     }
    45.  
    46.     public bool IsAutofocusEnabled()
    47.     {
    48.         return mAutofocusEnabled;
    49.     }
    50.  
    51.     public void SwitchAutofocus(bool ON)
    52.     {
    53.         if (ON)
    54.         {
    55.             if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO))
    56.             {
    57.                 Debug.Log("Successfully enabled continuous autofocus.");
    58.                 mAutofocusEnabled = true;
    59.             }
    60.             else
    61.             {
    62.                 // Fallback to normal focus mode
    63.                 Debug.Log("Failed to enable continuous autofocus, switching to normal focus mode");
    64.                 mAutofocusEnabled = false;
    65.                 CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
    66.             }
    67.         }
    68.         else
    69.         {
    70.             Debug.Log("Disabling continuous autofocus (enabling normal focus mode).");
    71.             mAutofocusEnabled = false;
    72.             CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
    73.         }
    74.     }
    75.  
    76.     public void TriggerAutofocusEvent()
    77.     {
    78.         // Trigger an autofocus event
    79.         CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO);
    80.  
    81.         // Then restore original focus mode
    82.         StartCoroutine(RestoreOriginalFocusMode());
    83.     }
    84.  
    85.     public void SelectCamera(CameraDevice.CameraDirection camDir)
    86.     {
    87.         if (RestartCamera(camDir))
    88.         {
    89.             mActiveDirection = camDir;
    90.  
    91.             // Upon camera restart, flash is turned off
    92.             mFlashTorchEnabled = false;
    93.         }
    94.     }
    95.  
    96.     public bool IsFrontCameraActive()
    97.     {
    98.         return (mActiveDirection == CameraDevice.CameraDirection.CAMERA_FRONT);
    99.     }
    100.  
    101.  
    102.     public bool RestartCamera(CameraDevice.CameraDirection direction)
    103.     {
    104.         ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
    105.         if (tracker != null)
    106.         {
    107.             tracker.Stop();
    108.         }
    109.         CameraDevice.Instance.Stop();
    110.         CameraDevice.Instance.Deinit();
    111.  
    112.         if (!CameraDevice.Instance.Init(direction))
    113.         {
    114.             Debug.Log("Failed to init camera for direction: " + direction.ToString());
    115.             return false;
    116.         }
    117.         if (!CameraDevice.Instance.Start())
    118.         {
    119.             Debug.Log("Failed to start camera for direction: " + direction.ToString());
    120.             return false;
    121.         }
    122.  
    123.         if (tracker != null)
    124.         {
    125.             if (!tracker.Start())
    126.             {
    127.                 Debug.Log("Failed to restart the Tracker.");
    128.                 return false;
    129.             }
    130.         }
    131.  
    132.         return true;
    133.     }
    134.     #endregion // PUBLIC_METHODS
    135.  
    136.  
    137.     #region PRIVATE_METHODS
    138.     private void OnVuforiaStarted()
    139.     {
    140.         mVuforiaStarted = true;
    141.         // Try enabling continuous autofocus
    142.         SwitchAutofocus(true);
    143.     }
    144.  
    145.     private void OnPaused(bool paused)
    146.     {
    147.         bool appResumed = !paused;
    148.         if (appResumed && mVuforiaStarted)
    149.         {
    150.             // Restore original focus mode when app is resumed
    151.             if (mAutofocusEnabled)
    152.                 CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    153.             else
    154.                 CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
    155.         }
    156.         else
    157.         {
    158.             // Set the torch flag to false on pause (because the flash torch is switched off by the OS automatically)
    159.             mFlashTorchEnabled = false;
    160.         }
    161.     }
    162.  
    163.     private IEnumerator RestoreOriginalFocusMode()
    164.     {
    165.         // Wait 1.5 seconds
    166.         yield return new WaitForSeconds(1.5f);
    167.  
    168.         // Restore original focus mode
    169.         if (mAutofocusEnabled)
    170.             CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
    171.         else
    172.             CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
    173.     }
    174.  
    175.     #endregion // PRIVATE_METHODS
    176. }
    177.  
    178.  
    2. We wait 10 seconds, we offer the user to scan again Vumark. And Run this code:
    Code (csharp):
    1.  
    2.     public void Execute()
    3.     {
    4.         CameraDevice.Instance.Stop();
    5.         GameObject cam = Camera.main.gameObject;
    6.         cam.SetActive(false);
    7.         cam.SetActive(true);
    8.         CameraDevice.Instance.Start();
    9.     }
    10.  
    3. camera focus )
     
  3. inovotm

    inovotm

    Joined:
    Jul 31, 2017
    Posts:
    1
    @skdev3
    Hy @skdev3 I am facing same problem Camera gets blurry and focus not working on Iphone se,6s,7. I have tried your script but its not working for me.i am tracking on 30 to 45 angle and it works on Iphone6 and ipad but not on 6s,se,7. Please Help..THANK YOU...
     
    srtSteel and kaichio like this.
  4. cheezorg

    cheezorg

    Joined:
    Jun 5, 2008
    Posts:
    394
    @skdev3 looks like you are restarting the camera to give Vuforia time to get tis settings correct. I tried your implementation but unfortunately didn't have any success with it. Calling Execute() after 10 seconds just flashes the camera view, but still no autofocus on iPhone 7 Plus.
    Running Unity 2018.1
     
  5. HidingGlass

    HidingGlass

    Joined:
    Apr 29, 2015
    Posts:
    25
    Because autofocus is only available again in ARKit 1.5, I would double check you are using the latest version of Vuforia (I’m not sure but if you migrated your project from an older version of Unity to 2018.1 you may still have an older version of Vuforia). I would also be sure your device is running iOS 11.3 or above as that’s the version that shipped with ARKit 1.5.

    I would test a brand new test project in 2018.1 on an iOS 11.3 or above device just to test.