flashlight - Android camera2 api openCamera error -
i want make simple flashlite application using camera2 api. using following code:
public class flashlightutilforl { private cameracapturesession msession; private capturerequest.builder mbuilder; private cameradevice mcameradevice; private cameramanager mcameramanager; public flashlightutilforl(context context) { try { mcameramanager = (cameramanager) context.getsystemservice(context.camera_service); //here judge if flash available cameracharacteristics cameracharacteristics = mcameramanager.getcameracharacteristics("0"); boolean flashavailable = cameracharacteristics.get(cameracharacteristics.flash_info_available); if (flashavailable) { mcameramanager.opencamera("0", new mycameradevicestatecallback(), null); } else { //todo: throw exception } //mcameramanager.opencamera("0", new mycameradevicestatecallback(), null); } catch (exception e) { e.printstacktrace(); } } class mycameradevicestatecallback extends cameradevice.statecallback { @override public void onopened(cameradevice camera) { mcameradevice = camera; //get builder try { mbuilder = camera.createcapturerequest(cameradevice.template_manual); //flash on, default on mbuilder.set(capturerequest.control_ae_mode, camerametadata.control_af_mode_auto); mbuilder.set(capturerequest.flash_mode, camerametadata.flash_mode_torch); list<surface> list = new arraylist<surface>(); surfacetexture msurfacetexture = new surfacetexture(1); size size = getsmallestsize(mcameradevice.getid()); msurfacetexture.setdefaultbuffersize(size.getwidth(), size.getheight()); surface msurface = new surface(msurfacetexture); list.add(msurface); mbuilder.addtarget(msurface); camera.createcapturesession(list, new mycameracapturesessionstatecallback(), null); } catch (cameraaccessexception e) { e.printstacktrace(); } } @override public void ondisconnected(cameradevice camera) { } @override public void onerror(cameradevice camera, int error) { } } private size getsmallestsize(string cameraid) throws cameraaccessexception { size[] outputsizes = mcameramanager.getcameracharacteristics(cameraid) .get(cameracharacteristics.scaler_stream_configuration_map) .getoutputsizes(surfacetexture.class); if (outputsizes == null || outputsizes.length == 0) { throw new illegalstateexception( "camera " + cameraid + "doesn't support outputsize."); } size chosen = outputsizes[0]; (size s : outputsizes) { if (chosen.getwidth() >= s.getwidth() && chosen.getheight() >= s.getheight()) { chosen = s; } } return chosen; } /** * session callback */ class mycameracapturesessionstatecallback extends cameracapturesession.statecallback { @override public void onconfigured(cameracapturesession session) { msession = session; try { msession.setrepeatingrequest(mbuilder.build(), null, null); } catch (cameraaccessexception e) { e.printstacktrace(); } } @override public void onconfigurefailed(cameracapturesession session) { } } public void turnonflashlight() { try { mbuilder.set(capturerequest.flash_mode, camerametadata.flash_mode_torch); msession.setrepeatingrequest(mbuilder.build(), null, null); } catch (exception e) { e.printstacktrace(); } } public void turnoffflashlight() { try { mbuilder.set(capturerequest.flash_mode, camerametadata.flash_mode_off); msession.setrepeatingrequest(mbuilder.build(), null, null); } catch (exception e) { e.printstacktrace(); } } private void close() { if (mcameradevice == null || msession == null) { return; } msession.close(); mcameradevice.close(); mcameradevice = null; msession = null; } }
and here how use class main activity:
flashlightutilforl util = new flashlightutilforl(getapplicationcontext()); util.turnonflashlight();
but nothing. found there error while opening camera, there no hint on erros caused by. here log:
this printed 3 times in row:
11-10 15:27:32.881 11801-11801/com.flashlight w/arrayutils: ignoring invalid value manual 11-10 15:27:32.881 11801-11801/com.flashlight w/arrayutils: ignoring invalid value fullscan 11-10 15:27:32.881 11801-11801/com.flashlight w/arrayutils: ignoring invalid value nashville 11-10 15:27:32.882 11801-11801/com.flashlight w/arrayutils: ignoring invalid value hefe 11-10 15:27:32.882 11801-11801/com.flashlight w/arrayutils: ignoring invalid value valencia 11-10 15:27:32.882 11801-11801/com.flashlight w/arrayutils: ignoring invalid value xproll 11-10 15:27:32.882 11801-11801/com.flashlight w/arrayutils: ignoring invalid value lofi 11-10 15:27:32.882 11801-11801/com.flashlight w/arrayutils: ignoring invalid value sierra 11-10 15:27:32.882 11801-11801/com.flashlight w/arrayutils: ignoring invalid value walden 11-10 15:27:32.882 11801-11801/com.flashlight w/arrayutils: ignoring invalid value normal
and once:
11-10 15:27:32.909 11801-11801/com.flashlight i/cameramanager: using legacy camera hal. 11-10 15:27:32.911 11801-12695/com.flashlight w/camera: error occurred while connecting camera: 0
i not find related particular error in camera2. cause problem? device redmi note 2 5.0.2, camera has flashlight, , have added camera permission in manifest.
i suspect issue cameradevice.template_manual
. not devices support this. not think works when device using legacy support. try switching cameradevice.template_preview
.
also, not forget release sufacetexture
when don it.
Comments
Post a Comment