fix Camera Degree - Android Camera

Android examples for Camera:Camera Feature

Description

fix Camera Degree

Demo Code

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.Log;
import android.view.Surface;
import android.view.WindowManager;

public class Main {
  private final static String TAG = "CameraUtils";
  private final static boolean DEBUG = true;

  public static void fixCameraDegree(Camera cam, int cameraId, Context ctx) {
    int rotation = getDisplayRotation(ctx);
    CameraInfo cameraInfo = new CameraInfo();
    Camera.getCameraInfo(cameraId, cameraInfo);
    int result;/*from w w w .ja  v a 2s  .co m*/
    if (CameraInfo.CAMERA_FACING_FRONT == cameraInfo.facing) {
      result = (cameraInfo.orientation + rotation) % 360;
      result = (360 - result) % 360;
    } else {
      result = (cameraInfo.orientation - rotation + 360) % 360;
    }
    cam.setDisplayOrientation(result);

    if (DEBUG) {
      Log.d(TAG, "fixCameraDegree : " + result + "; cameraInfo="
          + cameraInfo.orientation);
    }
  }

  private static int getDisplayRotation(Context ctx) {
    WindowManager wm = (WindowManager) ctx
        .getSystemService(Context.WINDOW_SERVICE);

    int rotation;

    switch (wm.getDefaultDisplay().getOrientation()) {

    case Surface.ROTATION_90:
      rotation = 90;
      break;

    case Surface.ROTATION_180:
      rotation = 180;
      break;

    case Surface.ROTATION_270:
      rotation = 270;
      break;

    default:
      rotation = 0;
      break;
    }
    if (DEBUG) {
      Log.d(TAG, "getDisplayRotation : " + rotation);
    }
    return rotation;
  }
}

Related Tutorials