set Camera Display Orientation by Camera Id - Android android.hardware

Android examples for android.hardware:Camera Orientation

Description

set Camera Display Orientation by Camera Id

Demo Code

import android.hardware.Camera;
import android.view.Surface;

public class Main {

  static void setCameraDisplayOrientation(final int rotation, final int cameraId, final Camera camera,
      final Camera.Parameters params) {
    int orientation = getCameraRotation(rotation, cameraId);
    camera.setDisplayOrientation(orientation);
    params.setRotation(orientation);//from   w  ww . java 2s .c  o  m
  }

  static int getCameraRotation(final int rotation, final int cameraId) {
    final Camera.CameraInfo info = new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int degrees = 0;
    switch (rotation) {
    case Surface.ROTATION_0:
      degrees = 0;
      break;
    case Surface.ROTATION_90:
      degrees = 90;
      break;
    case Surface.ROTATION_180:
      degrees = 180;
      break;
    case Surface.ROTATION_270:
      degrees = 270;
      break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
      result = (info.orientation + degrees) % 360;
      return (360 - result) % 360; // compensate the mirror
    } else { // back-facing
      return (info.orientation - degrees + 360) % 360;
    }
  }

}

Related Tutorials