find Camera - Android android.hardware

Android examples for android.hardware:Camera State

Description

find Camera

Demo Code

import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.Log;

public class Main {

  public static int cameraId = -1;
  public static String TAG = "CameraUtility";

  public static int findCamera(String side) {

    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    // int rotation = context.getWindowManager().getDefaultDisplay()
    // .getRotation();
    for (int i = 0; i < numberOfCameras; i++) {
      CameraInfo info = new CameraInfo();
      Camera.getCameraInfo(i, info);/*from w  ww .  jav  a2  s. co  m*/
      if (side == "FRONT") {
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
          Log.d(TAG, "Front Camera found: ID @" + i);
          cameraId = i;

          break;
        }
      }
      /*
       * else if(side == "BACK"){ if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
       * Log.d(TAG, "Back Camera found: ID @"+i); cameraId = i; break; } }
       */
      else {
        if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
          Log.d(TAG, "Back Camera found: ID @" + i);
          cameraId = i;
          break;
        }
      }

    }
    return cameraId;
  }

}

Related Tutorials