fix Camera Degree - Android android.hardware

Android examples for android.hardware:Camera Feature

Description

fix Camera Degree

Demo Code

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

public class Main {

  private final static String TAG = "CameraUtils";

  public static void fixCameraDegree(Camera cam, int cameraId, Context ctx) {
    int rotation = getDisplayRotation(ctx);
    CameraInfo cameraInfo = new CameraInfo();
    Camera.getCameraInfo(cameraId, cameraInfo);
    int result;// w  w  w  . j a va 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);

  }

  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;
    }

    return rotation;
  }

}

Related Tutorials