Home » Android » android – Detecting lack of rear camera

android – Detecting lack of rear camera

Posted by: admin June 15, 2020 Leave a comment

Questions:

My app has functionality that requires a rear camera. Whether there is a front camera or not is irrelevant to my needs. Putting together a robust routine to detect whether or not a rear camera exists, in all circumstances, is proving tricky. For example, a user with an HTC Evo 3D has complained that the app says there’s no rear camera (there clearly is), and I’ve had a number of similar complaints from other users.

This is a tricky thing to test, as despite having a number of devices I don’t have a device with only a front camera, such as the Nexus 7, or any of the models mentioned by the users.

Here’s what I have, and this was taken from code on other answers on this site:

boolean rearCameraFound = false;
    if(BUILD_VERSION > 8){
        int cameraCount = 0;
        Camera cam = null;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
            Camera.getCameraInfo( camIdx, cameraInfo );
            if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK  ) {
                try {
                    cam = Camera.open( camIdx );
                    Log.d("TAG", "Rear camera detected");
                    rearCameraFound = true;
                } catch (RuntimeException e) {
                    Log.e("TAG", "Camera failed to open: " + e.getLocalizedMessage());
                }
            }
            if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ) {
                Log.d("TAG", "Front camera detected");
            }
        }
        return rearCameraFound;
    }else{
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

I’ve now replaced this code with this much simpler version:

PackageManager pm = context.getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);

However, I don’t know what would happen on the Nexus 7 for example, with only a front camera. Would this return true?

I’m looking for code that will tell me for sure if there’s a rear camera or not!

How to&Answers:

Nexus 7 (which has only one frontal camera) returns false to FEATURE_CAMERA, Instead of it, you can use FEATURE_CAMERA_FRONT. Check out this discussion.

Now, by using the above, you could make sure that there is atleast one camera. So now, you can check the number of camera’s present in the phone, if it is greater than one, then there will be surely a rear camera.

import android.hardware.Camera;

int numCameras = Camera.getNumberOfCameras();
if (numCameras > 1) {
  rearCamera = true;
}

This is quite tricky. But that’s all, I can now think of. Just give it a try.

Answer:

Use this snippet. Note this only works for API 9 or higher.

private static boolean checkCameraFacing(final int facing) {
    if (getSdkVersion() < Build.VERSION_CODES.GINGERBREAD) {
        return false;
    }
    final int cameraCount = Camera.getNumberOfCameras();
    CameraInfo info = new CameraInfo();
    for (int i = 0; i < cameraCount; i++) {
        Camera.getCameraInfo(i, info);
        if (facing == info.facing) {
            return true;
        }
    }
    return false;
}

public static boolean hasBackFacingCamera() {
    final int CAMERA_FACING_BACK = 0;
    return checkCameraFacing(CAMERA_FACING_BACK);
}

public static boolean hasFrontFacingCamera() {
    final int CAMERA_FACING_BACK = 1;
    return checkCameraFacing(CAMERA_FACING_BACK);
}

public static int getSdkVersion() {
    return android.os.Build.VERSION.SDK_INT;
}

And this is no need to open the camera.

Answer:

For front camera

Context context = this; //If its Activity

// Context context=getActivity(); If its a Fragment

PackageManager packageManager = context.getPackageManager();

if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {

    Log.i("CameraLog", "Front Camera Available");

} else {

    Log.i("CameraLog", "No Front Camera Available");
      }

for back camera

Context context = this; //If its an Activity

// Context context=getActivity(); If its a fragments 

PackageManager packageManager = context.getPackageManager();

if (packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

    Log.i("CameraLog", "Back Camera is Available");

} else {

    Log.i("CameraLog", "No Back Camera Available");

      }

Answer:

If device has one camera only, it can have back camera only, or the front camera only, first check if back camera exists:

  • Check if front camera exists, but devices with two cameras has front camera obviously
  • Check if device has one camera only, but it can have back or front camera
  • Final result: If device has the front camera and it has one camera only, back camera no exists
private boolean backCameraExists() {
    boolean isFrontExists = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
    if (isFrontExists && (Camera.getNumberOfCameras()<2)) {
        return false;
    }
    return true;
}