How to set android camera2 preview and capture size? -


i using surfaceview show preview capture. want use width=1080,height=1920 preview. can set size of preview?

i googled answer, camera version one. using android.hardware.camera2.

private void takepreview() {     try {         final capturerequest.builder previewrequestbuilder = mcameradevice.createcapturerequest(cameradevice.template_preview);         previewrequestbuilder.addtarget(msurfaceholder.getsurface());         mcameradevice.createcapturesession(arrays.aslist(msurfaceholder.getsurface(), mimagereader.getsurface()), new cameracapturesession.statecallback() // ③         {             @override             public void onconfigured(cameracapturesession cameracapturesession) {                 if (null == mcameradevice) return;                 mcameracapturesession = cameracapturesession;                 try {                     previewrequestbuilder.set(capturerequest.control_af_mode, capturerequest.control_af_mode_continuous_picture);                     previewrequestbuilder.set(capturerequest.control_ae_mode, capturerequest.control_ae_mode_on_always_flash);                     previewrequestbuilder.set(capturerequest.jpeg_thumbnail_size, new size(1080,1920));                      capturerequest previewrequest = previewrequestbuilder.build();                     mcameracapturesession.setrepeatingrequest(previewrequest, null, childhandler);                 } catch (cameraaccessexception e) {                     log.e("takepreview","onconfigured(cameracapturesession cameracapturesession)",e);                 }             }             @override             public void onconfigurefailed(cameracapturesession cameracapturesession) {                 log.e("takepreview","onconfigurefailed");             }         }, childhandler);     } catch (cameraaccessexception e) {         log.e("takepreview","cameraaccessexception");     } } 

take @ camera2basic example google provides on github: https://github.com/googlesamples/android-camera2basic

there method in main fragment chooses optional preview size given device. may better approach if want make app more flexible, rather hardcoding size, if still rather stick set sizes can see how use results.

the summary set size of the, in case, textureview whatever size preview want.

the method name 'chooseoptimalsize' , includes comment/explanation:

/**      * given {@code choices} of {@code size}s supported camera, choose smallest 1      * @ least large respective texture view size, , @ large      * respective max size, , aspect ratio matches specified value. if such size      * doesn't exist, choose largest 1 @ large respective max size,      * , aspect ratio matches specified value.      *      * @param choices           list of sizes camera supports intended output      *                          class      * @param textureviewwidth  width of texture view relative sensor coordinate      * @param textureviewheight height of texture view relative sensor coordinate      * @param maxwidth          maximum width can chosen      * @param maxheight         maximum height can chosen      * @param aspectratio       aspect ratio      * @return optimal {@code size}, or arbitrary 1 if none big enough      */ 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -