[英]How to Switch/Change Front and back Vuforia AR Camera using a button?
I am developing an AR mobile app using Unity.我正在使用 Unity 开发 AR 移动应用程序。
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.为此,我添加了一个 Vuforia AR 摄像头,它可以与我的笔记本电脑摄像头配合使用,并在图像目标上显示 3D 模型。
My quastion is how can I switch between front cam and back cam on Android device using a simple button?我的问题是如何使用一个简单的按钮在 Android 设备上的前摄像头和后摄像头之间切换?
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.您好 Montacer,您只是忘记更改相机模式值。
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.正如更新:前置摄像头已从 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
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).
您现在只能访问后置摄像头(CameraDevice::init() 现在不接受任何参数)。
I've tried this code and it works perfectly.我试过这段代码,它工作得很好。
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();
}
}
}
I used this code and work fine我使用了这段代码并且工作正常
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.