harmony 鸿蒙Video Recording (C/C++)
Video Recording (C/C++)
As another important function of the camera application, video recording is the process of cyclic frame capture. To smooth video recording, you can follow step 5 in Photo Capture to set the resolution, flash, focal length, photo quality, and rotation angle.
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.
Create an AVRecorder instance, and call getInputSurface() of the instance to obtain a surface ID.
- Create a video output stream.
Based on the surface ID passed in, obtain the video output streams supported by the current device from videoProfiles in the CameraOutputCapability class. Then, define video recording parameters and use createVideoOutput() to create a video output stream.
Camera_VideoOutput* CreateVideoOutput(Camera_Manager* cameraManager, char* videoSurfaceIdStr,
const Camera_VideoProfile* videoProfile)
{
// Create a VideoOutput instance.
Camera_VideoOutput* videoOutput = nullptr;
Camera_ErrorCode ret = OH_CameraManager_CreateVideoOutput(cameraManager, videoProfile, videoSurfaceIdStr,
&videoOutput);
if (videoProfile == nullptr||videoOutput == nullptr||ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_CameraManager_CreateVideoOutput failed.");
}
return videoOutput;
}
- Start video recording.
Call OH_VideoOutput_Start() of the VideoOutput instance to start the video output stream.
// Start the video output stream.
Camera_ErrorCode VideoOutputStart(Camera_VideoOutput* videoOutput)
{
Camera_ErrorCode ret = OH_VideoOutput_Start(videoOutput);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Start failed.");
}
return ret;
}
- Stop video recording.
Call OH_VideoOutput_Stop() of the VideoOutput instance to stop the video output stream.
// Stop the video output stream.
Camera_ErrorCode VideoOutputStop(Camera_VideoOutput* videoOutput)
{
Camera_ErrorCode ret = OH_VideoOutput_Stop(videoOutput);
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_Stop failed.");
}
return ret;
}
Status Listening
During camera application development, you can listen for the status of the video output stream, including recording start, recording end, and video output errors.
- Register the ‘frameStart’ event to listen for recording start events. This event can be registered when a VideoOutput instance is created and is triggered when the bottom layer starts exposure for recording for the first time. Video recording starts as long as the event is triggered.
void VideoOutputOnFrameStart(Camera_VideoOutput* videoOutput)
{
OH_LOG_INFO(LOG_APP, "VideoOutputOnFrameStart");
}
- Register the ‘frameEnd’ event to listen for recording end events. This event can be registered when a VideoOutput instance is created and is triggered when the last frame of recording ends. Video recording ends as long as a result is returned.
void VideoOutputOnFrameEnd(Camera_VideoOutput* videoOutput, int32_t frameCount)
{
OH_LOG_INFO(LOG_APP, "VideoOutput frameCount = %{public}d", frameCount);
}
- Register the ‘error’ event to listen for video 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 VideoOutputOnError(Camera_VideoOutput* videoOutput, Camera_ErrorCode errorCode)
{
OH_LOG_INFO(LOG_APP, "VideoOutput errorCode = %{public}d", errorCode);
}
VideoOutput_Callbacks* GetVideoOutputListener(void)
{
static VideoOutput_Callbacks videoOutputListener = {
.onFrameStart = VideoOutputOnFrameStart,
.onFrameEnd = VideoOutputOnFrameEnd,
.onError = VideoOutputOnError
};
return &videoOutputListener;
}
Camera_ErrorCode RegisterVideoOutputCallback(Camera_VideoOutput* videoOutput)
{
Camera_ErrorCode ret = OH_VideoOutput_RegisterCallback(videoOutput, GetVideoOutputListener());
if (ret != CAMERA_OK) {
OH_LOG_ERROR(LOG_APP, "OH_VideoOutput_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框自动聚焦