configure Camera Parameters - Android android.hardware

Android examples for android.hardware:Camera

Description

configure Camera Parameters

Demo Code

import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Build;
import android.util.Log;
import android.view.Surface;

public class Main {

  private static final String CAMERA_PARAM_LANDSCAPE = "landscape";
  private static final String CAMERA_PARAM_PORTRAIT = "portrait";
  private static Camera mCamera;
  private static final String CAMERA_PARAM_ORIENTATION = "orientation";
  private static int mCameraAngle;

  public static void configureCameraParameters(Context context, int rotation) {
    Parameters cameraParams = mCamera.getParameters();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) { // for 2.1 and before
      if (isPortrait(context)) {
        cameraParams.set(CAMERA_PARAM_ORIENTATION, CAMERA_PARAM_PORTRAIT);
        mCameraAngle = 90;//w ww.  j a v a 2 s.c  o  m
      } else {
        cameraParams.set(CAMERA_PARAM_ORIENTATION, CAMERA_PARAM_LANDSCAPE);
        mCameraAngle = 0;
      }
    } else { // for 2.2 and later
      switch (rotation) {
      case Surface.ROTATION_0: // This is display orientation
        mCameraAngle = 90; // This is camera orientation
        break;
      case Surface.ROTATION_90:
        mCameraAngle = 0;
        break;
      case Surface.ROTATION_180:
        mCameraAngle = 270;
        break;
      case Surface.ROTATION_270:
        mCameraAngle = 180;
        // image
        break;
      default:
        mCameraAngle = 90;
        break;
      }
      mCamera.setDisplayOrientation(mCameraAngle);
    }

    cameraParams.setRecordingHint(true);
    mCamera.setParameters(cameraParams);
  }

  public static boolean isPortrait(Context context) {
    return (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT);
  }

}

Related Tutorials