Я хочу разрабатывать с помощью vuforiaEngine и Unity и использовать переднюю камеру для чтения ImageTarget.C#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Я хочу разрабатывать с помощью vuforiaEngine и Unity и использовать переднюю камеру для чтения ImageTarget.

Сообщение Anonymous »

Я хотел бы использовать vuforia и unity в некоторых своих приложениях для Android для достижения дополненной реальности.
Однако в настоящее время распознавание целевых изображений с помощью задней камеры работает хорошо, но не с передней камерой.
>Я провел исследование и нашел несколько ссылок, в которых говорится, что он не поддерживается после определенной версии.
Это больше невозможно?
Используется версия 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.name}, frontCamera: {devices.isFrontFacing}");
}

int frontCameraIndex = -1;
for (int i = 0; i < devices.Length; i++)
{
if (devices.isFrontFacing)
{
frontCameraIndex = i;
break;
}
}

if (frontCameraIndex != -1)
{

webcamTexture = new WebCamTexture(devices[frontCameraIndex].name);
Renderer renderer = GameObject.Find("ARObject")?.GetComponent();

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();
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("getExternalStoragePublicDirectory", directoryType))
{
return file.Call("getAbsolutePath") + "/" + fileName;
}
}
#else

return Application.persistentDataPath + "/" + fileName;
#endif
}
}


Подробнее здесь: https://stackoverflow.com/questions/792 ... ra-to-read
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C#»