1. 程式人生 > >Android Camera2 拍照(三)——切換攝像頭,延時拍攝和閃光模式

Android Camera2 拍照(三)——切換攝像頭,延時拍攝和閃光模式

openca any The visible surface else 提示 再次 .cn

原文:Android Camera2 拍照(三)——切換攝像頭,延時拍攝和閃光模式

一、切換攝像頭

在前後攝像頭之間切換,首先需要關閉之前打開的攝像頭,關閉preview,之後重新打開新的攝像頭,重新打開preview。

public void switchCamera() {
        if (mCameraId.equals(CAMERA_FRONT)) {
            mCameraId = CAMERA_BACK;
            closeCamera();
            reopenCamera();

        } else if (mCameraId.equals(CAMERA_BACK)) {
            mCameraId = CAMERA_FRONT;
            closeCamera();
            reopenCamera();
        }
    }

關閉攝像頭:

/**
     * Closes the current {@link CameraDevice}.
     */
    private void closeCamera() {
        try {
            mCameraOpenCloseLock.acquire();
            synchronized (mCameraStateLock) {

                // Reset state and clean up resources used by the camera.
                // Note: After calling this, the ImageReaders will be closed after any background
                // tasks saving Images from these readers have been completed.
                mPendingUserCaptures = 0;
                mState = STATE_CLOSED;
                if (null != mCaptureSession) {
                    mCaptureSession.close();
                    mCaptureSession = null;
                }
                if (null != mCameraDevice) {
                    mCameraDevice.close();
                    mCameraDevice = null;
                }
                if (null != mJpegImageReader) {
                    mJpegImageReader.close();
                    mJpegImageReader = null;
                }
                if (null != mRawImageReader) {
                    mRawImageReader.close();
                    mRawImageReader = null;
                }
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while trying to lock camera closing.", e);
        } finally {
            mCameraOpenCloseLock.release();
        }
    }

打開新的攝像頭:

public void reopenCamera() {
        if (mTextureView.isAvailable()) {
            openCamera();
        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }

如果TextureView是available的,那麽直接調用openCamera()打開camera(openCamera()的代碼參考之前的博客);如果|TextureView是unavailable的,那麽調用TextureView的setSurfaceTextureListener設置SurfaceTextureListener(參考上一篇博客)。

二、延時拍攝

要實現延時拍攝,核心是實現一個定時器。本例延時分為三種模式:3s延時,10s按時,無延時。可以在三種模式之中自有切換。

private void switchDelayState() {
        switch (mDelayState) {
            case 0:
                mTimer.setImageResource(R.mipmap.ic_3s);
                mDelayTime = 3 * 1000;
                mDelayState = 1;
                break;
            case 1:
                mTimer.setImageResource(R.mipmap.ic_10s);
                mDelayTime = 10 * 1000;
                mDelayState = 2;
                break;
            case 2:
                mTimer.setImageResource(R.mipmap.timer);
                mDelayTime = 0;
                mDelayState = 0;
                break;
            default:
                break;
        }
    }

變量mDelayTime和mDelaySate的定義:

/*
     * Delay state, 0 represents no delay, 1 represents 3s delay, while 2 represents 10s delay
     */
    private short mDelayState = 0; // Timer
    private short mDelayTime;

當拍攝按鈕被點擊時:

@Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.capture: {
                if (mDelayState == 0) {
                    takePicture();
                } else {
                    new CountDownTimer(mDelayTime, TIME_INTERVAL) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            mTimeText.setVisibility(View.VISIBLE);
                            mTimeText.setText("" + millisUntilFinished / TIME_INTERVAL);
                        }

                        @Override
                        public void onFinish() {
                            mTimeText.setVisibility(View.GONE);
                            takePicture();
                        }
                    }.start();
                }
                break;
            }

但有3s或10s延時的時候,利用CountDownTimer實現倒計時並每隔一秒更新剩余時間提示,到時候執行拍照功能。

三、切換閃光模式

支持自動,強制,禁止閃光。與閃光模式有關的屬性是CaptureRequest.CONTROL_AE_MODE和CaptureRequest.FLASH_MODE。 如果要使用FLASH_MODE屬相,閃光燈必須可用,並且,CONTROL_AE_MODE必須設置為on或off,否則,自動曝光屬性的ON_AUTO_FLASH,ON_ALWAYS_FLASH, ON_AUTO_FLASH_REDEYE將會覆蓋FLASH_MODE的設置。

private void switchFlashMode() {
        switch (mFlashMode) {
            case 0:
                mFlashMode = 1;
                mFlashBtn.setImageResource(R.mipmap.flash_auto);
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
                try {
                    mCaptureSession.setRepeatingRequest(
                            mPreviewRequestBuilder.build(),
                            mPreCaptureCallback, mBackgroundHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                    return;
                }

                break;
            case 1:
                mFlashMode = 2;
                mFlashBtn.setImageResource(R.mipmap.flash_on);
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
                try {
                    mCaptureSession.setRepeatingRequest(
                            mPreviewRequestBuilder.build(),
                            mPreCaptureCallback, mBackgroundHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                    return;
                }
                break;
            case 2:
                mFlashMode = 0;
                mFlashBtn.setImageResource(R.mipmap.flash_off);
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
                mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
                try {
                    mCaptureSession.setRepeatingRequest(
                            mPreviewRequestBuilder.build(),
                            mPreCaptureCallback, mBackgroundHandler);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                    return;
                }
                break;
        }
    }

在takePicture()之前,需要再次設置:

private void setFlashMode() {
        switch (mFlashMode) {
            case 0:
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
                mPreviewRequestBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
                break;
            case 1:
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
                break;
            case 2:
                mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
                break;
        }
    }
代碼參考:https://github.com/gengqifu/361Camera,歡迎fork/star。

Android Camera2 拍照(三)——切換攝像頭,延時拍攝和閃光模式