Camera Session Management (C/C++)
Before using the camera application for preview, photo capture, video recording, and metadata management, you must create a camera session.
You can implement the following functions in the session:
-
Configure the camera input and output streams. This is mandatory for photo capture.
Configuring an input stream is to add a device input, which means that the user selects a camera for photo capture. Configuring an output stream is to select a data output mode. For example, to implement photo capture, you must configure both the preview stream and photo stream as the output stream. The data of the preview stream is displayed on the XComponent, and that of the photo stream is saved to the Gallery application through the ImageReceiver API.
-
Perform more operations on the camera device. For example, add the flash and adjust the focal length. For details about the supported configurations and APIs, see Camera API Reference.
-
Control session switching. The application can switch the camera mode by removing and adding output streams. For example, to switch from photo capture to video recording, the application must remove the photo output stream and add the video output stream.
After the session configuration is complete, the application must commit the configuration and start the session before using the camera functionalities.
How to Develop
-
Import the NDK.
#include "hilog/log.h" #include "ohcamera/camera.h" #include "ohcamera/camera_input.h" #include "ohcamera/capture_session.h" #include "ohcamera/photo_output.h" #include "ohcamera/preview_output.h" #include "ohcamera/video_output.h" #include "ohcamera/camera_manager.h" -
Link the dynamic library in the CMake script.
target_link_libraries(entry PUBLIC libace_napi.z.so libohcamera.so libhilog_ndk.z.so ) -
Call OH_CameraManager_CreateCaptureSession() to create a session.
Camera_CaptureSession* CreateCaptureSession(Camera_Manager* cameraManager) { Camera_CaptureSession* captureSession = nullptr; if (cameraManager == nullptr) { OH_LOG_ERROR(LOG_APP, "cameraManager is nullptr."); return captureSession; } Camera_ErrorCode ret = OH_CameraManager_CreateCaptureSession(cameraManager, &captureSession); if (captureSession == nullptr || ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateCaptureSession failed."); } return captureSession; } -
Call OH_CaptureSession_SetSessionMode() to set the session mode.
Camera_ErrorCode SetSessionMode(Camera_CaptureSession* captureSession) { Camera_ErrorCode ret = OH_CaptureSession_SetSessionMode(captureSession, NORMAL_VIDEO); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetSessionMode failed."); } return ret; } -
Call OH_CaptureSession_BeginConfig() to configure the session.
Camera_ErrorCode BeginConfig(Camera_CaptureSession* captureSession) { Camera_ErrorCode ret = OH_CaptureSession_BeginConfig(captureSession); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed."); } return ret; } -
Configure the session. You can call OH_CaptureSession_AddInput() to add an input stream to the session, and call OH_CaptureSession_AddPreviewOutput() and OH_CaptureSession_AddPhotoOutput() to add output streams to the session. The code snippet below uses adding the preview stream previewOutput and photo stream photoOutput as an example to implement the photo capture and preview mode.
After the configuration, call OH_CaptureSession_CommitConfig() and OH_CaptureSession_Start() to commit the configuration and start the session.
Camera_ErrorCode StartSession(Camera_CaptureSession* captureSession, Camera_Input* cameraInput, Camera_PreviewOutput* previewOutput, Camera_PhotoOutput* photoOutput) { // Add the camera input stream to the session. Camera_ErrorCode ret = OH_CaptureSession_AddInput(captureSession, cameraInput); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed."); return ret; } // Add the preview output stream to the session. ret = OH_CaptureSession_AddPreviewOutput(captureSession, previewOutput); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPreviewOutput failed."); return ret; } // Add the photo output stream to the session. ret = OH_CaptureSession_AddPhotoOutput(captureSession, photoOutput); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddPhotoOutput failed."); return ret; } // Commit the session configuration. ret = OH_CaptureSession_CommitConfig(captureSession); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed."); return ret; } // Start the session. ret = OH_CaptureSession_Start(captureSession); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed."); } return ret; } -
Control the session. You can call OH_CaptureSession_Stop() to stop the session, and call OH_CaptureSession_RemovePhotoOutput() and OH_CaptureSession_AddVideoOutput() to switch to another session. The code snippet below uses removing the photo stream photoOutput and adding the video stream videoOutput as an example to complete the switching from photo capture to video recording.
Camera_ErrorCode ReloadSession(Camera_CaptureSession* captureSession, Camera_PhotoOutput* photoOutput, Camera_VideoOutput* videoOutput) { Camera_ErrorCode ret = OH_CaptureSession_Stop(captureSession); if (ret == CAMERA_OK) { OH_LOG_INFO(LOG_APP, "OH_CaptureSession_Stop success "); } else { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed. %d ", ret); } ret = OH_CaptureSession_BeginConfig(captureSession); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed."); return ret; } // Remove the photo output stream from the session. ret = OH_CaptureSession_RemovePhotoOutput(captureSession, photoOutput); if (ret == CAMERA_OK) { OH_LOG_INFO(LOG_APP, "OH_CaptureSession_RemovePhotoOutput success "); } else { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RemovePhotoOutput failed. %d ", ret); } // Release the photoOutput instance. ret = OH_PhotoOutput_Release(photoOutput); if (ret == CAMERA_OK) { OH_LOG_INFO(LOG_APP, "OH_PhotoOutput_Release success "); } else { OH_LOG_ERROR(LOG_APP, "OH_PhotoOutput_Release failed. %d ", ret); } // Add the video output stream to the session. ret = OH_CaptureSession_AddVideoOutput(captureSession, videoOutput); if (ret == CAMERA_OK) { OH_LOG_INFO(LOG_APP, "OH_CaptureSession_AddVideoOutput success "); } else { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddVideoOutput failed. %d ", ret); return ret; } // Commit the session configuration. ret = OH_CaptureSession_CommitConfig(captureSession); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed."); return ret; } // Start the session. ret = OH_CaptureSession_Start(captureSession); if (ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed."); } return ret; }