Camera-CreateCaptureSession

        Camera CaptureSession is a operation to configure the camera device's internal pipelines and allocating memory buffers to sending images to the desired targets. 

        A configured capture session for a cameraDevice is used for capturing images from the camera or reprocessing images captured from the camera in the same session previously and providing a set of target output surfaces for reprocessable capture session, and once created, the session is active until a new session is created by the camera device, or the camera device is closed.

Part One: createCaptureSession by List<Surface>

        camera createCaptureSession by List<Surface> will produce potentially risk to delaying camera startup. For example, if the preview Surface is obtained from SurfaceView, the SurfaceView will only be ready after the UI layout is done, so this opreation will take some time for waiting preview surface to be ready.

        Below is the struct of the createCaptureSession by List<Surface> and releated API.

         

framework/base/core/java/android/hardware/camera2/impl/CameraDeviceImpl.java

 

Part Two: createCaptureSession by List<OutputConfiguration> 

        To speed up camera startup time, the users can configure the CameraCaptureSession with the eventual preview size via OutputConfiguration and defer the preview output comfiguration until the Surface is ready. After the CameraCaptureSession is created successfully with this deferred output and other normal outputs, the application can start submitting requests as long as the do not include deferred out Surfaces. Once a deferred Surface is ready, the application can add the Surface to the Surface to the deferred output configuration with the addSurface method, and then update the deferred output configuration via this method, before it can submit caoture requests with this output target.

         finalizeOutputConfigurations- This function can also be called in case where multiple surfaces share the same OutputConfiguration, and one of the surfaces becomes available after the CameraCaptureSession is created. In that case, the user must first create the outputConfiguration with the available Surface before creating the CameraCaptureSession. After the CameraCaptureSession is created, and once the extra Surface becomes available, the users must then call add Surface before finalizing the configuration with this method, and attention again, if the provided OutputConfigurations are unchanged from session creation, this function call has no effect. This function must only be called once fir a particular output configuration.

        Below is the structure of the createCaptureSession by List<OutputConfiguration> and releated API.

 

        Attention: Use this method to quickly create cameraCaptureSession may be produce some errors when outputconfiguration that is different from the surface has been changed as different size and formats.

        Below has some error information to check and stay cautiously.

 

Part Three: createCaptureSession by createCaptureSessionByOutputConfigurations

        This method create a new camera capture session by providing the target output set of Surfaces and its correspending surface configuration to the camera device.

        Below is the releated API and function invoke.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Camera2 API 创建多个 CaptureSession 的示例代码: ```java public class CameraFragment extends Fragment { private CameraDevice mCameraDevice; private CameraCaptureSession mPreviewSession; private CameraCaptureSession mImageSession; private Size mPreviewSize; private Size mImageSize; private ImageReader mImageReader; private final CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() { @Override public void onOpened(@NonNull CameraDevice cameraDevice) { mCameraDevice = cameraDevice; createPreviewSession(); createImageSession(); } @Override public void onDisconnected(@NonNull CameraDevice cameraDevice) { cameraDevice.close(); mCameraDevice = null; } @Override public void onError(@NonNull CameraDevice cameraDevice, int error) { cameraDevice.close(); mCameraDevice = null; Activity activity = getActivity(); if (null != activity) { activity.finish(); } } }; private final CameraCaptureSession.StateCallback mPreviewSessionCallback = new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { mPreviewSession = cameraCaptureSession; updatePreview(); } @Override public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { Activity activity = getActivity(); if (null != activity) { Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show(); } } }; private final CameraCaptureSession.StateCallback mImageSessionCallback = new CameraCaptureSession.StateCallback() { @Override public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) { mImageSession = cameraCaptureSession; } @Override public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) { Activity activity = getActivity(); if (null != activity) { Toast.makeText(activity, "Failed", Toast.LENGTH_SHORT).show(); } } }; private final ImageReader.OnImageAvailableListener mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() { @Override public void onImageAvailable(ImageReader reader) { Image image = reader.acquireLatestImage(); // Process the captured image image.close(); } }; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_camera, container, false); return view; } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); // Initialize the camera openCamera(); } @Override public void onResume() { super.onResume(); // Start the preview session if (null != mCameraDevice) { createPreviewSession(); } } @Override public void onPause() { closeCamera(); super.onPause(); } private void openCamera() { Activity activity = getActivity(); if (null == activity || activity.isFinishing()) { return; } CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE); try { String cameraId = manager.getCameraIdList()[0]; CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId); StreamConfigurationMap map = characteristics.get( CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0]; mImageSize = map.getOutputSizes(ImageFormat.JPEG)[0]; mImageReader = ImageReader.newInstance(mImageSize.getWidth(), mImageSize.getHeight(), ImageFormat.JPEG, /*maxImages*/2); mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, null); manager.openCamera(cameraId, mStateCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } } private void closeCamera() { if (null != mPreviewSession) { mPreviewSession.close(); mPreviewSession = null; } if (null != mImageSession) { mImageSession.close(); mImageSession = null; } if (null != mCameraDevice) { mCameraDevice.close(); mCameraDevice = null; } if (null != mImageReader) { mImageReader.close(); mImageReader = null; } } private void createPreviewSession() { try { SurfaceTexture texture = getSurfaceTexture(); texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); Surface surface = new Surface(texture); CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); builder.addTarget(surface); mCameraDevice.createCaptureSession(Arrays.asList(surface), mPreviewSessionCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } } private void createImageSession() { try { Surface surface = mImageReader.getSurface(); CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); builder.addTarget(surface); mCameraDevice.createCaptureSession(Arrays.asList(surface), mImageSessionCallback, null); } catch (CameraAccessException e) { e.printStackTrace(); } } private void updatePreview() { if (null == mCameraDevice) { return; } try { CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); SurfaceTexture texture = getSurfaceTexture(); texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight()); Surface surface = new Surface(texture); builder.addTarget(surface); mPreviewSession.setRepeatingRequest(builder.build(), null, null); } catch (CameraAccessException e) { e.printStackTrace(); } } private SurfaceTexture getSurfaceTexture() { Activity activity = getActivity(); if (null == activity) { return null; } TextureView textureView = activity.findViewById(R.id.texture_view); return textureView.getSurfaceTexture(); } } ``` 在此示例中,我们创建了两个 CaptureSession:一个用于预览,一个用于捕获图像。我们使用 ImageReader 来捕获 JPEG 图像,然后在 mOnImageAvailableListener 中处理捕获的图像。在 openCamera() 方法中,我们初始化了 ImageReader 并调用了 manager.openCamera() 来打开相机。在 mStateCallback 的 onOpened() 方法中,我们创建了两个 CaptureSession:一个用于预览,一个用于捕获图像。在 createPreviewSession() 方法中,我们首先获取 SurfaceTexture,然后创建一个 Surface 并将其添加到 CaptureRequest.Builder 中。然后,我们调用 mCameraDevice.createCaptureSession() 来创建预览 CaptureSession。在 createImageSession() 方法中,我们创建了一个与 ImageReader 相关联的 Surface,并将其添加到 CaptureRequest.Builder 中。然后,我们调用 mCameraDevice.createCaptureSession() 来创建捕获图像的 CaptureSession。在 updatePreview() 方法中,我们首先获取 SurfaceTexture,然后创建一个 Surface 并将其添加到 CaptureRequest.Builder 中。然后,我们调用 mPreviewSession.setRepeatingRequest() 来更新预览。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值