Android網路攝影機自動對焦與觸摸對焦實現

來源:互聯網
上載者:User

標籤:

1、自動對焦

  在預覽前後設定的param裡setFocusMode();為Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO或者Camera.Parameters.FOCUS_MODE_AUTO,建議採用後者。注意調用此方法後,再次調用Camera.autoFocus()會導致對焦失敗

  參考此文 http://blog.csdn.net/yanzi1225627/article/details/8577682

 

2、觸摸對焦

  觸摸對焦在設定焦點前需要先取消先前設定的焦點,

  參考文章 http://stackoverflow.com/a/19272166

    /**     * On each tap event we will calculate focus area and metering area.     * <p/>     * Metering area is 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_ != null) {            //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 are 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<>();                        metering.add(new Camera.Area(focusRect, 1000));                        parameters.setMeteringAreas(metering);                    }                }                try {                    camera_.setParameters(parameters);                    camera_.autoFocus(new Camera.AutoFocusCallback() {                        @Override                        public void onAutoFocus(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 matrix configured in     * {@link SurfaceHolder.Callback#surfaceChanged(android.view.SurfaceHolder, int, int, int)}     */    private Rect calculateTapArea(float x, float y, float 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網路攝影機自動對焦與觸摸對焦實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.