Preview camera frame not capturing Android -


i have code setup (obviously not correctly seems ok)

my test code capturing frame this:

framecallback = new camera.previewcallback() {         public void onpreviewframe(byte[] data, camera camera) {             system.out.println("preview frame captured"); 

but onpreviewframe never ran, print statement tests.

the weird thing is, preview showing on surface. i.e- can see on screen , there no errors. why isn't onpreviewframe being called then?

here code reference:

public class huntactivity extends appcompatactivity implements surfaceholder.callback{      camera camera;     surfaceview surfaceview;     surfaceholder surfaceholder;     camera.previewcallback framecallback;      @override     protected void oncreate(bundle savedinstancestate) {          super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_hunt);         surfaceview = (surfaceview) findviewbyid(r.id.surfaceview);         surfaceholder = surfaceview.getholder();         surfaceholder.addcallback(this);         surfaceholder.settype(surfaceholder.surface_type_push_buffers);          framecallback = new camera.previewcallback() {             public void onpreviewframe(byte[] data, camera camera) {                  system.out.println("preview frame captured");             }         };     }      @override     public void surfacecreated(surfaceholder holder) {          try {camera = camera.open();         } catch (runtimeexception e) {             system.err.println(e);             return;         }         camera.setpreviewcallback(framecallback);          camera.parameters param;         param = camera.getparameters();         param.setpreviewsize(800, 480);         camera.setdisplayorientation(90);         camera.setparameters(param);         try {             camera.setpreviewdisplay(surfaceholder);             camera.startpreview();         } catch (exception e) {             system.err.println(e);             return;         }      }      @override     public void surfacechanged(surfaceholder holder, int format, int width, int height) {         refreshcamera();     }      @override     public void surfacedestroyed(surfaceholder holder) {         camera.stoppreview();         camera.release();         camera = null;     }      public void refreshcamera() {         if (surfaceholder.getsurface() == null) {return;}         try {camera.stoppreview();         } catch (exception e){}         try {             camera.setpreviewdisplay(surfaceholder);             camera.startpreview();         } catch (exception e) {}     } 

refreshcamera(); 

do not call function there. stop preview functionality before can use it.

move following code onsurfacechanged() , use width , heigth parameters

    camera.parameters param;     param = camera.getparameters();     //param.setpreviewsize(800, 480);             param.setpreviewsize(width, height);     ... , rest of lines... 

never hardcode resolutions 800x480


Comments