get Front Camera Id by checking if device has any front camera - Android android.hardware

Android examples for android.hardware:Front Camera

Description

get Front Camera Id by checking if device has any front camera

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 getFrontCameraId(Context c) {
    if (hasFrontCamera(c)) {
      return getCameraId(CameraInfo.CAMERA_FACING_FRONT);
    }// www . j  a  va  2s .c  o m
    return -1;
  }

  @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  public static boolean hasFrontCamera(Context c) {
    PackageManager pm = c.getPackageManager();
    return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
  }

  @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