Android如何在拍摄自定义相机时根据方向旋转照片

时间:2017-06-01 10:47:21

标签: android android-camera android-orientation android-framelayout

当我使用自定义相机拍摄照片时,我无法改变照片的方向。我想手动处理方向。当调用Camera.PictureCallback时,我不知道如何在onConfigurationchanged中旋转图片。我没有拍摄照片时成功改变了方向。我没有在任何图像视图中设置图像,而是在表面视图中显示它。这是我为其设置framelayout和surface的代码:

Framelayout camera_view = (FrameLayout) findViewById(R.id.camera_view);
CameraSurfaceCreater mCameraView = new CameraSurfaceCreater(this, mCamera, CameraSetter.this);//create a SurfaceView to show camera data
            camera_view.addView(mCameraView);//add the SurfaceView to the layout

拍摄照片时:

   Camera.PictureCallback mPicture = new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {

              pictaken = true;
            }
};

并在onConfiguration Changed:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            if(!isPictureTaken){
                setCameraDisplayOrientation(CameraSetter.this, 0, mCamera);
            }else {
               //dont know how to rotate a photo when picture is taken
            }

        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            if(!isPictureTaken){
                setCameraDisplayOrientation(CameraSetter.this, 0, mCamera);
            }

        }
    }

感谢任何帮助。我是否必须旋转framelayout或拍摄照片后我必须做的事情,并且用户更改方向。

1 个答案:

答案 0 :(得分:1)

使用OrientationEventListener,如下所示:

OrientationEventListener mOrientationEventListener = new OrientationEventListener(mApplication,
            SensorManager.SENSOR_DELAY_NORMAL) {

        @Override
        public void onOrientationChanged(int orientation) {

            if ((orientation == ORIENTATION_UNKNOWN) || (mCamera == null)) {
                return;
            }

            Log.e("current_ori", "" + orientation);

            Camera.Parameters params = mCamera.getParameters();
            Camera.CameraInfo info = new Camera.CameraInfo();

            Camera.getCameraInfo(cameraId, info);

            orientation = (orientation + 45) / 90 * 90;

            int rotation = 0;

            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {

                //Below one line will rotate image to portrait
                // rotation = (info.orientation - orientation + 360) % 360;

                //Below code makes image exactly how it has been captured(with mirror)

                if (orientation == 360 || orientation == 0 || orientation == 180) {
                    rotation = 270;
                } else if (orientation == 90 || orientation == 270) {
                    rotation = 90;
                }

            } else {
                /*
                 * back-facing camera
                 */
                //Below one line will rotate image to portrait
                //rotation = (info.orientation + orientation) % 360;

                //Below line makes image exactly how it has been captured
                rotation = 90;

            }

            params.setRotation(rotation);

            if (null == mCamera) {
                return;
            }

            mCamera.setParameters(params);
        }
    };

不要忘记启用和禁用OrientationEventListener:

启用:

if (mOrientationEventListener.canDetectOrientation()) {
        mOrientationEventListener.enable();
    }

要停用:

if (mOrientationEventListener.canDetectOrientation()) {
        mOrientationEventListener.disable();
    }