• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Camera Metadata (C/C++)
2<!--Kit: Camera Kit-->
3<!--Subsystem: Multimedia-->
4<!--Owner: @qano-->
5<!--SE: @leo_ysl-->
6<!--TSE: @xchaosioda-->
7
8Metadata 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.
9
10Metadata uses a tag (key) to find the corresponding data during the transfer of parameters and configurations, reducing memory copy operations.
11
12## How to Develop
13
14Read [Camera](../../reference/apis-camera-kit/capi-oh-camera.md) for the API reference.
15
161. Import the NDK.
17
18   ```c++
19   // Include the NDK header files.
20   #include "hilog/log.h"
21   #include "ohcamera/camera.h"
22   #include "ohcamera/camera_input.h"
23   #include "ohcamera/capture_session.h"
24   #include "ohcamera/photo_output.h"
25   #include "ohcamera/preview_output.h"
26   #include "ohcamera/video_output.h"
27   #include "ohcamera/camera_manager.h"
28   ```
29
302. Link the dynamic library in the CMake script.
31
32   ```txt
33   target_link_libraries(entry PUBLIC
34       libace_napi.z.so
35       libohcamera.so
36       libhilog_ndk.z.so
37   )
38   ```
39
403. 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.
41
42   ```c++
43   Camera_MetadataOutput* CreateMetadataOutput(Camera_Manager* cameraManager,
44       Camera_OutputCapability* cameraOutputCapability)
45   {
46       Camera_MetadataOutput* metadataOutput = nullptr;
47       if (cameraOutputCapability->supportedMetadataObjectTypes == nullptr) {
48           return metadataOutput;
49       }
50       Camera_MetadataObjectType* metaDataObjectType = nullptr;
51       bool isSupported = false;
52       for (int index = 0; index < cameraOutputCapability->metadataProfilesSize; index++) {
53           if (cameraOutputCapability->supportedMetadataObjectTypes[index] != nullptr &&
54               *cameraOutputCapability->supportedMetadataObjectTypes[index] == FACE_DETECTION) {
55               metaDataObjectType = *cameraOutputCapability->supportedMetadataObjectTypes;
56               isSupported = true;
57               break;
58           }
59       }
60       if (!isSupported || metaDataObjectType == nullptr) {
61           OH_LOG_ERROR(LOG_APP, "FACE_DETECTION is not supported.");
62           return metadataOutput;
63       }
64
65       Camera_ErrorCode ret = OH_CameraManager_CreateMetadataOutput(cameraManager, metaDataObjectType, &metadataOutput);
66       if (metadataOutput == nullptr || ret != CAMERA_OK) {
67           OH_LOG_ERROR(LOG_APP, "CreateMetadataOutput failed.");
68       }
69       return metadataOutput;
70   }
71   ```
72
734. Call **start()** to start outputting metadata. If the call fails, an error code is returned.
74
75   ```c++
76   Camera_ErrorCode StartMetadataOutput(Camera_MetadataOutput* metadataOutput)
77   {
78       Camera_ErrorCode ret = OH_MetadataOutput_Start(metadataOutput);
79       if (ret != CAMERA_OK) {
80           OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_Start failed.");
81       }
82       return ret;
83   }
84   ```
85
865. Call **stop()** to stop outputting metadata. If the call fails, an error code is returned.
87
88   ```c++
89   Camera_ErrorCode StopMetadataOutput(Camera_MetadataOutput* metadataOutput)
90   {
91       Camera_ErrorCode ret = OH_MetadataOutput_Stop(metadataOutput);
92       if (ret != CAMERA_OK) {
93           OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_Stop failed.");
94       }
95       return ret;
96   }
97   ```
98
99## Status Listening
100
101During camera application development, you can listen for the status of metadata objects and output stream.
102
103- 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.
104  ```c++
105  void OnMetadataObjectAvailable(Camera_MetadataOutput* metadataOutput,
106      Camera_MetadataObject* metadataObject, uint32_t size)
107  {
108      OH_LOG_INFO(LOG_APP, "size = %{public}d", size);
109  }
110  ```
111
112  > **NOTE**
113  >
114  > 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.
115
116- 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/capi-camera-h.md#camera_errorcode).
117
118  ```c++
119  void OnMetadataOutputError(Camera_MetadataOutput* metadataOutput, Camera_ErrorCode errorCode)
120  {
121      OH_LOG_INFO(LOG_APP, "OnMetadataOutput errorCode = %{public}d", errorCode);
122  }
123  ```
124
125  ```c++
126  MetadataOutput_Callbacks* GetMetadataOutputListener(void)
127  {
128      static MetadataOutput_Callbacks metadataOutputListener = {
129          .onMetadataObjectAvailable = OnMetadataObjectAvailable,
130          .onError = OnMetadataOutputError
131      };
132      return &metadataOutputListener;
133  }
134  Camera_ErrorCode RegisterMetadataOutputCallback(Camera_MetadataOutput* metadataOutput)
135  {
136      Camera_ErrorCode ret = OH_MetadataOutput_RegisterCallback(metadataOutput, GetMetadataOutputListener());
137      if (ret != CAMERA_OK) {
138          OH_LOG_ERROR(LOG_APP, "OH_MetadataOutput_RegisterCallback failed.");
139      }
140      return ret;
141  }
142  ```
143