Skip to content

Commit

Permalink
ofxAndroidVideoGrabber: fix for not supported fps
Browse files Browse the repository at this point in the history
  • Loading branch information
arturoc committed Jul 29, 2011
1 parent fc5c3f2 commit 58211a2
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
Expand Up @@ -7,6 +7,7 @@
import android.content.Context; import android.content.Context;
import android.graphics.ImageFormat; import android.graphics.ImageFormat;
import android.hardware.Camera; import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log; import android.util.Log;
import android.view.OrientationEventListener; import android.view.OrientationEventListener;


Expand All @@ -30,23 +31,51 @@ public static OFAndroidVideoGrabber getInstance(int id){
void initGrabber(int w, int h, int _targetFps){ void initGrabber(int w, int h, int _targetFps){
camera = Camera.open(); camera = Camera.open();
Camera.Parameters config = camera.getParameters(); Camera.Parameters config = camera.getParameters();

Log.i("OF","Grabber supported sizes");
for(Size s : config.getSupportedPreviewSizes()){
Log.i("OF",s.width + " " + s.height);
}

Log.i("OF","Grabber supported formats");
for(Integer i : config.getSupportedPreviewFormats()){
Log.i("OF",i.toString());
}

Log.i("OF","Grabber supported fps");
for(Integer i : config.getSupportedPreviewFrameRates()){
Log.i("OF",i.toString());
}

Log.i("OF", "Grabber default format: " + config.getPreviewFormat()); Log.i("OF", "Grabber default format: " + config.getPreviewFormat());
Log.i("OF", "Grabber default preview size: " + config.getPreviewSize().width + "," + config.getPreviewSize().height); Log.i("OF", "Grabber default preview size: " + config.getPreviewSize().width + "," + config.getPreviewSize().height);
config.setPreviewSize(w, h); config.setPreviewSize(w, h);
config.setPreviewFormat(ImageFormat.NV21); config.setPreviewFormat(ImageFormat.NV21);
config.setPreviewFrameRate(targetFps);
try{ try{
camera.setParameters(config); camera.setParameters(config);
}catch(Exception e){ }catch(Exception e){
Log.e("OF","couldn init camera", e); Log.e("OF","couldn init camera", e);
} }

config = camera.getParameters(); config = camera.getParameters();
width = config.getPreviewSize().width; width = config.getPreviewSize().width;
height = config.getPreviewSize().height; height = config.getPreviewSize().height;
if(width!=w || height!=h) Log.w("OF","camera size different than asked for, resizing (this can slow the app)");


if(_targetFps!=-1){
config = camera.getParameters();
config.setPreviewFrameRate(_targetFps);
try{
camera.setParameters(config);
}catch(Exception e){
Log.e("OF","couldn init camera", e);
}
}

targetFps = _targetFps; targetFps = _targetFps;
Log.i("OF","camera settings: " + width + "x" + height); Log.i("OF","camera settings: " + width + "x" + height);
if(width!=w || height!=h) Log.w("OF","camera size different than asked for, resizing (this can slow the app)");
buffer = new byte[width*height*2]; buffer = new byte[width*height*2];


orientationListener = new OrientationListener(OFAndroid.getContext()); orientationListener = new OrientationListener(OFAndroid.getContext());
Expand Down Expand Up @@ -76,27 +105,14 @@ public void stop(){


@Override @Override
public void pause(){ public void pause(){
if(initialized){ stop();
Log.i("OF","pausing camera preview");
camera.stopPreview();
orientationListener.disable();
}


} }


@Override @Override
public void resume(){ public void resume(){
if(initialized){ if(initialized){
switch(state){ initGrabber(width,height,targetFps);
case Paused:
Log.i("OF","camera paused, resuming");
camera.startPreview();
break;
case Stopped:
Log.i("OF","camera stopped, reinitializing");
initGrabber(width,height,targetFps);
break;
}
orientationListener.enable(); orientationListener.enable();
} }
} }
Expand Down Expand Up @@ -155,19 +171,23 @@ public OrientationListener(Context context) {
@Override @Override
public void onOrientationChanged(int orientation) { public void onOrientationChanged(int orientation) {
if (orientation == ORIENTATION_UNKNOWN) return; if (orientation == ORIENTATION_UNKNOWN) return;
Camera.Parameters config = camera.getParameters(); try{
/*Camera.CameraInfo info = Camera.Parameters config = camera.getParameters();
new Camera.CameraInfo();*/ /*Camera.CameraInfo info =
//Camera.getCameraInfo(camera, info); new Camera.CameraInfo();*/
orientation = (orientation + 45) / 90 * 90; //Camera.getCameraInfo(camera, info);
int rotation = orientation % 360; orientation = (orientation + 45) / 90 * 90;
//if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { int rotation = orientation % 360;
//rotation = (info.orientation - orientation + 360) % 360; //if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
/*} else { // back-facing camera //rotation = (info.orientation - orientation + 360) % 360;
rotation = (info.orientation + orientation) % 360; /*} else { // back-facing camera
}*/ rotation = (info.orientation + orientation) % 360;
config.setRotation(rotation); }*/
camera.setParameters(config); config.setRotation(rotation);
camera.setParameters(config);
}catch(Exception e){

}
} }


} }
Expand Down
2 changes: 1 addition & 1 deletion addons/ofxAndroid/src/ofxAndroidVideoGrabber.cpp
Expand Up @@ -111,7 +111,7 @@ static void releaseJavaObject(){


ofxAndroidVideoGrabber::ofxAndroidVideoGrabber(){ ofxAndroidVideoGrabber::ofxAndroidVideoGrabber(){


attemptFramerate = 30; attemptFramerate = -1;
newPixels = false; newPixels = false;
InitConvertTable(); InitConvertTable();
bGrabberInited = false; bGrabberInited = false;
Expand Down

0 comments on commit 58211a2

Please sign in to comment.