get Rear Camera Id - Android android.hardware

Android examples for android.hardware:Back Camera

Description

get Rear Camera Id

Demo Code

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.os.Build;

public class Main {

  @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  public static int getRearCameraId(Context c) {
    if (hasRearCamera(c)) {
      return getCameraId(CameraInfo.CAMERA_FACING_BACK);
    }/*from  www  . java 2s  .  c om*/
    return -1;
  }

  public static boolean hasRearCamera(Context c) {
    PackageManager pm = c.getPackageManager();
    return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
  }

  @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  private static int getCameraId(int facing) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
      Camera.CameraInfo info = new Camera.CameraInfo();
      for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
        Camera.getCameraInfo(i, info);
        if (info.facing == facing) {
          return i;
        }
      }
    }
    return 0;
  }

}

Related Tutorials