Android how to fix camera orientation

前端 未结 4 1095
滥情空心
滥情空心 2020-12-13 13:52

Notice how the view of the camera (NOT THE CAPTURED IMAGE) was flipped to left (image above), the or

4条回答
  •  有刺的猬
    2020-12-13 14:31

    This problem was solved a long time ago but I encountered some difficulties to put all pieces together so here is my final solution, I hope this will help others :

    public void startPreview() {
            try {
                Log.i(TAG, "starting preview: " + started);
    
                // ....
                Camera.CameraInfo camInfo = new Camera.CameraInfo();
                Camera.getCameraInfo(cameraIndex, camInfo);
                int cameraRotationOffset = camInfo.orientation;
                // ...
    
                Camera.Parameters parameters = camera.getParameters();
                List previewSizes = parameters.getSupportedPreviewSizes();
                Camera.Size previewSize = null;
                float closestRatio = Float.MAX_VALUE;
    
                int targetPreviewWidth = isLandscape() ? getWidth() : getHeight();
                int targetPreviewHeight = isLandscape() ? getHeight() : getWidth();
                float targetRatio = targetPreviewWidth / (float) targetPreviewHeight;
    
                Log.v(TAG, "target size: " + targetPreviewWidth + " / " + targetPreviewHeight + " ratio:" + targetRatio);
                for (Camera.Size candidateSize : previewSizes) {
                    float whRatio = candidateSize.width / (float) candidateSize.height;
                    if (previewSize == null || Math.abs(targetRatio - whRatio) < Math.abs(targetRatio - closestRatio)) {
                        closestRatio = whRatio;
                        previewSize = candidateSize;
                    }
                }
    
                int rotation = getWindowManager().getDefaultDisplay().getRotation();
                int degrees = 0;
                switch (rotation) {
                case Surface.ROTATION_0:
                    degrees = 0;
                    break; // Natural orientation
                case Surface.ROTATION_90:
                    degrees = 90;
                    break; // Landscape left
                case Surface.ROTATION_180:
                    degrees = 180;
                    break;// Upside down
                case Surface.ROTATION_270:
                    degrees = 270;
                    break;// Landscape right
                }
                int displayRotation;
                if (isFrontFacingCam) {
                    displayRotation = (cameraRotationOffset + degrees) % 360;
                    displayRotation = (360 - displayRotation) % 360; // compensate
                                                                        // the
                                                                        // mirror
                } else { // back-facing
                    displayRotation = (cameraRotationOffset - degrees + 360) % 360;
                }
    
                Log.v(TAG, "rotation cam / phone = displayRotation: " + cameraRotationOffset + " / " + degrees + " = "
                        + displayRotation);
    
                this.camera.setDisplayOrientation(displayRotation);
    
                int rotate;
                if (isFrontFacingCam) {
                    rotate = (360 + cameraRotationOffset + degrees) % 360;
                } else {
                    rotate = (360 + cameraRotationOffset - degrees) % 360;
                }
    
                Log.v(TAG, "screenshot rotation: " + cameraRotationOffset + " / " + degrees + " = " + rotate);
    
                Log.v(TAG, "preview size: " + previewSize.width + " / " + previewSize.height);
                parameters.setPreviewSize(previewSize.width, previewSize.height);
                parameters.setRotation(rotate);
                camera.setParameters(parameters);
                camera.setPreviewDisplay(mHolder);
                camera.startPreview();
    
                Log.d(TAG, "preview started");
    
                started = true;
            } catch (IOException e) {
                Log.d(TAG, "Error setting camera preview: " + e.getMessage());
            }
        }
    

提交回复
热议问题