C#Android MediaStore.ACTION_IMAGE_CAPTURE

时间:2016-05-19 06:18:42

标签: c# android android-intent unity3d

尝试在Unity C#中访问Android本机相机组件,但无法正确移植java代码。我有另一个打算分享的意图,但我似乎无法让这个工作。

这是我的代码:

static int REQUEST_IMAGE_CAPTURE = 1;

public void TakePhotoWithCamera()
{
    AndroidJavaClass intentClass = new AndroidJavaClass("android.provider.MediaStore");
    AndroidJavaObject intentObject = new AndroidJavaObject("android.provider.MediaStore");

    intentObject.GetStatic<string>("ACTION_IMAGE_CAPTURE");

    AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
    currentActivity.Call("startActivity", intentObject, REQUEST_IMAGE_CAPTURE);
}

2 个答案:

答案 0 :(得分:1)

虽然代码很好,但如果你想在单位之外使用相机 - 它不允许你在单位内操纵相机预览,例如在顶部添加着色器或UI元素。

如果你想做更高级的东西,可以使用下面的代码在WebCamTexture中获取正在运行的设备的实例,并修改摄像机的属性和行为。

以下是来自资产CameraCaptureKit(https://www.assetstore.unity3d.com/en/#!/content/56673)的一些代码示例,该资产是我们用来拍照并在社交应用中分享的组件。

Class clsPlayer = Class.forName("com.unity3d.player.UnityPlayer"); // com.unity3d.player.UnityPlayerNativeActivity
        Field fCurrentActivity = clsPlayer.getDeclaredField("currentActivity");
        fCurrentActivity.setAccessible(true);
        android.app.Activity currentActivity = (android.app.Activity)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);


        //Field fGetNumCameras = player.getClass().getDeclaredField("getNumCameras");


        java.util.ArrayList cameraArrays = (java.util.ArrayList)f.get( player );
        int sz = cameraArrays.size();

        if( sz == 0 ) {
            Log.d("Unity","CameraCaptureKit: Error cannot resolve Unity camera! - Maybe Unity has crashed and restarted?");
            ret.ok=false;
        } else {
            //Log.d("Unity","CameraCaptureKit: SIZE CAMERA ARRAY : " + sz);
            Object cameraContainer = (Object)cameraArrays.get(0);



            Field fCameraId = cameraContainer.getClass().getDeclaredField("h"); 
            fCameraId.setAccessible(true);
            ret.cameraId = (int)fCameraId.get( cameraContainer );

            Field fCameraInst = cameraContainer.getClass().getDeclaredField("a"); // there is a member called A which is private and contains a referance to the camera
            if( dbg ) Log.d("Unity", "CameraCaptureKit : a fieldType=" + ((Class) fCameraInst.getClass()).getName() );
            fCameraInst.setAccessible(true);
            Camera adkCamera = (Camera)fCameraInst.get( cameraContainer );

答案 1 :(得分:0)

而不是删除这个问题,因为我很难找到任何可以帮助我的东西,我会发布我所做的。

static int REQUEST_IMAGE_CAPTURE = 1;

public void TakePhotoWithCamera()
{
    AndroidJavaClass IntentClass = new AndroidJavaClass("android.content.Intent");
    AndroidJavaObject IntentObject = new AndroidJavaObject("android.content.Intent");

    AndroidJavaObject MSObject = new AndroidJavaObject("android.provider.MediaStore");

    IntentObject.Call<AndroidJavaObject>("setAction", MSObject.GetStatic<string>("ACTION_IMAGE_CAPTURE"));

    AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity");
    currentActivity.Call("startActivityForResult", IntentObject, REQUEST_IMAGE_CAPTURE);
}