从Unity访问Android Camera.Parameters

我想从android设备获取相机的数量,检查一个相机是否为背面,并为该相机获取水平和垂直视角Camera.Parameters.getHorizo​​ntalViewAngle().

我想从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’要求您的应用程序具有相机permises,除非您使用内置相机功能,否则它们不会由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

相关文章

前言 本文记录unity3D开发环境的搭建 unity安装 unity有中文...
前言 有时候我们希望公告牌跟随镜头旋转永远平行面向屏幕,同...
前言 经过一段时间的学习与实际开发,unity3D也勉强算是强行...
前言 在unity中我们常用的获取鼠标点击的方法有: 1、在3D场...
前言 在之前的例子中,我们都没有用到unity的精髓,例如地形...
这篇文章将为大家详细讲解有关Unity3D中如何通过Animator动画...