我想用vuforiaEngine和Unity进行开发,使用前置摄像头读取ImageTarget

问题描述 投票:0回答:1

我想在我的一些 Android 原生应用程序中使用 vuforia 和 unity 来实现 AR。 然而,目前后置摄像头的图像目标识别效果很好,但前置摄像头的图像目标识别效果不佳。 我一直在研究并发现一些参考文献说在某个版本之后不再支持它。 这已经不可能了吗?

使用的vuforiaEngine版本是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
    }
}
c# android unity-game-engine vuforia
1个回答
0
投票

Vuforia 或 ArFoundation 是可与 ARCore 和 ARKit 配合使用的框架。

硬件摄像头的选择取决于这些 SDK。

这些 SDK 使用前置摄像头进行人脸追踪。

© www.soinside.com 2019 - 2024. All rights reserved.