Android: Camera Asynctask with Preview Callback -


i've managed camera preview custom filter (grayscale, hue, etc) working. custom filter applied preview callback manipulating array of rgb , drawing canvas display @ surface view.

the drawback low fps. low fps, it's doing work in ui thread if don't in background thread using asynctask. tried use asynctask camera operation (my main purpose ui still working heavy work camera preview callback).

but after used asynctask, didn't much. wondering implementation wrong or because asynctask ui thread still affected?

snippet of code below:

cameraactivity.java

public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     log.d("activity_lifecycle","cameraactivity: oncreate");     setcontentview(r.layout.camera_layout); }  @targetapi(build.version_codes.honeycomb) @override protected void onresume() {     log.d("activity_lifecycle","cameraactivity: onresume");     if(preview == null){         preview = new camerapreviewasync(this,camera);         preview.execute();     }     super.onresume(); }  @override protected void onpause() {     log.d("activity_lifecycle","cameraactivity: onpause");     if(preview!=null){         preview.cancel(true);         camera = preview.getcamera();         if(camera!=null){             camera.stoppreview();             camera.setpreviewcallback(null);             camera.release();             camera = null;             preview.setcamera(camera);         }         preview = null;     }     super.onpause(); }  @override public void ondestroy(){     log.d("activity_lifecycle","cameraactivity: ondestroy");     super.ondestroy(); }  

camerapreviewasync.java:

