1

I have used this script to open front face camera:

private Camera openFrontFacingCameraGingerbread() {
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_FRONT) {
        try {
            cam = Camera.open(camIdx);
        } catch (RuntimeException e) {
            Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
        }
    }
}

I think this script should select and open the front camera on all devices. But on my devices is script working properly only on device with two cameras (front + back camera). On other device with one front camera I receive only blank screen. Is there any difference to access to camera with one front camera only ?

mira
  • 1,056
  • 3
  • 15
  • 32

2 Answers2

1

"On other device with one front camera I receive only blank screen."

By "receiv[ing] only blank screen", do you mean from the Camera Preview? Opening camera by calling Camera.open(cameraId) does not automatically give you the preview screen. Please refer to startPreview() method from this sample tutorial for an example of starting camera preview.

In addition, make sure your device doesn't have any hardware issues (confirm your codes by testing on stock-camera app to see if successfully opens camera preview or on different devices).

"But on my devices is script working properly only on device with two cameras (front + back camera)."

The values of camIdx and cameraCount are valid and as intended; therefore, it should be working properly. Regardless of the total number of the camera that device has, the code you provided in your question only opens front camera, Evaluating your code:

If you have one camera:

cameraCount = Camera.getNumberOfCameras(); // cameraCount == 1
// in for loop
cam = Camera.open(camIdx); // camIdx == 0

If you have two cameras:

cameraCount = Camera.getNumberOfCameras(); // cameraCount == 2
// ... more codes
// in for loop
cam = Camera.open(camIdx); // camIdx == 1

"Is there any difference to access to camera with one front camera only ?"*

There shouldn't be any difference in calling Camera object and its associated methods when dealing with different devices with different specs. As far as Android application development is concerned, all we need is to retrieve Camera.CameraInfo object by calling Camera.CameraInfo() and to call its associated methods when needed.

melvynkim
  • 1,655
  • 3
  • 25
  • 38
0

This method should give you the most front camera available. If the device only has a rear camera, you'll get that one. If there are two, you'll get the front one. If there's just one on the front, you'll get that one:

private Camera getCameraInstance() {
    Camera c = null;
    try {
        // get the front-est camera
        c = Camera.open(Camera.getNumberOfCameras() - 1);
    } catch (Exception e) {
        Log.d(TAG, "Couldn't get the camera");
    }
    return c;
}
visum
  • 357
  • 3
  • 10