Hi,
I had this app that was made in an older vrsion of Vuforia. Now I am using the newest version of Vuforia and in the script CammeraSettings.cs I had to make some changes because the the front camera activation is not supported anymore.
I made the necessary changes but now the camera is out of focus and I cannot fins why.
Here is the script:
using UnityEngine;
using System.Collections;
using Vuforia;
public class CameraSettings : MonoBehaviour
{
#region PRIVATE_MEMBERS
private bool mVuforiaStarted = false;
private bool mAutofocusEnabled = true;
private bool mFlashTorchEnabled = false;
private CameraDevice.CameraDeviceMode mActiveCamera = CameraDevice.CameraDeviceMode.MODE_DEFAULT;
#endregion //PRIVATE_MEMBERS
#region MONOBEHAVIOUR_METHODS
void Start()
{
var vuforia = VuforiaARController.Instance;
vuforia.RegisterVuforiaStartedCallback(OnVuforiaStarted);
vuforia.RegisterOnPauseCallback(OnPaused);
}
#endregion // MONOBEHAVIOUR_METHODS
#region PUBLIC_METHODS
public bool IsFlashTorchEnabled()
{
return mFlashTorchEnabled;
}
public void SwitchFlashTorch(bool ON)
{
if (CameraDevice.Instance.SetFlashTorchMode(ON))
{
Debug.Log("Successfully turned flash " + ON);
mFlashTorchEnabled = ON;
}
else
{
Debug.Log("Failed to set the flash torch " + ON);
mFlashTorchEnabled = false;
}
}
public bool IsAutofocusEnabled()
{
return mAutofocusEnabled;
}
public void SwitchAutofocus(bool ON)
{
if (ON)
{
if (CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO))
{
Debug.Log("Successfully enabled continuous autofocus.");
mAutofocusEnabled = true;
}
else
{
// Fallback to normal focus mode
Debug.Log("Failed to enable continuous autofocus, switching to normal focus mode");
mAutofocusEnabled = false;
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
}
else
{
Debug.Log("Disabling continuous autofocus (enabling normal focus mode).");
mAutofocusEnabled = false;
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
}
public void TriggerAutofocusEvent()
{
// Trigger an autofocus event
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_TRIGGERAUTO);
// Then restore original focus mode
StartCoroutine(RestoreOriginalFocusMode());
}
/* public void SelectCamera(CameraDevice.CameraDirection camDir)
{
if (RestartCamera(camDir))
{
mActiveDirection = camDir;
// Upon camera restart, flash is turned off
mFlashTorchEnabled = false;
}
}*/
public void SelectCamera(bool boolean)
{
if (RestartCamera(boolean))
{
// Upon camera restart, flash is turned off
mFlashTorchEnabled = false;
}
}
public bool IsFrontCameraActive()
{
return true; // because now CameraDirection does not exist and it will always use back camera as front camera is not supported anymore
//return (mActiveDirection == CameraDevice.CameraDirection.CAMERA_FRONT);
}
//public bool RestartCamera(CameraDevice.CameraDirection direction)
public bool RestartCamera(bool boolean)
{
ObjectTracker tracker = TrackerManager.Instance.GetTracker<ObjectTracker>();
if (tracker != null)
{
tracker.Stop();
}
CameraDevice.Instance.Stop();// stops the camera
CameraDevice.Instance.Deinit();// initialaizes de camera again
//if (!CameraDevice.Instance.Init(direction))
if (!CameraDevice.Instance.Init())
{
Debug.Log("Failed to init camera for direction: "/* + direction.ToString()*/);
return false;
}
if (!CameraDevice.Instance.Start())
{
Debug.Log("Failed to start camera for direction: " /*+ direction.ToString()*/);
return false;
}
if (tracker != null)
{
if (!tracker.Start())
{
Debug.Log("Failed to restart the Tracker.");
return false;
}
}
return true;
}
#endregion // PUBLIC_METHODS
#region PRIVATE_METHODS
private void OnVuforiaStarted()
{
mVuforiaStarted = true;
// Try enabling continuous autofocus
SwitchAutofocus(true);
}
private void OnPaused(bool paused)
{
bool appResumed = !paused;
if (appResumed && mVuforiaStarted)
{
// Restore original focus mode when app is resumed
if (mAutofocusEnabled)
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
else
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
else
{
// Set the torch flag to false on pause (because the flash torch is switched off by the OS automatically)
mFlashTorchEnabled = false;
}
}
private IEnumerator RestoreOriginalFocusMode()
{
// Wait 1.5 seconds
yield return new WaitForSeconds(1.5f);
// Restore original focus mode
if (mAutofocusEnabled)
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
else
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_NORMAL);
}
#endregion // PRIVATE_METHODS
}
Hi,
We have in our CoreSamples, most of the scenes, an UI Button Autofocus which should focus the camera. The script is: AutoFocusSettings.cs
If you deactivate the CamerSetting.cs does it work as expected?
Thank you.
Vuforia Engine Support