android camera portrait orientation

2019-01-22 23:32发布

问题:

I am using camera in my app. I am just using intent to start camera

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(cameraIntent, 101);

The captured image automatically comes in landscape view. How do i make camera to capture images in portrait view

回答1:

If device has v2.2 or above you can rotate camera orientation to portrait using camera.setDisplayOrientation(90). In devices below to v2.2 the camera will only display in landscape orientation and thus image will come in landscape. Check these posts Using Camera in Portrait Orientation, Camera is wrong unless keyboard is open.



回答2:

try this.

        Parameters param = mCamera.getParameters();

        switch(mDisplay.getRotation()){
        case Surface.ROTATION_0:
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO){
                mCamera.setDisplayOrientation(90);
                Log.d("Rotation_0", "whatever");
            }
            else{
                Log.d("Rotation_0", "whatever");
                param.setRotation(90);
                mCamera.setParameters(param);
            }
            break;
        case Surface.ROTATION_90:
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO){
                mCamera.setDisplayOrientation(0);
                Log.d("Rotation_0", "whatever");
            }
            else{
                Log.d("Rotation_90", "whatever");
                param.setRotation(0);
                mCamera.setParameters(param);
            }
            break;
        }


回答3:

Here, this code is suitable for all types of orientation that are supported in android camera application[Portrait mode] too.

int rotation = this.getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;

switch (rotation) {

    case Surface.ROTATION_0:
        degrees = 0;
        break;

    case Surface.ROTATION_90:
        degrees = 90;
        break;

    case Surface.ROTATION_180:
        degrees = 180;
        break;

    case Surface.ROTATION_270:
        degrees = 270;
        break;

}