How to programmatically take Photos while recording Video using Camera2 API in Android

前端 未结 6 1790
北恋
北恋 2020-12-24 09:41

I want to capture image while recording video using camera2 API.

Two separate demos are available. 1. To capture image and 2. To record video

I tried to com

6条回答
  •  抹茶落季
    2020-12-24 10:03

    Try this mate:

      public void takeSnapshot(CameraCaptureSession.CaptureCallback captureCallback) {
        try {
            //In case that we are in legacy
            if (null == mCameraActivity || CameraSettings.isLegacyHardwareLevel()) 
                return;
    
            int captureTemplate = CameraDevice.TEMPLATE_VIDEO_SNAPSHOT;
            final CaptureRequest.Builder captureBuilder;
            captureBuilder = YourCameraDevice.createCaptureRequest(captureTemplate);
            captureBuilder.addTarget(YourImageReader.getSurface());
            //To get the right orientation we must to get it in base of the sensor position.
            SensorOrientation = CameraManager.getCameraCharacteristics(mCameraId).get(
                CameraCharacteristics.SENSOR_ORIENTATION);
            captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, SensorOrientation);
    
            mCaptureSession.capture(captureBuilder.build(), captureCallback, mBackgroundHandler);
    
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    

提交回复
热议问题