gaitat
gaitat

Reputation: 12642

Access Android Camera.Parameters from Unity

I would like to get the number of cameras from the android device, check if one is backfacing and for that camera get the horizontal and vertical view angles Camera.Parameters.getHorizontalViewAngle().

I would like to do this from a Unity3D c# script but my java skills are limited. Can anybody offer any help? I have this code:

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

but not even the getNumberOfCameras() call works.

Upvotes: 3

Views: 6749

Answers (1)

Jacek Przemieniecki
Jacek Przemieniecki

Reputation: 5967

getNumberOfCameras is a static method, so you need to CallStatic your 'cameraClass'

There is no 'getCamera' method in 'android.hardware.Camera' (or at least I cannot find it in the docs). You should use 'Camera.open' instead, but there is a trick. 'Camera.open' requires your app to have camera permisions, which are not generated by Unity unless you use built-in camera functionality.

The following works on 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

Upvotes: 4

Related Questions