如何使用按钮切换/更换前后Vuforia AR相机?

时间:2018-04-19 16:06:14

标签: c# unity3d camera augmented-reality vuforia

我正在使用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();
     }
 }

4 个答案:

答案 0 :(得分:2)

Hello Montacer,                你只是忘了改变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");
     }

除此之外,一切看起来都很棒。这有帮助。

答案 1 :(得分:1)

仅作为更新:前置摄像头已从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

已删除前置摄像头支架。现在,您只能访问后置摄像头(CameraDevice :: init()现在不带任何参数)。

答案 2 :(得分:0)

我已尝试过此代码,但效果非常好。

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

答案 3 :(得分:0)

我使用了此代码,并且工作正常

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
    }

}