private final string tag = "camerapreviewasync";  private cameraactivity camact; private camera mcamera; private int cameraid; private surfaceview msurfaceview; private surfaceholder mholder;  private boolean ispreviewrunning = false; private int[] rgbints; private int width; private int height; private bitmap mbitmap;  public camerapreviewasync(cameraactivity act, camera cam){     this.camact = act;     this.mcamera = cam;     this.msurfaceview = (surfaceview) act.findviewbyid(r.id.surfaceview); }  public void resetsurface(){     if(mcamera!=null){         mcamera.stoppreview();         mcamera.setpreviewcallback(null);         mcamera.release();         mcamera = null;     }     int tempid = r.id.surfaceview;     relativelayout buttonbar = (relativelayout) camact.findviewbyid(r.id.buttonbar);     ((relativelayout) camact.findviewbyid(r.id.preview)).removeallviews();      surfaceview newsurface = new surfaceview(camact);     newsurface.setid(tempid);     relativelayout.layoutparams layparams = new relativelayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent);     layparams.alignwithparent = true;     newsurface.setlayoutparams(layparams);     ((relativelayout) camact.findviewbyid(r.id.preview)).addview(newsurface);     ((relativelayout) camact.findviewbyid(r.id.preview)).addview(buttonbar); }  @override protected void onpreexecute() {     //things before doinbackground executed     log.d(tag,"onpreexecute");      relativelayout.layoutparams layparams = new relativelayout.layoutparams(layoutparams.match_parent, layoutparams.match_parent);     layparams.alignwithparent = true;     msurfaceview.setlayoutparams(layparams);      //check number of camera in device, if less 2 remove swap button     if (camera.getnumberofcameras() < 2) {         ((relativelayout) camact.findviewbyid(r.id.buttonbar)).removeviewat(r.id.cameraswap);     }      //opening camera     cameraid = findbackfacingcamera();     if (cameraid < 0) {         cameraid = findfrontfacingcamera();         if (cameraid < 0)             toast.maketext(camact, "no camera found.", toast.length_long).show();         else             mcamera = camera.open(cameraid);     } else {         mcamera = camera.open(cameraid);     }      //invalidate menu bar , show menu appropriately     camact.invalidateoptionsmenu();      // camera parameters , set auto focus     if(mcamera!=null){         camera.parameters params = mcamera.getparameters();         list<string> focusmodes = params.getsupportedfocusmodes();         if (focusmodes.contains(camera.parameters.focus_mode_auto)) {             // set focus mode             params.setfocusmode(camera.parameters.focus_mode_auto);             // set camera parameters             mcamera.setparameters(params);         }     }      super.onpreexecute(); }  @override protected void doinbackground(void... params) {     //things in background thread     log.d(tag,"doinbackground");      mholder = msurfaceview.getholder();     mholder.addcallback(surfacecallback);      return null; }        @override protected void onpostexecute(void values) {     //things after doinbackground     log.d(tag,"onpostexecute");  }  @override protected void oncancelled(){     super.oncancelled(); }  /*  * ************************************************************************************  * surfaceholder callback  * ************************************************************************************  */ surfaceholder.callback surfacecallback = new surfaceholder.callback() {      @override     public void surfacecreated(surfaceholder holder) {         log.d(tag,"surfacecreated!!");         if(cameraactivity.filtermode == cameraactivity.normal_filter){             try {                 if (mcamera != null) {                     mcamera.startpreview();                     mcamera.setpreviewdisplay(holder);                 }else{                     log.d(tag,"camera null in surfacecreated!!");                 }             } catch (ioexception exception) {                 log.e(tag, "ioexception caused setpreviewdisplay()", exception);             }            }else{             synchronized(msurfaceview){                 if(ispreviewrunning){                     return;                 }else{                                            msurfaceview.setwillnotdraw(false);                     if(mcamera!=null){                         ispreviewrunning = true;                         camera.parameters p = mcamera.getparameters();                         list<size> sizes = p.getsupportedpreviewsizes();                          size size = p.getpreviewsize();                         width = size.width;                         height = size.height;                          p.setpreviewformat(imageformat.nv21);                         showsupportedcameraformats(p);                         mcamera.setparameters(p);                          rgbints = new int[width * height];                          mcamera.startpreview();                         mcamera.setpreviewcallback(previewcallback);                     }                 }             }         }     }      @override     public void surfacedestroyed(surfaceholder holder) {         log.d(tag,"surfacedestroyed!");          if(cameraactivity.filtermode == cameraactivity.normal_filter){             if (mcamera != null) {                 mcamera.stoppreview();                 ispreviewrunning = false;             }         }else{             synchronized(msurfaceview){                 if(mcamera!=null){                     mcamera.setpreviewcallback(null);                     mcamera.stoppreview();                     ispreviewrunning = false;                 }             }         }     }      @override     public void surfacechanged(surfaceholder holder, int format, int width,             int height) {         log.d(tag,"surfacechanged!");     } };   /*  * ************************************************************************************  * camera preview callback  * ************************************************************************************  */  camera.previewcallback previewcallback = new camera.previewcallback() {      @override     public void onpreviewframe(byte[] data, camera camera) {         if (!ispreviewrunning)             return;         canvas rescanvas = null;          if (mholder == null) {             return;         }          try {             synchronized (mholder) {                 rescanvas = mholder.lockcanvas(null);                 int rescanvasw = rescanvas.getwidth();                 int rescanvash = rescanvas.getheight();                  if(mbitmap == null){                     mbitmap =  bitmap.createbitmap (width, height, bitmap.config.argb_8888);                 }                  decodeyuv(rgbints, data, width, height);                  canvas canvas = new canvas(mbitmap);                  //setting filter                 if(camact.getcustomfilter().equalsignorecase("normal")) ;//don't change rgb value                 if(camact.getcustomfilter().equalsignorecase("grayscale")) rgbints = grayscale(rgbints);                 if(camact.getcustomfilter().equalsignorecase("invert")) rgbints = invert(rgbints);                 if(camact.getcustomfilter().equalsignorecase("boostred")) rgbints = boostcolor(rgbints,1);                 if(camact.getcustomfilter().equalsignorecase("boostgreen")) rgbints = boostcolor(rgbints,2);                 if(camact.getcustomfilter().equalsignorecase("boostblue")) rgbints = boostcolor(rgbints,3);                 if(camact.getcustomfilter().equalsignorecase("noise")) rgbints = noise(rgbints);                 if(camact.getcustomfilter().equalsignorecase("hue")) rgbints = hue(rgbints);                 if(camact.getcustomfilter().equalsignorecase("saturation")) rgbints = saturation(rgbints);                 if(camact.getcustomfilter().equalsignorecase("engrave")) rgbints = engrave(rgbints);                 if(camact.getcustomfilter().equalsignorecase("emboss")) rgbints = emboss(rgbints);                  // draw decoded image, centered on canvas                 canvas.drawbitmap(rgbints, 0, width, 0,0, width, height, false, null);                  rescanvas.drawbitmap (mbitmap, rescanvasw-((width+rescanvasw)>>1), rescanvash-((height+rescanvash)>>1),null);             }         }  catch (exception e){             e.printstacktrace();         } {             // in if exception thrown             // during above, don't leave surface in             // inconsistent state             if (rescanvas != null) {                 mholder.unlockcanvasandpost(rescanvas);             }         }     } }; 

any appreciated! :) in advance guys!

callbacks other methods delivered event loop of thread called open(). if thread has no event loop, callbacks delivered main application event loop. if there no main application event loop, callbacks not delivered. source


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -