Vuforia Engine 10.27+Unity安卓前置摄像头识别ImageTarget可行性咨询
前置摄像头无法识别ImageTarget(Vuforia Engine 10.27 + Unity)
我计划使用Vuforia Engine与Unity开发安卓原生AR应用,需求是通过前置摄像头识别ImageTarget。目前后置摄像头识别ImageTarget功能正常,但前置摄像头完全无法识别。查阅资料得知该功能可能在某版本后不再支持,我当前使用的Vuforia Engine版本为10.27,特此咨询该功能是否已无法实现。
以下是我在Unity中尝试的代码:
using UnityEngine; using UnityEngine.Networking; using System.Collections; using UnityEngine.Android; using System.IO; public class LoadAndUseFrontCamera : MonoBehaviour { private string fileName = "test.jpg"; private string imagePath; public Texture2D defaultTexture; private WebCamTexture webcamTexture; void Start() { imagePath = GetExternalStoragePublicDirectory("Pictures", fileName); StartFrontCamera(); } void StartFrontCamera() { WebCamDevice[] devices = WebCamTexture.devices; if (devices.Length > 0) { for (int i = 0; i < devices.Length; i++) { Debug.Log($"deviceName: {devices[i].name}, frontCamera: {devices[i].isFrontFacing}"); } int frontCameraIndex = -1; for (int i = 0; i < devices.Length; i++) { if (devices[i].isFrontFacing) { frontCameraIndex = i; break; } } if (frontCameraIndex != -1) { webcamTexture = new WebCamTexture(devices[frontCameraIndex].name); Renderer renderer = GameObject.Find("ARObject")?.GetComponent<Renderer>(); if (renderer != null) { renderer.material.mainTexture = webcamTexture; webcamTexture.Play(); } else { Debug.LogError("Renderer notfound"); } } else { Debug.LogWarning("frontCamera notfound"); ApplyDefaultTexture(); } } else { Debug.LogWarning("cameraDevice notfound"); ApplyDefaultTexture(); } } void ApplyDefaultTexture() { Renderer renderer = GameObject.Find("ARObject")?.GetComponent<Renderer>(); if (renderer != null) { renderer.material.mainTexture = defaultTexture; } else { Debug.LogError("Renderer notfound"); } } void OnApplicationPause(bool pauseStatus) { if (webcamTexture != null) { if (pauseStatus) { webcamTexture.Pause(); } else { webcamTexture.Play(); } } } void OnDestroy() { if (webcamTexture != null) { webcamTexture.Stop(); } } //Code to flip the camera Texture2D FlipTexture(Texture2D originalTexture) { Texture2D flippedTexture = new Texture2D(originalTexture.width, originalTexture.height); for (int y = 0; y < originalTexture.height; y++) { for (int x = 0; x < originalTexture.width; x++) { Color pixel = originalTexture.GetPixel(originalTexture.width - x - 1, originalTexture.height - y - 1); flippedTexture.SetPixel(x, y, pixel); } } flippedTexture.Apply(); return flippedTexture; } string GetExternalStoragePublicDirectory(string directoryType, string fileName) { #if UNITY_ANDROID && !UNITY_EDITOR using (AndroidJavaClass environment = new AndroidJavaClass("android.os.Environment")) { using (AndroidJavaObject file = environment.CallStatic<AndroidJavaObject>("getExternalStoragePublicDirectory", directoryType)) { return file.Call<string>("getAbsolutePath") + "/" + fileName; } } #else return Application.persistentDataPath + "/" + fileName; #endif } }
内容的提问来源于stack exchange,提问作者南将希
相关产品推荐
相关产品推荐

