I am developing an AR mobile app using Unity.
For that I have added a Vuforia AR Camera, it works fine with my Laptop cam, and it shows a 3D model on an image target.
My quastion is how can I switch between front cam and back cam on Android device using a simple button?
I have tried this code example to switch between the Laptop cam and an external webcam :
using UnityEngine;
using Vuforia;
public class SwitchCamera : MonoBehaviour
{
private bool cameraMode = false;
public void OnCameraChangeMode()
{
CameraDevice.CameraDirection currentDir = CameraDevice.Instance.GetCameraDirection();
if (!cameraMode)
{
RestartCamera(CameraDevice.CameraDirection.CAMERA_FRONT);
Debug.Log("Back Camera");
}
else
{
RestartCamera(CameraDevice.CameraDirection.CAMERA_BACK);
Debug.Log("Front Camera");
}
}
private void RestartCamera(CameraDevice.CameraDirection newDir)
{
CameraDevice.Instance.Stop();
CameraDevice.Instance.Deinit();
CameraDevice.Instance.Init(newDir);
CameraDevice.Instance.Start();
}
}
Hello Montacer, You just forgot to change the cameramode value.
CameraDevice.CameraDirection currentDir = CameraDevice.Instance.GetCameraDirection();
if (!cameraMode)
{
RestartCamera(CameraDevice.CameraDirection.CAMERA_FRONT);
cameramode = true;
Debug.Log("Back Camera");
}
else
{
RestartCamera(CameraDevice.CameraDirection.CAMERA_BACK);
cameramode = false;
Debug.Log("Front Camera");
}
other than that everything looks great.hope this helps.
just as update: Front camera was removed from Vuforia. https://library.vuforia.com/content/vuforia-library/en/articles/Release_Notes/Vuforia-SDK-Release-Notes.html https://developer.vuforia.com/forum/unity/front-camera-deprecation
Front camera support has been removed. You can now only access the back camera (CameraDevice::init() now does not take any parameters).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With