Starting from API version 18, multi-camera concurrent mode is supported, which allows an app to simultaneously open the front and rear cameras for preview and video recording. (Simultaneous photo capture with both front and rear cameras is coming soon.)
Operating both cameras at the same time imposes significant restrictions on available features. The current implementation supports seven core functions, as listed below. When using multi-camera concurrent mode, avoid accessing or enabling any capabilities beyond them.
Read Camera for the API reference.
Import the NDK, which provides camera-related properties and methods.
#include <cstdint> #include <unistd.h> #include <string> #include <thread> #include <cstdio> #include <fcntl.h> #include <map> #include <vector> #include <native_buffer/native_buffer.h> #include "iostream" #include "mutex" #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 "napi/native_api.h" #include "ohcamera/camera_manager.h"
Link the dynamic libraries in the CMake script.
target_link_libraries(entry PUBLIC libohcamera.so libace_napi.z.so libnative_buffer.so libhilog_ndk.z.so librawfile.z.so)
Obtain the corresponding front camera and rear camera through the OH_CameraManager_GetCameraDevice method. If the API returns CAMERA_SERVICE_FATAL_ERROR, it indicates, based on the configuration in the example, that the current device does not support the default camera type at the specified position (front or rear), and multi-camera concurrent mode cannot be implemented.
void NDKCamera::GetSupportedCamerasForConcurrent(Camera_Manager *cameraManager)
{
Camera_Device *cameras = nullptr;
uint32_t size = 0;
Camera_ErrorCode ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
int32_t concurrentDeviceSize = 2;
if (ret != CAMERA_OK || cameras == nullptr || size < concurrentDeviceSize) {
OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
return;
}
// Obtain the rear camera.
ret = OH_CameraManager_GetCameraDevice(cameraManager, Camera_Position::CAMERA_POSITION_BACK,
Camera_Type::CAMERA_TYPE_DEFAULT, &cameras[0]);
// Obtain the front camera.
ret = OH_CameraManager_GetCameraDevice(cameraManager, Camera_Position::CAMERA_POSITION_FRONT,
Camera_Type::CAMERA_TYPE_DEFAULT, &cameras[1]);
// Release the camera list and set the pointer to null after use to prevent memory leaks.
OH_CameraManager_DeleteSupportedCameras(cameraManager, cameras, size);
cameras = nullptr;
} Obtain the corresponding concurrent capability set. Use the OH_CameraManager_GetCameraConcurrentInfos method to obtain the Camera_ConcurrentInfo camera concurrent capability set object array. The array contains the camera modes and camera output capabilities supported by the simultaneously opened front and rear cameras in the specified multi-camera concurrent mode. In the concurrent camera opening scenario, the configured mode and output capabilities must fall within the scope of the concurrent capability set.
void NDKCamera::GetSupportedOutputCapabilityForConcurrent(Camera_Manager *cameraManager, Camera_Device *cameras)
{
Camera_ConcurrentInfo* cameraConcurrentInfo = nullptr;
uint32_t infoSize = 0;
Camera_ErrorCode ret = OH_CameraManager_GetCameraConcurrentInfos(cameraManager, cameras, 2,
&cameraConcurrentInfo, &infoSize);
if (ret != CAMERA_OK || cameraConcurrentInfo == nullptr) {
OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetCameraConcurrentInfos failed.");
return;
}
if (cameraConcurrentInfo->outputCapabilities == nullptr
|| cameraConcurrentInfo->outputCapabilities->previewProfiles == nullptr) {
OH_LOG_ERROR(LOG_APP, "outputCapabilities or previewProfiles is nullptr.");
}
} Create a PreviewOutput object based on the concurrent capability set.
Camera_ErrorCode NDKCamera::CreatePreviewOutputForConcurrent(Camera_Manager *cameraManager,
Camera_ConcurrentInfo* cameraConcurrentInfo, Camera_PreviewOutput** previewOutput, char* previewSurfaceId)
{
if (cameraManager == nullptr || cameraConcurrentInfo == nullptr || previewOutput == nullptr ||
cameraConcurrentInfo->outputCapabilities == nullptr ||
cameraConcurrentInfo->outputCapabilities->previewProfiles == nullptr) {
OH_LOG_ERROR(LOG_APP, "invalid parameter.");
return CAMERA_INVALID_ARGUMENT;
}
Camera_Profile *profile = cameraConcurrentInfo->outputCapabilities->previewProfiles[0];
if (profile == nullptr) {
OH_LOG_ERROR(LOG_APP, "previewProfiles[0] is nullptr.");
return CAMERA_INVALID_ARGUMENT;
}
Camera_ErrorCode ret = OH_CameraManager_CreatePreviewOutput(cameraManager, profile,
previewSurfaceId, previewOutput);
if (*previewOutput == nullptr || ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed.");
return ret;
}
return ret;
} Open the camera. Call OH_CameraInput_OpenConcurrentCameras to open the specified camera in multi-camera concurrent mode. Before using this API, check whether the camera supports concurrent capabilities and call OH_CameraManager_GetCameraConcurrentInfos to obtain the concurrent capability set in the multi-camera concurrent mode. Do not use OH_CameraInput_OpenConcurrentCameras without querying the concurrent capability set, as this will result in camera opening failure.
void NDKCamera::CameraInputOpenForConcurrent(Camera_Manager *cameraManager, Camera_Device *cameras)
{
Camera_Input *cameraInput = nullptr;
Camera_ErrorCode ret = OH_CameraManager_CreateCameraInput(cameraManager, &cameras[0], &cameraInput);
if (cameraInput == nullptr || ret != CAMERA_OK) {
return;
}
// The current version supports opening the camera only in CAMERA_CONCURRENT_TYPE_LIMITED_CAPABILITY mode.
ret = OH_CameraInput_OpenConcurrentCameras(cameraInput,
Camera_ConcurrentType::CAMERA_CONCURRENT_TYPE_LIMITED_CAPABILITY);
if (ret != CAMERA_OK) {
return;
}
} Implement the session flow. Configure the corresponding camera input stream Camera_Input and the required camera output streams. This example uses the preview output stream Camera_PreviewOutput.
void NDKCamera::SessionFlowForConcurrentFn(Camera_CaptureSession *captureSession,
Camera_Input *cameraInput, Camera_PreviewOutput *previewOutput)
{
// Start configuration for the session.
Camera_ErrorCode ret = OH_CaptureSession_BeginConfig(captureSession);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_BeginConfig failed.");
return;
}
// Add the camera input stream to the session.
ret = OH_CaptureSession_AddInput(captureSession, cameraInput);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_AddInput failed.");
return;
}
// 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;
}
// Commit the configuration.
ret = OH_CaptureSession_CommitConfig(captureSession);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_CommitConfig failed.");
return;
}
// Start the session.
ret = OH_CaptureSession_Start(captureSession);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
}
} The following provides examples of configurable capabilities for front and rear cameras in multi-camera concurrent mode. (Currently, only the seven core functions listed at the beginning of this document are supported.)
Camera_ErrorCode NDKCamera::HasFlashFn(uint32_t mode)
{
Camera_FlashMode flashMode = static_cast<Camera_FlashMode>(mode);
// Check whether the camera device has flash.
bool hasFlash = false;
Camera_ErrorCode ret = OH_CaptureSession_HasFlash(captureSession_, &hasFlash);
if (captureSession_ == nullptr || ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_HasFlash failed.");
}
if (hasFlash) {
OH_LOG_INFO(LOG_APP, "hasFlash success-----");
} else {
OH_LOG_ERROR(LOG_APP, "hasFlash fail-----");
}
// Query whether the flash mode is supported.
bool isSupported = false;
ret = OH_CaptureSession_IsFlashModeSupported(captureSession_, flashMode, &isSupported);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_IsFlashModeSupported failed.");
}
if (isSupported) {
OH_LOG_INFO(LOG_APP, "isFlashModeSupported success-----");
} else {
OH_LOG_ERROR(LOG_APP, "isFlashModeSupported fail-----");
}
// Set the flash mode.
ret = OH_CaptureSession_SetFlashMode(captureSession_, flashMode);
if (ret == CAMERA_OK) {
OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetFlashMode success.");
} else {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetFlashMode failed. %{public}d.", ret);
}
// Obtain the flash mode in use.
ret = OH_CaptureSession_GetFlashMode(captureSession_, &flashMode);
if (ret == CAMERA_OK) {
OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetFlashMode success. flashMode: %{public}d.", flashMode);
} else {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetFlashMode failed. %{public}d.", ret);
}
return ret;
}
// Focus mode.
Camera_ErrorCode NDKCamera::IsFocusModeSupported(uint32_t mode)
{
Camera_FocusMode focusMode = static_cast<Camera_FocusMode>(mode);
ret_ = OH_CaptureSession_IsFocusModeSupported(captureSession_, focusMode, &isFocusModeSupported_);
if (&isFocusModeSupported_ == nullptr || ret_ != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "IsFocusModeSupported failed.");
return CAMERA_INVALID_ARGUMENT;
}
return ret_;
}
Camera_ErrorCode NDKCamera::IsFocusMode(uint32_t mode)
{
OH_LOG_INFO(LOG_APP, "IsFocusMode start.");
Camera_FocusMode focusMode = static_cast<Camera_FocusMode>(mode);
ret_ = OH_CaptureSession_IsFocusModeSupported(captureSession_, focusMode, &isFocusModeSupported_);
if (&isFocusModeSupported_ == nullptr || ret_ != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "IsFocusModeSupported failed.");
return CAMERA_INVALID_ARGUMENT;
}
ret_ = OH_CaptureSession_SetFocusMode(captureSession_, focusMode);
if (ret_ != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "SetFocusMode failed.");
return CAMERA_INVALID_ARGUMENT;
}
ret_ = OH_CaptureSession_GetFocusMode(captureSession_, &focusMode);
if (&focusMode == nullptr || ret_ != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "GetFocusMode failed.");
return CAMERA_INVALID_ARGUMENT;
}
OH_LOG_INFO(LOG_APP, "IsFocusMode end.");
return ret_;
}
Camera_ErrorCode NDKCamera::setZoomRatioFn(uint32_t zoomRatio)
{
float zoom = float(zoomRatio);
// Obtain the supported zoom range.
float minZoom;
float maxZoom;
Camera_ErrorCode ret = OH_CaptureSession_GetZoomRatioRange(captureSession_, &minZoom, &maxZoom);
if (captureSession_ == nullptr || ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetZoomRatioRange failed.");
} else {
OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetZoomRatioRange success. minZoom: %{public}f, maxZoom: %{public}f.",
minZoom, maxZoom);
}
// Set the zoom ratio.
ret = OH_CaptureSession_SetZoomRatio(captureSession_, zoom);
if (ret == CAMERA_OK) {
OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetZoomRatio success.");
} else {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_SetZoomRatio failed. %{public}d.", ret);
}
// Obtain the zoom ratio of the current device.
ret = OH_CaptureSession_GetZoomRatio(captureSession_, &zoom);
if (ret == CAMERA_OK) {
OH_LOG_INFO(LOG_APP, "OH_CaptureSession_GetZoomRatio success. zoom: %{public}f.", zoom);
} else {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_GetZoomRatio failed. %{public}d.", ret);
}
return ret;
}