gpt4 book ai didi

android - 从 Unity 访问 Android Camera.Parameters

转载 作者:太空宇宙 更新时间:2023-11-03 13:26:51 32 4
gpt4 key购买 nike

我想从 android 设备获取摄像头的数量,检查是否有一个是背面的,并为该摄像头获取水平和垂直视角 Camera.Parameters.getHorizontalViewAngle() .

我想通过 Unity3D c# 脚本执行此操作,但我的 Java 技能有限。有人可以提供帮助吗?我有这段代码:

using UnityEngine;

#if UNITY_ANDROID
public class CameraParametersAndroid
{
public static float HorizontalViewAngle { get; protected set; }
public static float VerticalViewAngle { get; protected set; }
public static int numCameras { get; protected set; }

static CameraParametersAndroid()
{
AndroidJavaClass unityPlayerClass = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"),
cameraClass = new AndroidJavaClass ("android.hardware.Camera"),
cameraParametersClass = new AndroidJavaClass ("android.hardware.Camera.Parameters"),
cameraInfoClass = new AndroidJavaClass ("android.hardware.Camera.CameraInfo");

AndroidJavaObject currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
object[] args = {currentActivity};

AndroidJavaObject camera = cameraClass.CallStatic<AndroidJavaObject>("getCamera", args);

if (camera != null)
{
AndroidJavaObject cameraParameters = camera.Call<AndroidJavaObject>("getParameters");

numCameras = camera.Call<int>("getNumberOfCameras");
HorizontalViewAngle = cameraParameters.Call<float>("getHorizontalViewAngle");
VerticalViewAngle = cameraParameters.Call<float>("getVerticalViewAngle");

}
else
{
Debug.LogError("[CameraParametersAndroid] Camera not available");
}
}
}
#endif

但即使是 getNumberOfCameras() 也不是通话有效。

最佳答案

getNumberOfCameras是一个静态方法,所以你需要 CallStatic你的 'cameraClass'

“android.hardware.Camera”中没有“getCamera”方法(或者至少我在文档中找不到它)。您应该改用“Camera.open”,但有一个技巧。 “Camera.open”要求您的应用具有相机权限,除非您使用内置相机功能,否则 Unity 不会生成这些权限。

以下适用于 Android ICS:

using UnityEngine;

#if UNITY_ANDROID
public class CameraParametersAndroid
{
public static float HorizontalViewAngle { get; protected set; }
public static float VerticalViewAngle { get; protected set; }
public static int numCameras { get; protected set; }

static CameraParametersAndroid()
{
AndroidJavaClass cameraClass = new AndroidJavaClass("android.hardware.Camera");

numCameras = cameraClass.CallStatic<int>("getNumberOfCameras");

// This is an ugly hack to make Unity
// generate Camera permisions
WebCamDevice[] devices = WebCamTexture.devices;

// Camera.open gets back-facing camera by default
// you should check for exceptions
int camID = 0;
AndroidJavaObject camera = cameraClass.CallStatic<AndroidJavaObject>("open", camID);

// I'm pretty sure camera will never be null at this point
// It will either be a valid object or Camera.open would throw an exception
if (camera != null)
{
AndroidJavaObject cameraParameters = camera.Call<AndroidJavaObject>("getParameters");
HorizontalViewAngle = cameraParameters.Call<float>("getHorizontalViewAngle");
VerticalViewAngle = cameraParameters.Call<float>("getVerticalViewAngle");

}
else
{
Debug.LogError("[CameraParametersAndroid] Camera not available");
}
}
}
#endif

关于android - 从 Unity 访问 Android Camera.Parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18450404/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com