Camera2录制视频

预览启动流程

  1. onResume中判断TextureView是否可用,不可用就给TextureView设置监听,可用时才openCamera
  2. openCamera中先得到相机服务CameraManager,由CameraManager得到相机的属性信息CameraCharacteristics,而CameraCharacteristics包含有配置流信息StreamConfigurationMap等,根据TextureView的尺寸及宽高比从中选择合适的分辨率尺寸来设置预览尺寸和录制/拍照尺寸等信息,需要处理预览方向及拉伸异常。
注意:此时尚未通过CameraManager真正打开相机,但已经可以获取这些信息并进行配置
  1. 通过CameraManager真正打开相机,得到CameraDevice对象实例,在相机设备状态回调成功时开启预览。

(1)将PreviewSize(由TextureView得到)设置为SurfaceTexture(由TextureView得到)的缓冲区尺寸,并得到对应Surface。

本人觉得此操作可以提前到真正打开相机前

(2)CameraDevice创建CameraDevice.TEMPLATE_PREVIEW预览类型的CaptureRequest.Builder对象实例,并添加刚才的Surface对象

mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mPreviewBuilder.addTarget(previewSurface);

(3)CameraDevice创建相机会话,需要刚才的Surface对象,在会话回调方法成功时得到CameraCaptureSession对象实例,
对mPreviewBuilder设置一些对焦模式等参数后,mPreviewSession通过setRepeatingRequest以启动预览

mPreviewSession = session;
setUpCaptureRequestBuilder(mPreviewBuilder);
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, mBackgroundHandler);
Camera2录制视频_第1张图片
预览流程.PNG

开始录制流程

  1. 以MediaRecorder录制为例,新建该对象实例,并设置如下类似参数
        mMediaRecorder = new MediaRecorder();
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        if (mNextVideoAbsolutePath == null || mNextVideoAbsolutePath.isEmpty()) {
            mNextVideoAbsolutePath = getVideoFilePath(getActivity());
            Log.i(TAG, "setUpMediaRecorder: " + mNextVideoAbsolutePath);
        }
        mMediaRecorder.setOutputFile(mNextVideoAbsolutePath);
        mMediaRecorder.setVideoEncodingBitRate(10000000);
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        switch (mSensorOrientation) {
            case SENSOR_ORIENTATION_DEFAULT_DEGREES:
                mMediaRecorder.setOrientationHint(DEFAULT_ORIENTATIONS.get(rotation));
                break;
            case SENSOR_ORIENTATION_INVERSE_DEGREES:
                mMediaRecorder.setOrientationHint(INVERSE_ORIENTATIONS.get(rotation));
                break;
        }
        mMediaRecorder.prepare();
  1. 创建用于录制的CaptureRequest.Builder,设置预览及MediaRecorder的Surface
mPreviewBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
// Set up Surface for the camera preview
mPreviewBuilder.addTarget(previewSurface);
// Set up Surface for the MediaRecorder
Surface recorderSurface = mMediaRecorder.getSurface();
mPreviewBuilder.addTarget(recorderSurface);
  1. CameraDevice再次创建相机会话,需要预览及MediaRecorder的Surface对象,并在回调成功后更新预览,开始录制
mPreviewSession = cameraCaptureSession;
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, mBackgroundHandler);
ps:打开相机,开启预览,录制更新预览是在mBackgroundHandler线程
而mMediaRecorder.start()需要在主线程中
Camera2录制视频_第2张图片
录制流程.PNG

完整代码:
https://github.com/WaterYuanData/Camera2New/blob/camera2_0315/camera2video/src/main/java/com/example/camera2video/CameraFragment.java

你可能感兴趣的:(Camera2录制视频)