android - Camera2API adding ImageReader as a target surface laggs my camera preview -
i'm trying process frames use of imagereader while displaying video camera preview on surfaceview. if target surface add surfaceview preview smooth, if add imagereader second target preview starts lag heavily. why be? tried create handlerthread , handler , use those, didn't change thing. think i'm doing acquire , close next image.
public void startbackgroundthread() { handlerthread = new handlerthread("image processing thread"); handlerthread.start(); handler = new handler(handlerthread.getlooper()); }
configuring camera here:
public void configurecamera() { try { cameracharacteristics characteristics = cameramanager.getcameracharacteristics(cameraid); streamconfigurationmap configs = characteristics.get(cameracharacteristics.scaler_stream_configuration_map); size[] sizes = configs.getoutputsizes(imageformat.yuv_420_888); imagereader = imagereader.newinstance(sizes[0].getwidth(), sizes[0].getheight(), imageformat.yuv_420_888, 1); range<integer>[] ranges = characteristics.get(cameracharacteristics.control_ae_available_target_fps_ranges); fpsrange = ranges[ranges.length - 1]; imagereader.setonimageavailablelistener(this, handler); camerasurfaceview.getholder().setfixedsize(sizes[0].getwidth(), sizes[0].getheight()); } catch (cameraaccessexception | nullpointerexception e) { e.printstacktrace(); } }
starting camera preview:
private void startcamera() { try { cameramanager.opencamera("0", new cameradevice.statecallback() { @override public void onopened(@nonnull cameradevice camera) { cameradevice = camera; try { cameradevice.createcapturesession(arrays.aslist(camerasurfaceview.getholder().getsurface(), imagereader.getsurface()), new cameracapturesession.statecallback() { @override public void onconfigured(@nonnull cameracapturesession session) { capturesession = session; try { requestbuilder = cameradevice.createcapturerequest(cameradevice.template_preview); requestbuilder.addtarget(camerasurfaceview.getholder().getsurface()); requestbuilder.addtarget(imagereader.getsurface()); requestbuilder.set(capturerequest.flash_mode, capturerequest.flash_mode_torch); requestbuilder.set(capturerequest.control_ae_target_fps_range, fpsrange); capturerequest = requestbuilder.build(); cameraready = true; capturesession.setrepeatingrequest(capturerequest, null, handler); onstartbuttonclick(startbutton); } catch (cameraaccessexception e) { e.printstacktrace(); } } @override public void onconfigurefailed(@nonnull cameracapturesession session) { } }, null); } catch (cameraaccessexception e) { e.printstacktrace(); } }
and here comes imagereader's onimageavailablelistener (which main activity currently):
@override public void onimageavailable(imagereader reader) { reader.acquirenextimage().close(); }
these code snippets reside in main activity currently. phone using motorola moto x play testing purposes. camerasurfaceview simple surfaceview no customizations whatsoever.
it's little hard see form indentations, looks running camera handler on current thread code snippet.
the last parameter in cameramanager.opencamera tells thread use - if null uses current thread. android documentation:
parameters cameraid string: unique identifier of camera device open
callback cameradevice.statecallback: callback invoked once camera opened
handler handler: handler on callback should invoked, or null use current thread's looper.
if in example on github camera2basic () can see specify separate handler:
manager.opencamera(mcameraid, mstatecallback, mbackgroundhandler);
this started on separate thread:
/** * starts background thread , {@link handler}. */ private void startbackgroundthread() { mbackgroundthread = new handlerthread("camerabackground"); mbackgroundthread.start(); mbackgroundhandler = new handler(mbackgroundthread.getlooper()); }
Comments
Post a Comment