Front Facing Camera on Android?

#1

Hey guys,

First of all, I want to say I’m enjoying ofAndroid, thanks for the port. However, I’m doing an app for Android Tablets and I want to use the front facing camera (on the same side as the screen, not the back side). I’ve tried listDevices() with ofVideoGrabber, but it doesn’t seem to do anything. I’ve tried passing different ids to setDeviceID(), but it doesn’t seem to have any effect.

Any suggestions on what I should do to get the other camera? Also, I’m planning on making an iPad version, so is there a function for iOS?

Thanks,
Matt

#2

I got it. Not as clean as it could be, but it works. First, the project needs to compile to Android 2.3.1 at the minimum, so you need to add this line:

target=android-9

to ant.properties.

Then, you need to make a copy of OFAndroidVideoGrabber.java from ofAndroidLib and put it into your srcJava folder, in the cc.openframeworks package.

Then, you’ll need to modify it a little. Add an import:

import android.hardware.Camera.CameraInfo;

and then a function:

public int getFrontCameraId() {
CameraInfo ci = new CameraInfo();
for (int i = 0 ; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, ci);
if (ci.facing == CameraInfo.CAMERA_FACING_FRONT) return i;
}
return -1; // No front-facing camera found
}

Then change initGrabber to use the function above:
void initGrabber(int w, int h, int _targetFps){
camera = Camera.open(getFrontCameraId());

That’s it! There’s probably a way to incorporate thing better into OF, but this works for me for now.

Matt

#3

thank you so much for this @madparker – much appreciated!

after implementing your suggestion, unfortunately I’m getting a dex error due to the original OFAndroidVideoGrabber and the overridden one both being included. I’ve spent quite a bit of time trying to drop “exclude” statements in several gradle locations to no avail.

Any advice on solving this last piece of the puzzle?

#4

An alternative I worked out in the meantime is simply to assume the front-facing is the second camera in the devices array, which works for now (although is a bit less elegant):

vid.setDeviceID(vid.listDevices()[1].id); // front facing camera

Note this setting has to be done post-constructor, pre-init – else init defaults to first device in the array and set device doesn’t change that afterward.

Anyway, sorry to bug you 7 years on, but I’d love to hear how you cracked it.

#5

Heh, it’s been a long time :slight_smile:

Glad you were able to sort it out.