14

I have created a camera app and I want my app to be turned in all 4 possible orientations and the to update the camera preview accordingly. for that I have used the following method I have copied from: Android - Camera preview is sideways

 public void updateCameraDisplay(int w, int h) {
        // set preview size and make any resize, rotate or
        // reformatting changes here

        Log.i("CameraPreviews", "Updating camera orientation with w=" + w
                + " and h=" + h);
        Parameters parameters = camera.getParameters();
        Display display = getActivity().getWindowManager()
                .getDefaultDisplay();

        int rotation = getActivity().getResources().getConfiguration().orientation;
        Log.i("CameraPreviews", "rotation is " + display.getRotation());
        if (display.getRotation() == Surface.ROTATION_0) {

            parameters.setPreviewSize(h, w);
            camera.setDisplayOrientation(0);
        }

        if (display.getRotation() == Surface.ROTATION_90) {
            parameters.setPreviewSize(w, h);
            camera.setDisplayOrientation(270);
        }

        if (display.getRotation() == Surface.ROTATION_180) {
            parameters.setPreviewSize(h, w);
            camera.setDisplayOrientation(180);
        }

        if (display.getRotation() == Surface.ROTATION_270) {
            parameters.setPreviewSize(w, h);
            camera.setDisplayOrientation(90);
        }

        try {
            camera.setParameters(parameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

I have tweeked the values, testing them on the samsung galaxy tab2 to finally get the right orientations and it all works. When I tried it on htc one s phone it doesn't work at all!!!!! All e orientations are totally wrong! So I have arrived to the conclusion that thre must be 2 type of devices (or more... please no!) because the rotation represents how many degrees the screen has been rotated from its "default" position then some devices have one default position and others another. How could I find out about this default rotation and act accordingly in my code? ej: defaultOrientation=some code if(defaultOrientation==0) ... else ....

locking screen orientation is out of question. target api>=11 thanks a lot

EDIT: I have modified my code to:

public void updateCameraDisplay(int w, int h) {
        // set preview size and make any resize, rotate or
        // reformatting changes here

        Log.i("CameraPreviews", "Updating camera orientation with w=" + w
                + " and h=" + h);
        Parameters parameters = camera.getParameters();
        Display display = getActivity().getWindowManager()
                .getDefaultDisplay();

        int rotation = getActivity().getResources().getConfiguration().orientation;
        Log.i("CameraPreviews", "screen rotation is " + rotation);
        Log.i("CameraPreviews", "display rotation is " + display.getRotation());
        if (display.getRotation() == Surface.ROTATION_0) {

            if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
                parameters.setPreviewSize(h, w);
                camera.setDisplayOrientation(0);
            } else {
                parameters.setPreviewSize(h, w);
                camera.setDisplayOrientation(90);
            }
        }

        else if (display.getRotation() == Surface.ROTATION_90) {
            if (rotation == Configuration.ORIENTATION_PORTRAIT) {
                parameters.setPreviewSize(w, h);
                camera.setDisplayOrientation(270);
            } else {
                parameters.setPreviewSize(w, h);
                //camera.setDisplayOrientation(0);
            }
        }

        else if (display.getRotation() == Surface.ROTATION_180) {
            if (rotation == Configuration.ORIENTATION_LANDSCAPE) {
            parameters.setPreviewSize(h, w);
            camera.setDisplayOrientation(180);
            }else {
                parameters.setPreviewSize(h, w);
                camera.setDisplayOrientation(270);
            }
        }

        else if (display.getRotation() == Surface.ROTATION_270) {
            if (rotation == Configuration.ORIENTATION_PORTRAIT) {
                parameters.setPreviewSize(w, h);
                camera.setDisplayOrientation(90);
            } else {
                parameters.setPreviewSize(w, h);
                camera.setDisplayOrientation(180);
            }
        }

        try {
            camera.setParameters(parameters);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

works better on htc one s and samsung galaxy tab as long as we don't rotate the phone in the portrait mode upside down

3
  • 1
    I think for proper implementation you need to calculate device height/width as well...Using combination of angle and device current height/width you will able to figure out exact view position
    – dreamcoder
    Mar 18, 2014 at 11:21
  • Use ExifInterface to get the orientation ..it is working fine for me but I have not tested my code in any HTC device.
    – Biplab
    Mar 22, 2014 at 9:19
  • 2 types of devices: stackoverflow.com/a/35431231/192373
    – Alex Cohn
    May 21, 2016 at 14:58

1 Answer 1

0

the factors you need to think about are: the orientation of the device, the degree between the screen and the camera, assuming you are using the back camera, and whether you enable the activity to sense the orientation change.

And one important thing is whether the Camera HAL code implemented by the manufacturer is compliant with Google's protocol.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.