harmony 鸿蒙Preview (C/C++)
Preview (C/C++)
Preview is the image you see after you start the camera application but before you take photos or record videos.
How to Develop
Read Camera for the API reference.
- Import the NDK, which provides camera-related attributes and methods.
// 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
libace_napi.z.so
libohcamera.so
libhilog_ndk.z.so
)
Obtain the surface ID.
The XComponent, the capabilities of which are provided by the UI, offers the surface ID for preview streams. For details, see XComponent.
Call OH_CameraManager_GetSupportedCameraOutputCapability() to obtain the preview capability supported by the current device based on the surface ID. Then call OH_CameraManager_CreatePreviewOutput() to create a PreviewOutput instance, with the parameters set to the cameraManager pointer, the first item in the previewProfiles array, the surface ID obtained in step 3, and the returned previewOutput pointer, respectively.
Camera_PreviewOutput* CreatePreviewOutput(char* previewSurfaceIdstr, Camera_Manager* cameraManager, const Camera_Profile* previewProfile) { Camera_PreviewOutput* previewOutput = nullptr; Camera_ErrorCode ret = OH_CameraManager_CreatePreviewOutput(cameraManager, previewProfile, previewSurfaceIdstr, &previewOutput); if (previewOutput == nullptr||ret != CAMERA_OK) { OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreatePreviewOutput failed."); } return previewOutput; }
Configure the session. Call commitConfig() to commit the session configuration, and then call start() to start outputting the preview stream. If the call fails, an error code is returned. For details, see Camera_ErrorCode.
Camera_ErrorCode SessionStart(Camera_CaptureSession* captureSession)
{
Camera_ErrorCode ret = OH_CaptureSession_Start(captureSession);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Start failed.");
}
return ret;
}
- Call OH_CaptureSession_Stop() to stop outputting the preview stream. If the call fails, an error code is returned. For details, see Camera_ErrorCode.
Camera_ErrorCode SessionStop(Camera_CaptureSession* captureSession)
{
Camera_ErrorCode ret = OH_CaptureSession_Stop(captureSession);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CaptureSession_Stop failed.");
}
return ret;
}
Status Listening
During camera application development, you can listen for the preview output stream status, including preview stream start, preview stream end, and preview stream output errors.
- Register the ‘frameStart’ event to listen for preview start events. This event can be registered when a PreviewOutput object is created and is triggered when the bottom layer starts exposure for the first time. The preview stream starts as long as a result is returned.
void PreviewOutputOnFrameStart(Camera_PreviewOutput* previewOutput)
{
OH_LOG_INFO(LOG_APP, "PreviewOutputOnFrameStart");
}
- Register the ‘frameEnd’ event to listen for preview end events. This event can be registered when a PreviewOutput object is created and is triggered when the last frame of preview ends. The preview stream ends as long as a result is returned.
void PreviewOutputOnFrameEnd(Camera_PreviewOutput* previewOutput, int32_t frameCount)
{
OH_LOG_INFO(LOG_APP, "PreviewOutput frameCount = %{public}d", frameCount);
}
- Register the ‘error’ event to listen for preview output errors. The callback function returns an error code when an API is incorrectly used. For details about the error code types, see Camera_ErrorCode.
void PreviewOutputOnError(Camera_PreviewOutput* previewOutput, Camera_ErrorCode errorCode)
{
OH_LOG_ERROR(LOG_APP, "PreviewOutput errorCode = %{public}d", errorCode);
}
PreviewOutput_Callbacks* GetPreviewOutputListener(void)
{
static PreviewOutput_Callbacks previewOutputListener = {
.onFrameStart = PreviewOutputOnFrameStart,
.onFrameEnd = PreviewOutputOnFrameEnd,
.onError = PreviewOutputOnError
};
return &previewOutputListener;
}
Camera_ErrorCode RegisterPreviewOutputCallback(Camera_PreviewOutput* previewOutput)
{
Camera_ErrorCode ret = OH_PreviewOutput_RegisterCallback(previewOutput, GetPreviewOutputListener());
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_PreviewOutput_RegisterCallback failed.");
}
return ret;
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Basic Camera Animation (ArkTS)
harmony 鸿蒙Practices for Camera Recovery from the Background (ArkTS)
harmony 鸿蒙Deferred Photo Delivery Practices (ArkTS)
harmony 鸿蒙Deferred Photo Delivery (ArkTS)
harmony 鸿蒙Practices for High-Performance Photo Capture (for System Applications Only) (ArkTS)
harmony 鸿蒙High-Performance Photo Capture (for System Applications Only) (ArkTS)
harmony 鸿蒙Depth Data (for System Applications Only) (ArkTS)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