【问题标题】:Android how to rotate a photo depending on orientation when picture is taken custom cameraAndroid如何在拍摄照片时根据方向旋转照片自定义相机
【发布时间】:2017-06-01 10:47:21
【问题描述】:

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

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 中改变了:

@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);
            }

        }
    }

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

【问题讨论】:

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


    【解决方案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();
        }
    

    【讨论】:

    • 感谢您的回答。但是我应该在哪里添加这个 OrientationEventListener 在代码中?还是我应该删除 onconfigurationchanged?
    • 您可以在 onCreate() 中添加此代码,但请确保在相机初始化后使用此代码。从 onResume() 启用它并从 onPause() 禁用。
    • 这有一个问题,当您拍照并将设备移动到横向图片时,它会再次打开相机。
    • 你是在同一个活动中展示拍摄的照片吗?
    • 打开相机并旋转相机而不是旋转图片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 2012-05-22
    相关资源
    最近更新 更多