java - Android cameraSource.stop() causing app to freeze -
i building app has qr scanner using google vision api. having trouble stopping camera after qr code read. flow mainactivity -> qractivity
once qr-code received detection app should return main activity.
if not call camerasource.release()
works fine device heats lot , has significant impact on battery drain. if release camera source mainactivity becomes un-responsive , app crash.
why becoming unresponsive? , correct place release camera source?
qractivity
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_qr); cancelbtn = (button) findviewbyid(r.id.cancel_button); cancelbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { onbackpressed(); } }); new qrreader(this); }
qrreader class
public class qrreader { private static final string tag = "qrreader"; private surfaceview cameraview; private textview barcodeinfo; private barcodedetector barcodedetector; private camerasource camerasource; private activity mactivity; private accesspointcredentials barcodedata; public qrreader(activity activity) { this.mactivity = activity; cameraview = (surfaceview) mactivity.findviewbyid(r.id.camera_view); barcodeinfo = (textview) mactivity.findviewbyid(r.id.code_info); barcodedetector = new barcodedetector.builder(mactivity) .setbarcodeformats(barcode.qr_code) .build(); camerasource = new camerasource .builder(mactivity, barcodedetector) .setautofocusenabled(true) .build(); cameraview.getholder().addcallback(new surfaceholder.callback() { @override public void surfacecreated(surfaceholder holder) { camerasource = new camerasource .builder(mactivity, barcodedetector) .setautofocusenabled(true) .setfacing(0) .build(); try { camerasource.start(cameraview.getholder()); } catch (exception ioe) { ioe.printstacktrace(); } } @override public void surfacechanged(surfaceholder holder, int format, int width, int height) { } @override public void surfacedestroyed(surfaceholder holder) { // log.i(tag, "surfacedestroyed: stopping camera source"); // camerasource.release(); } }); barcodedetector.setprocessor(new detector.processor<barcode>() { @override public void release() { log.i(tag, "release: "); } @override public void receivedetections(detector.detections<barcode> detections) { final sparsearray<barcode> barcodes = detections.getdetecteditems(); if (barcodes.size() != 0) { log.i(tag, "received barcode"); barcodeinfo.post(new runnable() { // use post method of textview public void run() { barcodeinfo.settext(barcodes.valueat(0).displayvalue); } }); gson g = new gson(); try { barcodedata = g.fromjson(barcodes.valueat(0).rawvalue, accesspointcredentials.class); } catch (exception e) { barcodedata = new accesspointcredentials(); barcodedata.setssid(barcodes.valueat(0).rawvalue); barcodedata.setpass(null); e.printstacktrace(); } connecttowifi(barcodedata); // camerasource.release causes app freeze // camerasource.release(); } } }); } private void connecttowifi(final accesspointcredentials credentials) { //wificonnect code } }
it's been 3 months stumbled on same problem , figure out. code inside receivedetections method running on different thread if want needs ui thread need post handler:
handler handler = new handler(looper.getmainlooper()); handler.post(new runnable() { @override public void run() { camerasource.release(); } });
Comments
Post a Comment