android - How start SurfaceHolder.Callback() event if Button Clicked? -


i tried start surfacecallback of camera if button clicked, cannot , if did camera.setpreviewcallback() not invoked although works in invoked surfaceholder.callback() oncreat()method shown in code below

public class heartratemonitor extends actionbaractivity {  private static final string tag = "heartratemonitor"; private static surfaceview preview = null; private static surfaceholder previewholder = null; private static camera camera ; private static view image = null; private static textview text = null; private static wakelock wakelock = null; private static long starttime = 0; static context context; static circlebutton cb ; static int txt ;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);     placeholderfragment  p = new placeholderfragment() ;     if (savedinstancestate == null) {         getsupportfragmentmanager().begintransaction()                 .add(r.id.contain, p)                 .commit();     }     preview = (surfaceview) findviewbyid(r.id.preview);     previewholder = preview.getholder();     previewholder.addcallback(surfacecallback);     previewholder.settype(surfaceholder.surface_type_push_buffers);      image = findviewbyid(r.id.image);     text = (textview) findviewbyid(r.id.text);     powermanager pm = (powermanager) getsystemservice(context.power_service);     wakelock = pm.newwakelock(powermanager.full_wake_lock, "donotdimscreen");     heartratemonitor.context = getapplicationcontext(); }    class placeholderfragment extends fragment implements view.onclicklistener {      // fragment circular button .     view rootview ;     public placeholderfragment() {     }     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);     }     @targetapi(build.version_codes.jelly_bean)     @override     public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         rootview = inflater.inflate(r.layout.start, container, false);         cb = (circlebutton) rootview.findviewbyid(r.id.startbutton);         cb.setimageresource(r.drawable.start);         cb.isclickable();         cb.findviewbyid(r.id.startbutton).setonclicklistener(this);         return rootview;     }     @override     public void onclick(view v) {        // toast.maketext(getactivity(), "button clicked", toast.length_short).show();         cb.setimageresource(r.drawable.stop);         start();     } }   public void start(){     // here want start preview . }   @override protected void onresume() {     super.onresume();     wakelock.acquire();     camera = camera.open();     starttime = system.currenttimemillis(); }  @override public void onpause() {     super.onpause();     wakelock.release();     camera = null; }   private static previewcallback previewcallback = new previewcallback() {      @override     public void onpreviewframe(byte[] data, camera cam) {       // process preview data here .     } };    private static surfaceholder.callback surfacecallback = new surfaceholder.callback() {     @override     public void surfacecreated(surfaceholder holder) {          try {             camera.setpreviewdisplay(previewholder);             camera.setpreviewcallback(previewcallback);         } catch (throwable t) {         }     }     @override     public void surfacechanged(surfaceholder holder, int format, int width, int height) {         camera.parameters parameters = camera.getparameters();         parameters.setflashmode(camera.parameters.flash_mode_torch);         camera.size size = getsmallestpreviewsize(width, height, parameters);         if (size != null) {             parameters.setpreviewsize(size.width, size.height);         }         camera.setparameters(parameters);         camera.startpreview();     }     @override     public void surfacedestroyed(surfaceholder holder) {         // ignore     } };     private static camera.size getsmallestpreviewsize(int width, int height, camera.parameters parameters) {     camera.size result = null;      (camera.size size : parameters.getsupportedpreviewsizes()) {         if (size.width <= width && size.height <= height) {             if (result == null) {                 result = size;             } else {                 int resultarea = result.width * result.height;                 int newarea = size.width * size.height;                 if (newarea < resultarea) result = size;             }         }     }     return result; } } 

if delay calling previewholder.addcallback(), actual events of surfacecreated() , surfacechanged() fired usual, callbacks won't called.

you can startpreview @ time after preview surface created , bound (i.e. after call camera.setpreviewdisplay(previewholder)). can setpreviewcallback when need data. can set preview transparent, or hide behind other view.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -