Android Camera2 photo (quad)--focus mode

Source: Internet
Author: User

Original: Android Camera2 photo (quad)--focus mode

This article focuses on setting up manual focus using the CAMERA2 API and switching between manual focus and autofocus mode.

One, manual focus response Event

First we will implement manual focus by clicking on the preview area, which is achieved by registering the touch event with the Textureview.

mtextureview.setontouchlistener (New View.ontouchlistener () {@Override public boolean OnTouch (View V, motionevent event) {int actionmasked = motioneventcompat.getactionmasked (E                VENT);                int Fingerx, fingery;                int length = (int) (Getresources (). Getdisplaymetrics (). density * 80); Switch (actionmasked) {case MotionEvent.ACTION_DOWN:fingerX = (int) event.getx                        ();                        Fingery = (int) event.gety ();                        LOGUTIL.D ("ontouch:x->" + Fingerx + ",y->" + fingery);                        Mivfocus.setx (FINGERX-LENGTH/2);                        Mivfocus.sety (FINGERY-LENGTH/2);                        Mivfocus.setvisibility (view.visible);                        Triggerfocusarea (Fingerx, fingery);                Break            } return false; }        });

The Triggerfocusarea will achieve manual focus.

Second, manual focus implementation

private void Triggerfocusarea (float x, float y) {Cameramanager manager = (Cameramanager) getactivity (). Getsystemse        Rvice (Context.camera_service);            try {cameracharacteristics characteristics = manager.getcameracharacteristics (mcameraid);            Integer sensororientation = Characteristics.get (cameracharacteristics.sensor_orientation); Sensororientation = Sensororientation = = null?            0:sensororientation;            Rect cropregion = autofocushelper.cropregionforzoom (characteristics, 1f);            Maeregions = Autofocushelper.aeregionsfornormalizedcoord (x, Y, cropregion, sensororientation);            Mafregions = Autofocushelper.afregionsfornormalizedcoord (x, Y, cropregion, sensororientation);            Step 1:request Single Frame Control_af_trigger_start.            Capturerequest.builder Builder;            Builder = mcameradevice.createcapturerequest (Cameradevice.template_preview); Builder. AddTarget (Mpreviewsurface);            Builder.set (Capturerequest.control_mode, Camerametadata.control_mode_auto);            Mcontrolafmode = Autofocusmode.auto;            Builder.set (Capturerequest.control_af_mode, Mcontrolafmode.switchtocamera2focusmode ());            Builder.set (Capturerequest.control_af_trigger, Capturerequest.control_af_trigger_start);            Mcapturesession.capture (Builder.build (), Mprecapturecallback, Mbackgroundhandler);            Step 2:call repeatingpreview to update mcontrolafmode.            Sendrepeatpreviewrequest ();        Resumecontinuousafafterdelay (DELAY_TIME_RESUME_CONTINUOUS_AF);        } catch (Cameraaccessexception ex) {LOG.E (TAG, "Could not execute preview request.", ex); }    }

First set the focus mode to Autofocusmode. AUTO. Autofocusmode is the definition of an enumeration type, which is defined as follows:

Enum Autofocusmode {        /**         * System is continuously focusing.         */        Continuous_picture,        /**         * System is running a triggered scan.         */        AUTO;        int Switchtocamera2focusmode () {            switch (this) {case                continuous_picture:                    return Camerametadata.control_af_mode_continuous_picture;                Case AUTO:                    return camerametadata.control_af_mode_auto;                Default:                    return camerametadata.control_af_mode_continuous_picture;}}}    

Used to implement the corresponding focus mode in the custom focus mode and Camerametadata. Where auto is the corresponding manual focus

Then through Builder.set (capturerequest. Control_af_mode,mcontrolafmode. Switchtocamera2focusmode ()); Switch focus mode to manual focus. After determining the location area, the letter Sendrepeatpreviewrequest is restored to

Camerametadata.control_af_mode_continuous_picture;

Source code reference: Https://github.com/gengqifu/361Camera, Welcome to Fork/star.

Android Camera2 photo (quad)--focus mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.