Android camera autofocus with touch focus implementation

Source: Internet
Author: User
Tags gety

1. Auto Focus

In the Param set before and after the preview Setfocusmode (); for Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO or Camera.Parameters.FOCUS_MODE_AUTO , it is recommended that the latter be used. Note When this method is called, calling Camera.autofocus () again causes the focus to fail

Refer to this article http://blog.csdn.net/yanzi1225627/article/details/8577682

2. Touch Focus

Touch focus needs to cancel the previously set focus before setting the focus.

Reference article http://stackoverflow.com/a/19272166

    /** * On each tap event we'll calculate focus area and metering area.     * <p/> * Metering area are slightly larger as it should contain more info for exposure calculation. * As it is very easy to over/under expose */public void Focusontouch (Motionevent event) {if (Camera_! = Nu            LL) {//cancel previous Actions camera_.cancelautofocus ();            Rect focusrect = Calculatetaparea (Event.getx (), Event.gety (), 1f);            Rect meteringrect = Calculatetaparea (Event.getx (), Event.gety (), 1.5f);            Camera.parameters Parameters = null;            try {parameters = Camera_.getparameters ();            } catch (Exception e) {e.printstacktrace ();            }//Check if parameters is set (handle Runtimeexception:getparameters failed (empty parameters))                if (Parameters! = null) {Parameters.setfocusmode (Camera.Parameters.FOCUS_MODE_AUTO); IF (Build.VERSION.SDK_INT >= build.version_codes.                    Ice_cream_sandwich) {list<camera.area> focus = new arraylist<> ();                    Focus.add (New Camera.area (Focusrect, 1000));                    Parameters.setfocusareas (focus); if (Parameters.getmaxnummeteringareas () > 0) {list<camera.area> metering = new arraylist&                        Lt;> ();                        Metering.add (New Camera.area (Focusrect, 1000));                    Parameters.setmeteringareas (metering);                    }} try {camera_.setparameters (parameters); Camera_.autofocus (New Camera.autofocuscallback () {@Override public void OnA                Utofocus (Boolean success, Camera camera) {}});                } catch (Exception e) {e.printstacktrace ();          }  }}} private int focusareasize = 300;     /** * Convert Touch position x:y to {@link Camera.area} position-1000:-1000 to 1000:1000. * <p/> * Rotate, scale and translate touch rectangle using the matrix configured in * {@link surfaceholder.callb Ack#surfacechanged (android.view.SurfaceHolder, int, int, int)} */Private Rect Calculatetaparea (float x, float y, F        Loat coefficient) {int areasize = float.valueof (focusareasize * coefficient). Intvalue ();        int left = clamp ((int) X-AREASIZE/2, 0, Surfaceview_.getwidth ()-areasize);        int top = clamp ((int) Y-AREASIZE/2, 0, Surfaceview_.getheight ()-areasize);        RECTF RECTF = new RECTF (left, top, left + areasize, top + areasize);        (New Matrix ()). Maprect (RECTF);    return new Rect (Math.Round (Rectf.left), Math.Round (Rectf.top), Math.Round (Rectf.right), Math.Round (Rectf.bottom)); } private int clamp (int x, int min, int max) {if (x > Max) {reTurn Max;        } if (x < min) {return min;    } return x; }

  

Android camera autofocus with touch focus implementation

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.