如何 Switch/Change 使用按钮前后 Vuforia AR 相机?

How to Switch/Change Front and back Vuforia AR Camera using a button?

我正在使用 Unity 开发 AR 移动应用程序。

为此,我添加了一个 Vuforia AR 相机,它可以很好地与我的笔记本电脑摄像头配合使用,并在图像目标上显示 3D 模型。

我的问题是如何使用简单的按钮在 Android 设备上切换前摄像头和后摄像头?

我已尝试使用此代码示例在笔记本电脑摄像头和外部网络摄像头之间切换:

 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();
     }
 }

你好蒙塔塞, 您只是忘记更改 cameramode 值。

     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");
     }

除此之外,一切看起来 great.hope 这有帮助。

我试过这段代码,效果很好。

using UnityEngine;
using Vuforia;

public class Cameraa : MonoBehaviour
{
    private bool on = true;

    public void Switch()
    {
        on = !on;
        if (on)
        {
            CameraDevice.Instance.Stop();
            CameraDevice.Instance.Deinit();
            TrackerManager.Instance.GetTracker<ObjectTracker>().Stop();

            CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_BACK);
            CameraDevice.Instance.Start();
            TrackerManager.Instance.GetTracker<ObjectTracker>().Start();

        }
        else if (!on)
        {
            CameraDevice.Instance.Stop();
            CameraDevice.Instance.Deinit();

            TrackerManager.Instance.GetTracker<ObjectTracker>().Stop();
            CameraDevice.Instance.Init(CameraDevice.CameraDirection.CAMERA_FRONT);

            CameraDevice.Instance.Start();
            TrackerManager.Instance.GetTracker<ObjectTracker>().Start();
        }
    }
}

正如更新一样:前置摄像头已从 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).

我使用了这段代码并且工作正常

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


public class ARManager : MonoBehaviour
{
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
    bool isTorchOn = false;
#endif

    public void toggleTorch()
    {
#if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR
        if (EasyWebCam.isActive) {
            if (isTorchOn) {
                //torchImage.sprite = torchOffSprite;
                EasyWebCam.setTorchMode (TBEasyWebCam.Setting.TorchMode.Off);
            } else {
                //torchImage.sprite = torchOnSprite;
                EasyWebCam.setTorchMode (TBEasyWebCam.Setting.TorchMode.On);
            }
            isTorchOn = !isTorchOn;
        }
#endif
    }

}