Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.
HarmonyOS
Metadata is the description and context of image information returned by the camera application. It provides detailed data for the image information, such as the coordinates of a viewfinder frame for identifying a portrait in a photo or video.
Metadata primarily uses a TAG (Key) to locate the corresponding data (Value), enabling parameter and configuration information transfer while reducing memory copy operations.
For detailed API descriptions, see OH_Camera.
Import the NDK.
- #include <cstdint>
- #include <native_buffer/buffer_common.h>
- #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"
- #include <window_manager/oh_display_info.h>
- #include <window_manager/oh_display_manager.h>
-
- namespace OHOS_CAMERA_SAMPLE {
- class NDKCamera {
- public:
- struct CameraBuildingConfig {
- char *str;
- uint32_t focusMode;
- uint32_t cameraDeviceIndex;
- bool isVideo;
- bool isHdr;
- char *videoId;
- };
- ~NDKCamera();
- explicit NDKCamera(CameraBuildingConfig config);
- // ...
- };
- } // namespace OHOS_CAMERA_SAMPLE
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_GetSupportedCameraOutputCapability() to obtain the metadata types supported by the current device, and then call OH_CameraManager_CreateMetadataOutput() to create a metadata output stream.
- Camera_ErrorCode NDKCamera::CreateMetadataOutput(void)
- {
- metaDataObjectType_ = cameraOutputCapability_->supportedMetadataObjectTypes[0];
- if (metaDataObjectType_ == nullptr) {
- OH_LOG_ERROR(LOG_APP, "Get metaDataObjectType failed.");
- return CAMERA_INVALID_ARGUMENT;
- }
- ret_ = OH_CameraManager_CreateMetadataOutput(cameraManager_, metaDataObjectType_, &metadataOutput_);
- if (metadataOutput_ == nullptr || ret_ != CAMERA_OK) {
- OH_LOG_ERROR(LOG_APP, "CreateMetadataOutput failed.");
- return CAMERA_INVALID_ARGUMENT;
- }
- MetadataOutputRegisterCallback();
- return ret_;
- }
Call OH_CameraManager_CreateCaptureSession() to create a session.
- ret = OH_CameraManager_CreateCaptureSession(cameraManager_, &captureSession_);
- if (captureSession_ == nullptr || ret != CAMERA_OK) {
- OH_LOG_ERROR(LOG_APP, "Create captureSession failed.");
- }
Configure the session. After the configuration, call OH_CaptureSession_Start() to output metadata. If the call fails, an error code is returned. For details about the error code types, see Camera_ErrorCode.
- // Start configuring the session.
- Camera_ErrorCode ret = OH_CaptureSession_BeginConfig(captureSession_);
-
- // Add the camera input stream to the session.
- ret = OH_CaptureSession_AddInput(captureSession_, cameraInput_);
-
- // Add the camera preview stream to the session.
- ret = OH_CaptureSession_AddPreviewOutput(captureSession_, previewOutput_);
-
- if (isVideo_) {
- // Add the camera video stream to the session.
- AddVideoOutput();
- if (isHdrVideo) {
- // HDR Vivid video requires setting the color space to OH_COLORSPACE_BT2020_HLG_LIMIT.
- OH_NativeBuffer_ColorSpace colorSpace = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT2020_HLG_LIMIT;
- SetColorSpace(colorSpace);
- }
- } else {
- // Add the camera photo stream to the session.
- AddPhotoOutput();
- ret = CreateMetadataOutput();
- ret = OH_CaptureSession_AddMetadataOutput(captureSession_, metadataOutput_);
- OH_NativeBuffer_ColorSpace colorSpace = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_P3_FULL;
- SetColorSpace(colorSpace);
- }
-
- // Commit the session configuration.
- ret = OH_CaptureSession_CommitConfig(captureSession_);
- // ...
-
- InitPreviewRotation();
- // Start the session.
- OH_LOG_INFO(LOG_APP, "session start");
- ret = OH_CaptureSession_Start(captureSession_);
Call OH_MetadataOutput_Stop() to stop outputting metadata. If the API call fails, a corresponding error code is returned.
- Camera_ErrorCode StopMetadataOutput(Camera_MetadataOutput* metadataOutput)
- {
- Camera_ErrorCode ret = OH_MetadataOutput_Stop(metadataOutput);
- if (ret != CAMERA_OK) {
- OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_Stop failed.");
- }
- return ret;
- }
During camera application development, you can listen for the status of metadata objects and output stream.
Register the 'metadataObjectsAvailable' event to listen for metadata objects that are available. When a valid metadata object is detected, the callback function returns the metadata. This event can be registered when a MetadataOutput object is created.
- void OnMetadataObjectAvailable(Camera_MetadataOutput *metadataOutput, Camera_MetadataObject *metadataObject,
- uint32_t size)
- {
- OH_LOG_INFO(LOG_APP, "size = %{public}d", size);
- }
Currently, only FACE_DETECTION is available for the metadata type. The metadata object is the rectangle of the recognized face, including the x-axis coordinate and y-axis coordinate of the top-left corner of the rectangle as well as the width and height of the rectangle.
Register the 'error' event to listen for metadata stream 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 OnMetadataOutputError(Camera_MetadataOutput *metadataOutput, Camera_ErrorCode errorCode)
- {
- OH_LOG_ERROR(LOG_APP, "OnMetadataOutput errorCode = %{public}d", errorCode);
- }
- MetadataOutput_Callbacks *NDKCamera::GetMetadataOutputListener(void)
- {
- static MetadataOutput_Callbacks metadataOutputListener = {
- .onMetadataObjectAvailable = OnMetadataObjectAvailable,
- .onError = OnMetadataOutputError
- };
- return &metadataOutputListener;
- }
-
- Camera_ErrorCode NDKCamera::MetadataOutputRegisterCallback(void)
- {
- ret_ = OH_MetadataOutput_RegisterCallback(metadataOutput_, GetMetadataOutputListener());
- if (ret_ != CAMERA_OK) {
- OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_RegisterCallback failed.");
- }
- return ret_;
- }
Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
Quick start
Helps you find desired resources with ease.