Android Camera2 拍照(四)——對焦模式

來源:互聯網
上載者:User

標籤:.com   switch   surf   註冊   views   private   date   list   star   

原文:Android Camera2 拍照(四)——對焦模式

本篇將重點介紹使用Camera2 API進行手動對焦的設定,以及在手動對焦與自動對焦模式之間切換。

一、手動對焦響應事件

首先我們要實現點擊preview地區進行手動對焦,這通過對TextureView註冊touch事件實現。

mTextureView.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                int actionMasked = MotionEventCompat.getActionMasked(event);                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;            }        });

triggerFocusArea將實現手動對焦。

二、手動對焦實現

private void triggerFocusArea(float x, float y) {        CameraManager manager = (CameraManager) getActivity().getSystemService(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);        }    }

首先將對焦模式設定為AutoFocusMode.AUTO。AutoFocusMode是定義枚舉類型,其定義如下:

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;            }        }    }

用來實現自訂對焦模式和CameraMetadata中對焦模式的對應。其中AUTO即對應手動對焦

然後通過builder.set(CaptureRequest.CONTROL_AF_MODE,mControlAFMode.switchToCamera2FocusMode());將對焦模式切換為手動對焦。在確定定位地區之後,通過函sendRepeatPreviewRequest恢複到

CameraMetadata.CONTROL_AF_MODE_CONTINUOUS_PICTURE;

源碼參考:https://github.com/gengqifu/361Camera,歡迎fork/star。

Android Camera2 拍照(四)——對焦模式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.