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