Skip to content Skip to sidebar Skip to footer

Check Which Camera Is Open Front Or Back Android

I know i can set a boolean flag while opening front Camera. And if flag is true it means front camera is on. But is there a way using Android API to know which Camera is Open righ

Solution 1:

privatebooleansafeCameraOpen(int id) {
    boolean qOpened = false;

    try {
        releaseCameraAndPreview();
        mCamera = Camera.open(id);
        qOpened = (mCamera != null);
    } catch (Exception e) {
        Log.e(getString(R.string.app_name), "failed to open Camera");
        e.printStackTrace();
    }

    return qOpened;    
}

privatevoidreleaseCameraAndPreview() {
    mPreview.setCamera(null);
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

Since API level 9, the camera framework supports multiple cameras. If you use the legacy API and call open() without an argument, you get the first rear-facing camera.Android devices can have multiple cameras, for example a back-facing camera for photography and a front-facing camera for video calls. Android 2.3 (API Level 9) and later allows you to check the number of cameras available on a device using the Camera.getNumberOfCameras() method. To access the primary camera, use the Camera.open() method and be sure to catch any exceptions, as shown in the code below:

/** A safe way to get an instance of the Camera object. */publicstatic Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

On devices running Android 2.3 (API Level 9) or higher, you can access specific cameras using Camera.open(int). The example code above will access the first, back-facing camera on a device with more than one camera.

Solution 2:

In new android.hardware.camera2 package, you can enquire from CameraCharacteristics.LENS_FACING property and each CameraDevice publishes its id with CameraDevice.getId() it's easy to get to the characteristics.

In the older camera API, I think the only way is to keep track of the index you opened it with.

privateint cameraId;

publicvoidopenFrontCamera(){
   cameraId = getFrontCameraId();
   if (cameraId != -1)
     camera = Camera.open(cameraId); //try catch omitted for brevity
}

Then use cameraId later, this little snippet might be a better way of achieving what you are trying to:

publicvoidonOrientationChanged(int orientation) {
     if (orientation == ORIENTATION_UNKNOWN) return;
     android.hardware.Camera.CameraInfoinfo=newandroid.hardware.Camera.CameraInfo();
     android.hardware.Camera.getCameraInfo(cameraId, info);
     orientation = (orientation + 45) / 90 * 90;
     introtation=0;
     if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
         rotation = (info.orientation - orientation + 360) % 360;
     } else {  // back-facing camera
         rotation = (info.orientation + orientation) % 360;
     }
     mParameters.setRotation(rotation);
 }

Solution 3:

if you have a custom Camera Activity you can try this method.

boolean inPreview;

in your on surfaceChanged method from surfaceView set

inPreview = True;

in your Camera.CallbackListener variable set

inPreview = false;

Post a Comment for "Check Which Camera Is Open Front Or Back Android"