How to set the tango camera exposure and iso parameters from c#/Unity

久未见 提交于 2019-12-06 02:16:17

You might need to write a plugin that can set the iso value and exposure on the Unity camera instance. You can do that by taking an instance to the running camera's referance by some tricky hackery involving resolving the camera instance and then you should be able to inject the iso/exposure parameters.

An example of such plugin would be something like the Camera Capture Kit for Unity ( https://www.assetstore.unity3d.com/en/#!/content/56673 )

It will enable you to hook into the camera and apply properties. Here is a snippet on how the camera is resolved.

Class clsPlayer = Class.forName("com.unity3d.player.UnityPlayer");
    Field fCurrentActivity = clsPlayer.getDeclaredField("currentActivity");
    fCurrentActivity.setAccessible(true);
    com.unity3d.player.UnityPlayerActivity currentActivity = (com.unity3d.player.UnityPlayerActivity)fCurrentActivity.get(null);
    ret.playerActivity = currentActivity;

    Field fPlayer = currentActivity.getClass().getDeclaredField("mUnityPlayer");
    fPlayer.setAccessible(true);
    com.unity3d.player.UnityPlayer player = (com.unity3d.player.UnityPlayer)fPlayer.get(currentActivity);
    ret.player = player;

    Field f = player.getClass().getDeclaredField("y");
    f.setAccessible(true);
    java.util.ArrayList cameraArrays = (java.util.ArrayList)f.get( player );
    int sz = cameraArrays.size();

You will then have to change the parameters inside an Android plugin , with something like this ( Taken from Camera Capture Kit )

Camera.Parameters params = ret.camera.getParameters();

    String flat = params.flatten();
    String iso_keyword=null;
    if(flat.contains("iso-values")) {
        iso_keyword="iso";
    } else if(flat.contains("iso-mode-values")) {
        iso_keyword="iso";
    } else if(flat.contains("iso-speed-values")) {
        iso_keyword="iso-speed";
    } else if(flat.contains("nv-picture-iso-values")) {
        iso_keyword="nv-picture-iso";
    }
    if( iso_keyword == null ) {
        Log.d("Unity", "CameraCaptureKit: It looks like there was no support for iso on the device." ); 
        return;
    }

    String strSupportedIsoValues = UnityCamera_GetSupportedISOValues();
    ArrayList<String> supportedIsoValues = new ArrayList<String>(Arrays.asList(strSupportedIsoValues.split(",") ));
    //ArrayList<String> supportedIsoValues = Arrays.asList( strSupportedIsoValues.split(",") );
    boolean contains = false;
    for( String isoValue : supportedIsoValues ) {
        if(isoValue.contains(newValue)) { 
            contains = true; 
            break; 
        }
    }
    if( contains == false ) {
        Log.d("Unity", "CameraCaptureKit: failed to set ISO, the value " + newValue + " is not supported. ( " + strSupportedIsoValues + " )" );
        return;
    }

    // update the camera.
    params.set( iso_keyword, newValue );
    ret.camera.setParameters(params);

Cheers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!