The camera framework offers capabilities for focusing, enabling applications to configure focus modes and focus points based on specific use cases.
For details about the APIs, see Camera API Reference.
Import the NDK APIs.
// Include the NDK header files. #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 libohcamera.so libhilog_ndk.z.so)
Call OH_CaptureSession_SetFocusMode to set a focus mode.
Prior to this operation, call OH_CaptureSession_IsFocusModeSupported to check whether the device supports the specified focus mode.
This operation must be performed after the session calls OH_CaptureSession_CommitConfig to complete stream configuration.
Camera_ErrorCode SetFocusMode(Camera_CaptureSession *captureSession, uint32_t mode)
{
bool isFocusModeSupported = false;
Camera_FocusMode focusMode = static_cast<Camera_FocusMode>(mode);
Camera_ErrorCode ret = OH_CaptureSession_IsFocusModeSupported(captureSession, focusMode, &isFocusModeSupported);
if (&isFocusModeSupported == nullptr || ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "IsFocusModeSupported failed.");
return CAMERA_INVALID_ARGUMENT;
}
if (!isFocusModeSupported) {
OH_LOG_INFO(LOG_APP, "current focusMode(%{public}d) is not supported.", focusMode);
return CAMERA_OK;
}
OH_LOG_INFO(LOG_APP, "OH_CaptureSession_SetFocusMode focusMode(%{public}d).", focusMode);
ret = OH_CaptureSession_SetFocusMode(captureSession, focusMode);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "SetFocusMode failed.");
return CAMERA_INVALID_ARGUMENT;
}
return ret;
} If the focus mode is set to autofocus by using OH_CaptureSession_SetFocusMode, call OH_CaptureSession_SetFocusPoint to set the focus point. The camera triggers autofocus based on the focus point.
This operation must be performed after the session calls OH_CaptureSession_CommitConfig to complete stream configuration.
Camera_ErrorCode SetFocusPoint(Camera_CaptureSession *captureSession, float x, float y)
{
Camera_Point focusPoint;
focusPoint.x = x;
focusPoint.y = y;
Camera_ErrorCode ret = OH_CaptureSession_SetFocusPoint(captureSession, focusPoint);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "SetFocusPoint failed.");
return CAMERA_INVALID_ARGUMENT;
}
return ret;
} During camera application development, you can listen for camera focus status changes at any time.
Obtain the listening result by registering the callback function of OnFocusStateChange. This event is triggered only when the camera focus status changes in autofocus mode.
void CaptureSessionOnFocusStateChange(Camera_CaptureSession* captureSession, Camera_FocusState focusState)
{
OH_LOG_INFO(LOG_APP, "CaptureSession_Callbacks CaptureSessionOnFocusStateChange");
OH_LOG_INFO(LOG_APP, "CaptureSession focusState = %{public}d", focusState);
// To ensure user experience, you can set the focus mode to continuous autofocus after autofocus is successful.
if (focusState == Camera_FocusState::FOCUS_STATE_FOCUSED) {
Camera_ErrorCode ret = SetFocusMode(captureSession, Camera_FocusMode::FOCUS_MODE_CONTINUOUS_AUTO);
}
}
void CaptureSessionOnError(Camera_CaptureSession* captureSession, Camera_ErrorCode errorCode)
{
OH_LOG_INFO(LOG_APP, "CaptureSession_Callbacks CaptureSessionOnError");
OH_LOG_INFO(LOG_APP, "CaptureSession errorCode = %{public}d", errorCode);
}
CaptureSession_Callbacks* GetCaptureSessionRegister(void)
{
static CaptureSession_Callbacks captureSessionCallbacks = {
.onFocusStateChange = CaptureSessionOnFocusStateChange,
.onError = CaptureSessionOnError
};
return &captureSessionCallbacks;
} Camera_ErrorCode RegisterCallback(Camera_CaptureSession* captureSession)
{
Camera_ErrorCode ret = OH_CaptureSession_RegisterCallback(captureSession, GetCaptureSessionRegister());
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_RegisterCallback failed.");
}
return ret;
}