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.

Only Essential Cookies
Accept All

Camera Metadata (C/C++)

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.

How to Develop

For detailed API descriptions, see OH_Camera.

  1. Import the NDK.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. #include <cstdint>
    2. #include <native_buffer/buffer_common.h>
    3. #include <unistd.h>
    4. #include <string>
    5. #include <thread>
    6. #include <cstdio>
    7. #include <fcntl.h>
    8. #include <map>
    9. #include <vector>
    10. #include <native_buffer/native_buffer.h>
    11. #include "iostream"
    12. #include "mutex"
    13. #include "hilog/log.h"
    14. #include "ohcamera/camera.h"
    15. #include "ohcamera/camera_input.h"
    16. #include "ohcamera/capture_session.h"
    17. #include "ohcamera/photo_output.h"
    18. #include "ohcamera/preview_output.h"
    19. #include "ohcamera/video_output.h"
    20. #include "napi/native_api.h"
    21. #include "ohcamera/camera_manager.h"
    22. #include <window_manager/oh_display_info.h>
    23. #include <window_manager/oh_display_manager.h>
    24. namespace OHOS_CAMERA_SAMPLE {
    25. class NDKCamera {
    26. public:
    27. struct CameraBuildingConfig {
    28. char *str;
    29. uint32_t focusMode;
    30. uint32_t cameraDeviceIndex;
    31. bool isVideo;
    32. bool isHdr;
    33. char *videoId;
    34. };
    35. ~NDKCamera();
    36. explicit NDKCamera(CameraBuildingConfig config);
    37. // ...
    38. };
    39. } // namespace OHOS_CAMERA_SAMPLE
  2. Link the dynamic library in the CMake script.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. target_link_libraries(entry PUBLIC
    2. libace_napi.z.so
    3. libohcamera.so
    4. libhilog_ndk.z.so
    5. )
  3. 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.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. Camera_ErrorCode NDKCamera::CreateMetadataOutput(void)
    2. {
    3. metaDataObjectType_ = cameraOutputCapability_->supportedMetadataObjectTypes[0];
    4. if (metaDataObjectType_ == nullptr) {
    5. OH_LOG_ERROR(LOG_APP, "Get metaDataObjectType failed.");
    6. return CAMERA_INVALID_ARGUMENT;
    7. }
    8. ret_ = OH_CameraManager_CreateMetadataOutput(cameraManager_, metaDataObjectType_, &metadataOutput_);
    9. if (metadataOutput_ == nullptr || ret_ != CAMERA_OK) {
    10. OH_LOG_ERROR(LOG_APP, "CreateMetadataOutput failed.");
    11. return CAMERA_INVALID_ARGUMENT;
    12. }
    13. MetadataOutputRegisterCallback();
    14. return ret_;
    15. }
  4. Call OH_CameraManager_CreateCaptureSession() to create a session.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. ret = OH_CameraManager_CreateCaptureSession(cameraManager_, &captureSession_);
    2. if (captureSession_ == nullptr || ret != CAMERA_OK) {
    3. OH_LOG_ERROR(LOG_APP, "Create captureSession failed.");
    4. }
  5. 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.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // Start configuring the session.
    2. Camera_ErrorCode ret = OH_CaptureSession_BeginConfig(captureSession_);
    3. // Add the camera input stream to the session.
    4. ret = OH_CaptureSession_AddInput(captureSession_, cameraInput_);
    5. // Add the camera preview stream to the session.
    6. ret = OH_CaptureSession_AddPreviewOutput(captureSession_, previewOutput_);
    7. if (isVideo_) {
    8. // Add the camera video stream to the session.
    9. AddVideoOutput();
    10. if (isHdrVideo) {
    11. // HDR Vivid video requires setting the color space to OH_COLORSPACE_BT2020_HLG_LIMIT.
    12. OH_NativeBuffer_ColorSpace colorSpace = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_BT2020_HLG_LIMIT;
    13. SetColorSpace(colorSpace);
    14. }
    15. } else {
    16. // Add the camera photo stream to the session.
    17. AddPhotoOutput();
    18. ret = CreateMetadataOutput();
    19. ret = OH_CaptureSession_AddMetadataOutput(captureSession_, metadataOutput_);
    20. OH_NativeBuffer_ColorSpace colorSpace = OH_NativeBuffer_ColorSpace::OH_COLORSPACE_P3_FULL;
    21. SetColorSpace(colorSpace);
    22. }
    23. // Commit the session configuration.
    24. ret = OH_CaptureSession_CommitConfig(captureSession_);
    25. // ...
    26. InitPreviewRotation();
    27. // Start the session.
    28. OH_LOG_INFO(LOG_APP, "session start");
    29. ret = OH_CaptureSession_Start(captureSession_);
  6. Call OH_MetadataOutput_Stop() to stop outputting metadata. If the API call fails, a corresponding error code is returned.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. Camera_ErrorCode StopMetadataOutput(Camera_MetadataOutput* metadataOutput)
    2. {
    3. Camera_ErrorCode ret = OH_MetadataOutput_Stop(metadataOutput);
    4. if (ret != CAMERA_OK) {
    5. OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_Stop failed.");
    6. }
    7. return ret;
    8. }

Status Listening

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.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. void OnMetadataObjectAvailable(Camera_MetadataOutput *metadataOutput, Camera_MetadataObject *metadataObject,
    2. uint32_t size)
    3. {
    4. OH_LOG_INFO(LOG_APP, "size = %{public}d", size);
    5. }
    NOTE

    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.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. void OnMetadataOutputError(Camera_MetadataOutput *metadataOutput, Camera_ErrorCode errorCode)
    2. {
    3. OH_LOG_ERROR(LOG_APP, "OnMetadataOutput errorCode = %{public}d", errorCode);
    4. }
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. MetadataOutput_Callbacks *NDKCamera::GetMetadataOutputListener(void)
    2. {
    3. static MetadataOutput_Callbacks metadataOutputListener = {
    4. .onMetadataObjectAvailable = OnMetadataObjectAvailable,
    5. .onError = OnMetadataOutputError
    6. };
    7. return &metadataOutputListener;
    8. }
    9. Camera_ErrorCode NDKCamera::MetadataOutputRegisterCallback(void)
    10. {
    11. ret_ = OH_MetadataOutput_RegisterCallback(metadataOutput_, GetMetadataOutputListener());
    12. if (ret_ != CAMERA_OK) {
    13. OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_RegisterCallback failed.");
    14. }
    15. return ret_;
    16. }