如何防止相机应用在Android中更改横向方向?

时间:2015-10-26 10:00:01

标签: android android-camera

我已经通过我的应用程序使用相机原生应用来拍照。我使用以下代码仅以纵向模式显示相机应用程序。

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
takePictureIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
                ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
startActivityForResult(takePictureIntent, actionCode);

但是,它没有使用上面的代码。

任何建议都将受到赞赏: - )

3 个答案:

答案 0 :(得分:3)

您无法控制启动的外部应用程序的方向,因此无法执行此操作。 但是你可以创建自己的相机活动。

答案 1 :(得分:0)

将mCamera视为您的相机,您可以在开始预览之前添加mCamera.setDisplayOrientation(90);来创建相机活动,设置àmatainerPReview并在protrait模式下设置预览

以下是potrait模式下FrameLayout中相机预览的示例:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            if(mCamera!=null){
            mCamera.setPreviewDisplay(holder);
            mCamera.setDisplayOrientation(90);
            mCamera.startPreview();}
        } catch (IOException e) {
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e) {
        }
    }

然后从MainActivity设置预览,如下所示:

mPreview = new CameraPreview(getApplicationContext(), camera);
        preview.addView(mPreview);

答案 2 :(得分:0)

请浏览android开发者链接:

http://developer.android.com/guide/topics/media/camera.html#capture-video

默认情况下,相机预览的方向为横向。请检查一下:

  

注意:相机预览不必处于横向模式。开始   在Android 2.2(API Level 8)中,您可以使用setDisplayOrientation()   设置预览图像旋转的方法。为了改变   当用户重新定位手机时,预览方向   预览类的surfaceChanged()方法,首先停止预览   使用Camera.stopPreview()更改方向,然后启动   使用Camera.startPreview()再次预览。

您还可以参考Camera.Parameters.setRotation()了解更多信息。

或者您可以在录制视频时致电mediaRecorder.setOrientationHint(rotation)

此外,如果要使摄像机图像以与显示屏相同的方向显示,可以使用以下代码。

public static void setCameraDisplayOrientation(Activity activity,
         int cameraId, android.hardware.Camera camera) {
     android.hardware.Camera.CameraInfo info =
             new android.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     int rotation = activity.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;
     }

     int result;
     if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
         result = (info.orientation + degrees) % 360;
         result = (360 - result) % 360;  // compensate the mirror
     } else {  // back-facing
         result = (info.orientation - degrees + 360) % 360;
     }
     camera.setDisplayOrientation(result);
 }

从API级别14开始,可以在预览处于活动状态时调用此方法