Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting lack of rear camera

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!

like image 898
androidneil Avatar asked Jan 18 '13 12:01

androidneil


1 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.

like image 184
Sahil Mahajan Mj Avatar answered Sep 27 '22 19:09

Sahil Mahajan Mj