android - Initializing Camera with a button results in blank screen, initializing in onCreate works fine -


i working on app needs control camera, i've run issue when try initialize it.

if call function safeopencamera() inside oncreate() works fine , can see camera preview. if try call when press button, area preview should goes blank.

in both cases "camera started" gets logged, , no exceptions.

i've tried figure out few days can't find cause. need activate button user can choose camera use and, hopefully, swap them while app running.

main activity:

    public class mainactivity extends actionbaractivity {     private camera mcamera;     private camerapreview mpreview;     private view mcameraview;     private button ulbutton;     private int current_camera=1;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          ulbutton= (button) findviewbyid(r.id.ul_button);         ulbutton.setonclicklistener(new view.onclicklistener() {             public void onclick(view v) {                 // if try start camera button, preview area goeas blank                 boolean opened = safeopencamera((framelayout) findviewbyid(r.id.preview_layout), current_camera);                 if (opened == false) {                     log.d("camera", "error, camera failed open");                 } else {                     log.d("camera", "camera started");                 }             }         });          // if place code opening camera here, works fine: //                 /*boolean opened = safeopencamera((framelayout) findviewbyid(r.id.preview_layout), current_camera);         if (opened == false) {             log.d("camera", "error, camera failed open");         } else {             log.d("camera", "camera started");         }*/      }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, menu);         return true;     }      private boolean safeopencamera(view view, int camera_id){         log.d("safeopencamera","starting method");         boolean qopened=false;         releasecameraandpreview();         log.d("safeopencamera", "camera id " + camera_id);         mcamera=getcamerainstance(camera_id);         mcamera.setdisplayorientation(90);         mcameraview=view;         qopened=(mcamera!=null);         if(qopened==true) {             log.d("safeopencamera","qopened true");             mpreview = new camerapreview(getbasecontext(), mcamera,mcameraview);             framelayout preview = (framelayout) findviewbyid(r.id.preview_layout);             preview.addview(mpreview);             mpreview.startcamerapreview();             //additemsonspinner2(mpreview.getsizes());          }         return qopened;     }     public camera getcamerainstance(int camera_id){         camera c=null;         try{             c = camera.open(camera_id);         }catch (exception e){             e.printstacktrace();         }         return c;     }     private void releasecameraandpreview(){         if(mcamera!=null) {             mcamera.stoppreview();             mcamera.release();             mcamera = null;         }         //if(mpreview!=null){         //mpreview.destroydrawingcache();          //}     } } 

preview class:

    public class camerapreview extends surfaceview implements surfaceholder.callback {     private surfaceholder mholder;     private camera mcamera;     private context mcontext;     private camera.size mpreviewsize;     private list<camera.size> msupportedpreviewsizes;     private list<camera.size> msupportedsizes;     private list<string> msupportedflashmodes;     private view mcameraview;      public camerapreview(context context, camera camera, view cameraview){         super(context);          mcameraview=cameraview;         mcontext=context;         setcamera(camera);          mholder = getholder();         mholder.addcallback(this);         mholder.settype(surfaceholder.surface_type_push_buffers);     }      public void startcamerapreview(){         try{             log.d("terminal preview","try set preview");             mcamera.setpreviewdisplay(mholder);             log.d("terminal preview", "try start preview");             mcamera.startpreview();         }catch(exception e){             log.d("terminal preview","exception");             e.printstacktrace();         }     }      public list<camera.size> getsizes(){         return mcamera.getparameters().getsupportedpicturesizes();     }      private void setcamera(camera camera){         log.d("terminal preview","set camera");         mcamera=camera;         msupportedpreviewsizes=mcamera.getparameters().getsupportedpreviewsizes();         msupportedsizes=mcamera.getparameters().getsupportedpicturesizes();         msupportedflashmodes=mcamera.getparameters().getsupportedflashmodes();          camera.parameters parameters=mcamera.getparameters();         log.d("terminal preview","set rotation");         parameters.setrotation(90);          if(msupportedflashmodes!=null && msupportedflashmodes.contains(camera.parameters.flash_mode_auto)){             log.d("terminal preview","set flash mode");             parameters.setflashmode(camera.parameters.flash_mode_auto);          }         log.d("terminal preview","set params");         mcamera.setparameters(parameters);         log.d("terminal preview", "requesting layout...");         requestlayout();     }        public void surfacechanged(surfaceholder holder, int format, int w, int h){         log.d("terminal preview", "surface changed");         if (mholder.getsurface()==null){             log.d("terminal preview","mholder null, return");             return;         }         try{             log.d("terminal preview","try stop preview");             mcamera.stoppreview();         }catch (exception e){             log.d("terminal", "surfacechanged exception stoppreview");}          try{             camera.parameters parameters=mcamera.getparameters();              if (mcamera.getparameters().getsupportedfocusmodes().contains(camera.parameters.focus_mode_continuous_picture)) {                 log.d("terminal preview", "set focus mode");                 parameters.setfocusmode(camera.parameters.focus_mode_continuous_picture);             }              if(mpreviewsize!=null){                 camera.size previewsize=mpreviewsize;                 parameters.setpreviewsize(previewsize.width, previewsize.height);             }               mcamera.setparameters(parameters);             mcamera.startpreview();         }catch (exception e){             log.d("terminal","surfacechanged exception");             e.printstacktrace();         }     }      @override     public void surfacecreated(surfaceholder holder) {         try{             log.d("terminal preview", "try surfacecreated");             if(mcamera==null){log.d("terminal","mcamera es null");}             if(holder==null){log.d("terminal","holder es null");}             mcamera.setpreviewdisplay(holder);             mcamera.startpreview(); //         }catch (ioexception e   ){             log.d("terminal preview", "exception surfacecreated");             e.printstacktrace();         }     }     @override     public void surfacedestroyed(surfaceholder holder) {         log.d("terminal preview", "surface destroyed");         if (mcamera!=null){             try{                 mcamera.stoppreview();                 mcamera=null;             }catch (exception e){log.d("terminal preview", "exception surface destroyed");}         }     }      @override     protected void onmeasure(int widthmeasurespec, int heightmeasurespec){         log.d("terminal preview", "onmeassure");         final int width=resolvesize(getsuggestedminimumwidth(),widthmeasurespec);         final int height=resolvesize(getsuggestedminimumheight(), heightmeasurespec);         setmeasureddimension(width, height);         if (msupportedpreviewsizes!=null){             mpreviewsize=getoptimalpreviewsize(msupportedpreviewsizes,width,height);         }      }      @override     protected void onlayout(boolean changed, int left, int top, int right, int bottom) {         log.d("terminal preview", "onlayout");         if (changed) {              final int width=right-left;             final int height=top-bottom;             int previewwidth=width;             int previewheight=height;              if(mpreviewsize!=null){                 display display=((windowmanager)mcontext.getsystemservice(context.window_service)).getdefaultdisplay();                  switch (display.getrotation()){                     case surface.rotation_0:                         previewwidth=mpreviewsize.height;                         previewheight=mpreviewsize.width;                         mcamera.setdisplayorientation(90);                         break;                     case surface.rotation_90:                         previewwidth=mpreviewsize.width;                         previewheight=mpreviewsize.height;                         break;                     case surface.rotation_180:                         previewwidth=mpreviewsize.height;                         previewheight=mpreviewsize.width;                         break;                     case surface.rotation_270:                         previewwidth=mpreviewsize.width;                         previewheight=mpreviewsize.height;                         mcamera.setdisplayorientation(180);                         break;                 }             }             log.d("terminal","t,b,l,r: "+top+" "+bottom+" "+left+" "+right);             log.d("terminal","w: "+width);             log.d("terminal","h: "+height);              log.d("terminal","pw: "+previewwidth);             log.d("terminal","ph: "+previewheight);              final int scaledchildheight=previewheight*width/previewwidth;              log.d("terminal","h-sh: "+(height-scaledchildheight));              mcameraview.layout(0, height - scaledchildheight, width, height);          }     }       private camera.size getoptimalpreviewsize(list<camera.size> sizes, int width, int height){         log.d("terminal preview", "getoptimalpreviewsize");         camera.size optimalsize=null;         final double aspect_tolerance=0.1;         double targetratio = (double)height/width;          for(camera.size size: sizes){             if(size.height!=width) continue;             double ratio=(double)size.width/size.height;             if(ratio<=targetratio+aspect_tolerance && ratio >targetratio-aspect_tolerance){                 optimalsize=size;             }         }         if(optimalsize==null){          }         return optimalsize;     }   } 

this log when camera initialized in oncreate() (the 1 works)

/safeopencamera﹕ starting method /safeopencamera﹕ camera id 1 /dalvikvm﹕ note: class lcom/lge/mdm/manager/ilgmdmdevicepolicymanager$stub; has 235 unimplemented (abstract) methods /safeopencamera﹕ qopened true /terminal preview﹕ set camera /terminal preview﹕ set rotation /terminal preview﹕ set params /terminal preview﹕ requesting layout... /terminal preview﹕ try set preview /camera﹕ app passed null surface /terminal preview﹕ try start preview /camera﹕ camera started /terminal preview﹕ onmeassure /terminal preview﹕ getoptimalpreviewsize /terminal preview﹕ onmeassure /terminal preview﹕ getoptimalpreviewsize /libegl﹕ loaded /vendor/lib/egl/libegl_powervr_sgx540_120.so /libegl﹕ loaded /vendor/lib/egl/libglesv1_cm_powervr_sgx540_120.so /libegl﹕ loaded /vendor/lib/egl/libglesv2_powervr_sgx540_120.so /openglrenderer﹕ enabling debug mode 0 /terminal preview﹕ onlayout /terminal﹕ t,b,l,r: 0 300 0 492 /terminal﹕ w: 492 /terminal﹕ h: -300 /terminal﹕ pw: 492 /terminal﹕ ph: -300 /terminal﹕ h-sh: 0 /terminal preview﹕ onlayout /terminal preview﹕ try surfacecreated /terminal preview﹕ surface changed /terminal preview﹕ try stop preview /terminal preview﹕ onmeassure /terminal preview﹕ getoptimalpreviewsize /terminal preview﹕ onmeassure /terminal preview﹕ getoptimalpreviewsize /terminal preview﹕ onlayout 

here's log when try initialize button (the 1 doesn't work)

/viewrootimpl﹕ viewroot  touchdown(absolute) down (105 , 202) /safeopencamera﹕ starting method /safeopencamera﹕ camera id 1 /dalvikvm﹕ note: class lcom/lge/mdm/manager/ilgmdmdevicepolicymanager$stub; has 235 unimplemented (abstract) methods /safeopencamera﹕ qopened true /terminal preview﹕ set camera /terminal preview﹕ set rotation /terminal preview﹕ set params /terminal preview﹕ requesting layout... /terminal preview﹕ try set preview /camera﹕ app passed null surface /terminal preview﹕ try start preview /camera﹕ camera started /terminal preview﹕ onmeassure /terminal preview﹕ getoptimalpreviewsize /terminal preview﹕ onmeassure /terminal preview﹕ getoptimalpreviewsize /terminal preview﹕ onlayout /terminal﹕ t,b,l,r: 0 300 0 492 /terminal﹕ w: 492 /terminal﹕ h: -300 /terminal﹕ pw: 492 /terminal﹕ ph: -300 /terminal﹕ h-sh: 0 /terminal preview﹕ onlayout /terminal preview﹕ try surfacecreated /terminal preview﹕ surface changed /terminal preview﹕ try stop preview 

the differences between logs seem when preview successfuly drawn onmeassure() gets called, , loading of libraries: libegl_powervr_sgx540_120.so, libglesv1_cm_powervr_sgx540_120.so , libglesv2_powervr_sgx540_120.so, , don't know whether related camera or not.

in layout i've set preview area have colored background, turns white when try initialize preview button, addview() function that's not working properly.

the source code can found here in case has time check it: dropbox

after looking through code. problem on onlayout call.

if ignore changed param (always call code), stack overflow because there infinite amount of layout calls. problem in logic of how using onlayout. can't dive further right (took long enough here haha). removing function causes both button , non button scenario work.

so @ calling change in layout code , change logic of how want size layout. problem posted here, code below. remove function , code run. should going again! (offending code below)

@override protected void onlayout(boolean changed, int left, int top, int right, int bottom) {     if (changed) {          final int width=right-left;         final int height=top-bottom;         int previewwidth=width;         int previewheight=height;          if(mpreviewsize!=null){              display display=((windowmanager)mcontext.getsystemservice(context.window_service)).getdefaultdisplay();              switch (display.getrotation()){                 case surface.rotation_0:                     previewwidth=mpreviewsize.height;                     previewheight=mpreviewsize.width;                     mcamera.setdisplayorientation(90);                     break;                 case surface.rotation_90:                     previewwidth=mpreviewsize.width;                     previewheight=mpreviewsize.height;                     break;                 case surface.rotation_180:                     previewwidth=mpreviewsize.height;                     previewheight=mpreviewsize.width;                     break;                 case surface.rotation_270:                     previewwidth=mpreviewsize.width;                     previewheight=mpreviewsize.height;                     mcamera.setdisplayorientation(180);                     break;             }         }          final int scaledchildheight=previewheight*width/previewwidth;          mcameraview.layout(0, height - scaledchildheight, width, height);      }     } 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -