Camera preview is in portrait mode but image captured is rotated

后端 未结 3 1409
挽巷
挽巷 2020-12-01 03:29

I am trying to capture a photo using the camera. The preview by default was in landscape mode which i could change it to portrait mode using

    setCameraDis         


        
相关标签:
3条回答
  • 2020-12-01 03:37

    I faced the same problem when taking photo from camera in portrait mode. Below lines of code solved my problem:

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

    Call setCameraDisplayOrientation() method in surfaceCreated callback as the following:

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        camera = Camera.open();
        setCameraDisplayOrientation(getActivity(), CameraInfo.CAMERA_FACING_BACK, camera);
    }
    

    I had to rotate the image in Camera onPictureTaken() callback:

    camera.takePicture(null, null, new PictureCallback() {
    
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
    
                if (data != null) {
                    int screenWidth = getResources().getDisplayMetrics().widthPixels;
                    int screenHeight = getResources().getDisplayMetrics().heightPixels;
                    Bitmap bm = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);
    
                    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                        // Notice that width and height are reversed
                        Bitmap scaled = Bitmap.createScaledBitmap(bm, screenHeight, screenWidth, true);
                        int w = scaled.getWidth();
                        int h = scaled.getHeight();
                        // Setting post rotate to 90
                        Matrix mtx = new Matrix();
                        mtx.postRotate(90);
                        // Rotating Bitmap
                        bm = Bitmap.createBitmap(scaled, 0, 0, w, h, mtx, true);
                    }else{// LANDSCAPE MODE
                        //No need to reverse width and height
                        Bitmap scaled = Bitmap.createScaledBitmap(bm, screenWidth,screenHeight , true);
                        bm=scaled;
                    }
                    photoPreview.setImageBitmap(bm);
                }
                isImageCaptured = true;
                photoPreview.setVisibility(View.VISIBLE);
                surfaceView.setVisibility(View.GONE);
            }
    });
    
    0 讨论(0)
  • 2020-12-01 03:39

    Below code worked for me for Front facing camera.

    if (data != null) {
                    try {
                        int screenWidth = getResources().getDisplayMetrics().widthPixels;
                        int screenHeight = getResources().getDisplayMetrics().heightPixels;
                        bm = BitmapFactory.decodeByteArray(data, 0,
                                (data != null) ? data.length : 0);
                        CameraInfo info = new CameraInfo();
                        Camera.getCameraInfo(cameraFace, info);
                        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                            // Notice that width and height are reversed
                            // Bitmap scaled = Bitmap.createScaledBitmap(bm,
                            // screenHeight, screenWidth, true);
                            // int w = scaled.getWidth();
                            // int h = scaled.getHeight();
                            // Setting post rotate to 90
                            Matrix mtx = new Matrix();
                            mtx.postRotate(90);
                            if (cameraFace == CameraInfo.CAMERA_FACING_FRONT)
                                mtx.postRotate(180);
                            // Rotating Bitmap
                            bm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
                                    bm.getHeight(), mtx, true);
                        } else {// LANDSCAPE MODE
                            Bitmap scaled = Bitmap.createScaledBitmap(bm,
                                    screenWidth, screenHeight, true);
                            bm = scaled;
                        }
                    } catch (Exception e) {
                    } catch (Error e) {
                    }
                }
    
    0 讨论(0)
  • 2020-12-01 03:55

    Yes, I tried the way in the answer, it works for back camera, for front camera, it need to rotate 270 not 90. :)

    0 讨论(0)
提交回复
热议问题