EXIF orientation tag value always 0 for image taken with portrait camera app android

前端 未结 4 509
南方客
南方客 2020-11-27 16:24

I have a camera app in portrait mode which takes pictures from both front and back end cameras.I am saving the image in my sd card and try to find the corresponding exif val

4条回答
  •  忘掉有多难
    2020-11-27 17:13

    This problem was solved a long time ago but I encountered some difficulties to make camera work correctly so here is my final solution, without using exif. 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());
            }
        }
    

提交回复
热议问题