1e41f4b71Sopenharmony_ci# Camera Metadata (C/C++)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciMetadata 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.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciMetadata uses a tag (key) to find the corresponding data during the transfer of parameters and configurations, reducing memory copy operations.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci## How to Develop
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ciRead [Camera](../../reference/apis-camera-kit/_o_h___camera.md) for the API reference.
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci1. Import the NDK.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci   ```c++
14e41f4b71Sopenharmony_ci    // Include the NDK header files.
15e41f4b71Sopenharmony_ci    #include "hilog/log.h"
16e41f4b71Sopenharmony_ci    #include "ohcamera/camera.h"
17e41f4b71Sopenharmony_ci    #include "ohcamera/camera_input.h"
18e41f4b71Sopenharmony_ci    #include "ohcamera/capture_session.h"
19e41f4b71Sopenharmony_ci    #include "ohcamera/photo_output.h"
20e41f4b71Sopenharmony_ci    #include "ohcamera/preview_output.h"
21e41f4b71Sopenharmony_ci    #include "ohcamera/video_output.h"
22e41f4b71Sopenharmony_ci    #include "ohcamera/camera_manager.h"
23e41f4b71Sopenharmony_ci   ```
24e41f4b71Sopenharmony_ci
25e41f4b71Sopenharmony_ci2. Link the dynamic library in the CMake script.
26e41f4b71Sopenharmony_ci
27e41f4b71Sopenharmony_ci   ```txt
28e41f4b71Sopenharmony_ci    target_link_libraries(entry PUBLIC libohcamera.so libhilog_ndk.z.so)
29e41f4b71Sopenharmony_ci   ```
30e41f4b71Sopenharmony_ci
31e41f4b71Sopenharmony_ci3. 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.
32e41f4b71Sopenharmony_ci     
33e41f4b71Sopenharmony_ci   ```c++
34e41f4b71Sopenharmony_ci    Camera_Manager *cameraManager = nullptr;
35e41f4b71Sopenharmony_ci    Camera_Device* cameras = nullptr;
36e41f4b71Sopenharmony_ci    Camera_OutputCapability* cameraOutputCapability = nullptr;
37e41f4b71Sopenharmony_ci    Camera_MetadataOutput* metadataOutput = nullptr;
38e41f4b71Sopenharmony_ci    const Camera_MetadataObjectType* metaDataObjectType;
39e41f4b71Sopenharmony_ci    uint32_t size = 0;
40e41f4b71Sopenharmony_ci    uint32_t cameraDeviceIndex = 0;
41e41f4b71Sopenharmony_ci    char* previewSurfaceId = nullptr;
42e41f4b71Sopenharmony_ci    Camera_ErrorCode ret = OH_Camera_GetCameraManager(&cameraManager);
43e41f4b71Sopenharmony_ci    if (cameraManager == nullptr || ret != CAMERA_OK) {
44e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_Camera_GetCameraManager failed.");
45e41f4b71Sopenharmony_ci    }
46e41f4b71Sopenharmony_ci    ret = OH_CameraManager_GetSupportedCameras(cameraManager, &cameras, &size);
47e41f4b71Sopenharmony_ci    if (cameras == nullptr || size < 0 || ret != CAMERA_OK) {
48e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameras failed.");
49e41f4b71Sopenharmony_ci    }
50e41f4b71Sopenharmony_ci    ret = OH_CameraManager_GetSupportedCameraOutputCapability(cameraManager, &cameras[cameraDeviceIndex],
51e41f4b71Sopenharmony_ci                                                                      &cameraOutputCapability);
52e41f4b71Sopenharmony_ci    if (cameraOutputCapability == nullptr || ret != CAMERA_OK) {
53e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_CameraManager_GetSupportedCameraOutputCapability failed.");
54e41f4b71Sopenharmony_ci    }
55e41f4b71Sopenharmony_ci    if (cameraOutputCapability->previewProfilesSize < 0) {
56e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "previewProfilesSize == null");
57e41f4b71Sopenharmony_ci    }
58e41f4b71Sopenharmony_ci    metaDataObjectType = cameraOutputCapability->supportedMetadataObjectTypes[2]; // 2:camera metedata types
59e41f4b71Sopenharmony_ci    if (metaDataObjectType == nullptr) {
60e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "Get metaDataObjectType failed.");
61e41f4b71Sopenharmony_ci    }
62e41f4b71Sopenharmony_ci    
63e41f4b71Sopenharmony_ci    ret = OH_CameraManager_CreateMetadataOutput(cameraManager, metaDataObjectType, &metadataOutput);
64e41f4b71Sopenharmony_ci    if (metadataOutput == nullptr || ret != CAMERA_OK) {
65e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "CreateMetadataOutput failed.");
66e41f4b71Sopenharmony_ci    }
67e41f4b71Sopenharmony_ci   ```
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci4. Call **start()** to start outputting metadata. If the call fails, an error code is returned.
70e41f4b71Sopenharmony_ci     
71e41f4b71Sopenharmony_ci   ```c++
72e41f4b71Sopenharmony_ci    ret = OH_MetadataOutput_Start(metadataOutput);
73e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
74e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_Start failed.");
75e41f4b71Sopenharmony_ci    }
76e41f4b71Sopenharmony_ci   ```
77e41f4b71Sopenharmony_ci
78e41f4b71Sopenharmony_ci5. Call **stop()** to stop outputting metadata. If the call fails, an error code is returned.
79e41f4b71Sopenharmony_ci     
80e41f4b71Sopenharmony_ci   ```c++
81e41f4b71Sopenharmony_ci    ret = OH_MetadataOutput_Stop(metadataOutput);
82e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
83e41f4b71Sopenharmony_ci        OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_Stop failed.");
84e41f4b71Sopenharmony_ci    }
85e41f4b71Sopenharmony_ci   ```
86e41f4b71Sopenharmony_ci
87e41f4b71Sopenharmony_ci## Status Listening
88e41f4b71Sopenharmony_ci
89e41f4b71Sopenharmony_ciDuring camera application development, you can listen for the status of metadata objects and output stream.
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci- 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.
92e41f4b71Sopenharmony_ci    
93e41f4b71Sopenharmony_ci  ```c++
94e41f4b71Sopenharmony_ci    ret = OH_MetadataOutput_RegisterCallback(metadataOutput, GetMetadataOutputListener());
95e41f4b71Sopenharmony_ci    if (ret != CAMERA_OK) {
96e41f4b71Sopenharmony_ci      OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_RegisterCallback failed.");
97e41f4b71Sopenharmony_ci    }
98e41f4b71Sopenharmony_ci  ```
99e41f4b71Sopenharmony_ci  ```c++
100e41f4b71Sopenharmony_ci    void OnMetadataObjectAvailable(Camera_MetadataOutput* metadataOutput,
101e41f4b71Sopenharmony_ci        Camera_MetadataObject* metadataObject, uint32_t size)
102e41f4b71Sopenharmony_ci    {
103e41f4b71Sopenharmony_ci        OH_LOG_INFO(LOG_APP, "size = %{public}d", size);
104e41f4b71Sopenharmony_ci    }
105e41f4b71Sopenharmony_ci
106e41f4b71Sopenharmony_ci    MetadataOutput_Callbacks* GetMetadataOutputListener(void)
107e41f4b71Sopenharmony_ci    {
108e41f4b71Sopenharmony_ci        static MetadataOutput_Callbacks metadataOutputListener = {
109e41f4b71Sopenharmony_ci            .onMetadataObjectAvailable = OnMetadataObjectAvailable,
110e41f4b71Sopenharmony_ci            .onError = OnMetadataOutputError
111e41f4b71Sopenharmony_ci        };
112e41f4b71Sopenharmony_ci        return &metadataOutputListener;
113e41f4b71Sopenharmony_ci    }
114e41f4b71Sopenharmony_ci  ```
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci  > **NOTE**
117e41f4b71Sopenharmony_ci  >
118e41f4b71Sopenharmony_ci  > 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 upper left corner of the rectangle as well as the width and height of the rectangle.
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci- 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](../../reference/apis-camera-kit/_o_h___camera.md#camera_errorcode-1).
121e41f4b71Sopenharmony_ci    
122e41f4b71Sopenharmony_ci  ```c++
123e41f4b71Sopenharmony_ci    void OnMetadataOutputError(Camera_MetadataOutput* metadataOutput, Camera_ErrorCode errorCode)
124e41f4b71Sopenharmony_ci    {
125e41f4b71Sopenharmony_ci        OH_LOG_INFO(LOG_APP, "OnMetadataOutput errorCode = %{public}d", errorCode);
126e41f4b71Sopenharmony_ci    }
127e41f4b71Sopenharmony_ci  ```
128