9

How do I detect if the device has a camera on the BACK?

This is my code to detect if the device has a Flashlight:

if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH) 
    || (_camera.getParameters().getSupportedFlashModes() != null)) hasFlashlight = true;
else hasFlashlight = false;

But it is TRUE even if the device only has a Front Camera without the flashlight.

Yousha Aleayoub
  • 4,532
  • 4
  • 53
  • 64
  • This SO Answer should suit you: http://stackoverflow.com/questions/14399333/detecting-lack-of-rear-camera – Alboz Sep 29 '14 at 19:52
  • BTW, can i use `Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);` just to Open the BACK camera? – Yousha Aleayoub Sep 29 '14 at 20:02
  • 1
    "can i use Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK); just to Open the BACK camera?" -- no, as the parameter to `open()` is not a property like `CAMERA_FACING_BACK`. – CommonsWare Sep 29 '14 at 20:07

4 Answers4

22

For API >= 9 : you can use Camera.getCameraInfo with something like this :

int backCameraId = -1;
for(int i=0;i<Camera.getNumberOfCameras();i++){
    CameraInfo cameraInfo = new CameraInfo();
    Camera.getCameraInfo(i,cameraInfo);
    if(cameraInfo.facing==CameraInfo.CAMERA_FACING_BACK) {
        backCameraId = i;
        break;
    }
}
Log.d(TAG, "back camera exists ? "+(backCameraId>-1));
Log.d(TAG, "back camera id  :"+backCameraId);

For API >= 21, you are advised to use the Camera2 API :

String backCameraId = null;
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
for(String cameraId:manager.getCameraIdList()){
    CameraCharacteristics cameraCharacteristics = manager.getCameraCharacteristics(cameraId);
    Integer facing = cameraCharacteristics.get(CameraCharacteristics.LENS_FACING);
    if(facing==CameraMetadata.LENS_FACING_BACK) {
        backCameraId = cameraId;
        break;
    }
}
Log.d(TAG, "back camera exists ? "+(backCameraId!=null));
Log.d(TAG, "back camera id  :"+backCameraId);
ben75
  • 29,217
  • 10
  • 88
  • 134
  • Can i use these codes in Android 2.2 with @ SuppressLint or @ TargetApi? – Yousha Aleayoub Sep 29 '14 at 21:08
  • 1
    This code requires API 9+ (2.2.3) Be aware that Android 2.2 is about 0.7% of the market (https://developer.android.com/about/dashboards/index.html). So if you really need 2.2 compatibility you need to guard this code (see this : http://developer.android.com/training/basics/supporting-devices/platforms.html) – ben75 Sep 30 '14 at 17:47
  • Great! I'd just add **break;** after line *backCameraId = i;* and replace "camera id :" with **"camera index :"** – 1111161171159459134 Jun 18 '15 at 01:36
  • 1
    Note that the Camera class has been deprecated in favor of the android.hardware.camera2 API – eliasbagley Mar 02 '16 at 22:51
1

Try check the number of Cameras:

import android.hardware.Camera;

int numberOfCameras = Camera.getNumberOfCameras();

If it is greater than 1, pretty sure got a rear camera.

Peter Pei Guo
  • 7,770
  • 18
  • 35
  • 54
  • 1
    Thanks, but `getNumberOfCameras()` is NOT for Android 2.2 or less... ("Cannot find symbol") – Yousha Aleayoub Sep 29 '14 at 20:05
  • 5
    @YoushaAleayoub: There was no front-facing camera support for Android 2.2 and earlier. Hence, on those devices, if it has a camera, it is a rear-facing camera. That being said, do not just assume that the number of cameras indicates if one is rear-facing -- use ben75's approach in the other answer. – CommonsWare Sep 29 '14 at 20:08
  • 3
    @YoushaAleayoub If you require it to work on Android 2.2 and older then you should add that to your question. Android 2.3 is really the oldest version that most developers would care about. – Intrications Sep 29 '14 at 20:09
  • 1
    Thanks both, "If it is greater than 1, pretty sure got a rear camera." means `getNumberOfCameras()` will check the FrontCamera AT FIRST? – Yousha Aleayoub Sep 29 '14 at 20:12
  • 1
    There is a huge risk to miss back camera on devices with only one back camera. This solution is very wrong. – ben75 Sep 29 '14 at 20:29
0
public boolean isRearCameraAvailable(Context context) {
    return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
}

Checks if the device has a camera facing away from the device user

Ali Hassan
  • 198
  • 3
  • 10
0
CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

 /** Which way the camera is facing. */
    public static enum Facing {
        FRONT, BACK;
    }

 /** Returns the ID of the first camera facing the given direction. */
    private String findCameraId(Facing facing) {
        if (facing == Facing.FRONT) {
            return findFirstFrontCameraId();
        } else {
            return findFirstBackCameraId();
        }
    }

    /** Returns the ID of the first back-facing camera. */
    private String findFirstBackCameraId() {
        Log.d(TAG, "Getting First BACK Camera");
        String cameraId = findFirstCameraIdFacing(CameraCharacteristics.LENS_FACING_BACK);
        if (cameraId == null) {
            Log.w(TAG, "No back-facing camera found.");
        }
        return cameraId;
    }

    /** Returns the ID of the first front-facing camera. */
    private String findFirstFrontCameraId() {
        Log.d(TAG, "Getting First FRONT Camera");
        String cameraId = findFirstCameraIdFacing(CameraCharacteristics.LENS_FACING_FRONT);
        if (cameraId == null) {
            Log.w(TAG, "No front-facing camera found.");
        }
        return cameraId;
    }


    /** Returns the ID of the first camera facing the given direction. */
    private String findFirstCameraIdFacing(int facing) {
        try {
            String[] cameraIds = mCameraManager.getCameraIdList();
            for (String cameraId : cameraIds) {
                CameraCharacteristics characteristics = mCameraManager
                      .getCameraCharacteristics(cameraId);
                if (characteristics.get(CameraCharacteristics.LENS_FACING) == facing) {
                    return cameraId;
                }
            }
        } catch (CameraAccessException ex) {
            Log.w(TAG, "Unable to get camera ID", ex);
        }
        return null;
    }
nkalra0123
  • 2,281
  • 16
  • 17