2013-08-26 93 views
3

我想從android設備獲取攝像頭的數量,檢查是否有背光,並且該攝像頭的水平和垂直視角爲Camera.Parameters.getHorizontalViewAngle()訪問來自Unity的Android Camera.Parameters

我想從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()通話作品。

+0

**重要**最近注意到這個非常有用的原生相機插件在資產商店... https://www.assetstore.unity3d.com/en /#!/ content/52154 – Fattie

回答

3

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 
+0

哇,這太好了。有效。你有任何輸入如何調用Camera.CameraInfo來檢查後置攝像頭嗎? – gaitat

+0

正如我所說,'Camera.open()'如果沒有參數調用獲取第一個背面照相機,沒有必要檢查它。 –

+0

是的,我知道。我只是想知道應該如何進行調用,因爲Camera.getCameraInfo()返回一個Camera.CameraInfo結構,並且不清楚如何從Unity進行這樣的Java調用。 – gaitat