OpenCV Android Getting Front Camera From JavaCameraView (Bug #2839)


Added by Eliezer Graber about 12 years ago. Updated almost 12 years ago.


Status:Done Start date:2013-02-27
Priority:Normal Due date:
Assignee:Alexander Smorkalov % Done:

0%

Category:android
Target version:2.4.6
Affected version:branch '2.4' Operating System:
Difficulty: HW Platform:
Pull request:https://github.com/Itseez/opencv/pull/800

Description

On certain devices (especially tablets) there is only a front-facing camera. Therefore, choosing 1 as the camera index will result in an error (since there is only 1 camera, it will be 0). My proposed solution is that if mCameraIndex is set to 1 check the CameraInfo to find the front-facing camera (if there is one). I have tested this with devices that only have a front camera, and it works. This proposal is based off of the 2.4.4 beta, in JavaCameraView.java, method initializeCamera:

if (mCameraIndex == -1) {
Log.d(TAG, "Trying to open camera with old open()");
try {
mCamera = Camera.open();
}
catch (Exception e){
Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage());
}
if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
boolean connected = false;
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) {
Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(camIdx) + ")");
try {
mCamera = Camera.open(camIdx);
connected = true;
} catch (RuntimeException e) {
Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage());
}
if (connected) break;
}
}
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
Log.d(TAG, "Trying to open camera with new open(" + Integer.valueOf(mCameraIndex) + ")");
//here is where the proposed solution begins
if(mCameraIndex == 1) {
int count = 0;
Camera.CameraInfo cInfo = new Camera.CameraInfo();
count = Camera.getNumberOfCameras();
for(int i = 0; i < count; i++) {
Camera.getCameraInfo(i, cInfo);
if(cInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
mCamera = Camera.open(i);
break;
} catch(Exception e) {
Log.e(TAG, "Camera #" + mCameraIndex + "failed to open: " + e.getLocalizedMessage());
break;
}
}
}
}
else {
try {
mCamera = Camera.open(mCameraIndex);
} catch (RuntimeException e) {
Log.e(TAG, "Camera #" + mCameraIndex + "failed to open: " + e.getLocalizedMessage());
}
}
}
}

Related issues

related to Bug #3032: Using camera_id-tag in layout XML forces crash on Nexus 7 Cancelled 2013-05-17 2013-05-17

Associated revisions

Revision 071daa1d
Added by Alexander Alekhin almost 11 years ago

Merge pull request #2839 from ilya-lavrenov:tapi_fix

History

Updated by Alexander Shishkov about 12 years ago

  • Priority changed from High to Normal

Updated by Andrey Kamaev almost 12 years ago

  • Target version set to 2.4.6

Updated by Alexander Smorkalov almost 12 years ago

Camera facing check added to both Java and Native cameras.

  • Pull request set to https://github.com/Itseez/opencv/pull/800

Updated by Alexander Smorkalov almost 12 years ago

  • Status changed from Open to Done

Also available in: Atom PDF