Jonah G Jonah G - 1 year ago 93
Android Question

How to open camera with a portrait orientation?

My whole android camera app is in portrait mode, so when I open the camera I need it to automatically open in portrait mode. When I open the camera now the preview is sideways. How do I set the camera preview to open in portrait mode so the preview will look right?

Answer Source

You could use this method from the Android Developer documentation to rotate the camera preview.

public final void setDisplayOrientation (int degrees)

Set the clockwise rotation of preview display in degrees. This affects the preview frames and the picture displayed after snapshot. This method is useful for portrait mode applications. Note that preview display of front-facing cameras is flipped horizontally before the rotation, that is, the image is reflected along the central vertical axis of the camera sensor. So the users can see themselves as looking into a mirror.

private 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()

    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
        degrees = 0;
    case Surface.ROTATION_90:
        degrees = 90;
    case Surface.ROTATION_180:
        degrees = 180;
    case Surface.ROTATION_270:
        degrees = 270;

    int result = 0;

    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;

I hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download