Android Camera setDisplayOrientation does not work

后端 未结 4 1094
春和景丽
春和景丽 2020-12-17 01:07

i am working camera project in android.my problem is camera setDisplayOrientation method is not working and my camera preview on surface always landscape.i want

4条回答
  •  情书的邮戳
    2020-12-17 01:43

    For lower API levels you could use:

    private void setCameraDisplayOrientationAPI8(){
            //Sets the camera right Orientation.
            //Special void for API 8 build.
            //This void should be called before calling camera.setParameters(cameraParameters).
            if (activeActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
            {   
                camera.setDisplayOrientation(90);
            }
            if (activeActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {                               
                camera.setDisplayOrientation(180);
            }
        }
    

    and just call setCameraDisplayOrientationAPI8(); on your surfaceChanged func.

    plus:

    private void setCameraDisplayOrientation() {
            //Sets the camera right Orientation.
            //IMPORTANT!! This code is available only for API Level 9 build or greater.
            if (mApiLvl<9){
                Log.d(TAG, "setCameraDisplayOrientation ERROR: This code is available only for API Level 9 build or greater.");
                return;
            }
            android.hardware.Camera.CameraInfo info =
                    new android.hardware.Camera.CameraInfo();
            android.hardware.Camera.getCameraInfo(cameraIndex, info);
            int rotation = activeActivity.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);
        }
    

    hope this helps.

提交回复
热议问题