• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Camera Metadata (ArkTS)
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 parameter transfers and configurations, reducing memory copy operations.
6
7## How to Develop
8
9Read [Camera](../reference/apis/js-apis-camera.md) for the API reference.
10
111. Import the modules.
12   ```ts
13   import camera from '@ohos.multimedia.camera';
14   import { BusinessError } from '@ohos.base';
15   ```
16
172. Obtain the metadata types supported by the current device from **supportedMetadataObjectTypes** in **CameraOutputCapability**, and then use **createMetadataOutput()** to create a metadata output stream.
18
19   ```ts
20   function getMetadataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): camera.MetadataOutput | undefined {
21     let metadataObjectTypes: Array<camera.MetadataObjectType> = cameraOutputCapability.supportedMetadataObjectTypes;
22     let metadataOutput: camera.MetadataOutput | undefined = undefined;
23     try {
24       metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes);
25     } catch (error) {
26       let err = error as BusinessError;
27       console.error('Failed to createMetadataOutput, error code: '+ err.code);
28     }
29     return metadataOutput;
30   }
31   ```
32
333. Call **start()** to start outputting metadata. If the call fails, an error code is returned. For details, see [Camera Error Codes](../reference/apis/js-apis-camera.md#cameraerrorcode).
34
35   ```ts
36   function startMetadataOutput(metadataOutput: camera.MetadataOutput): void {
37     metadataOutput.start().then(() => {
38       console.info('Callback returned with metadataOutput started.');
39     }).catch((err: BusinessError) => {
40       console.error('Failed to metadataOutput start, error code: '+ err.code);
41     });
42   }
43   ```
44
454. Call **stop()** to stop outputting metadata. If the call fails, an error code is returned. For details, see [Camera Error Codes](../reference/apis/js-apis-camera.md#cameraerrorcode).
46
47   ```ts
48   function stopMetadataOutput(metadataOutput: camera.MetadataOutput): void {
49     metadataOutput.stop().then(() => {
50       console.info('Callback returned with metadataOutput stopped.');
51     }).catch((err: BusinessError) => {
52       console.error('Failed to metadataOutput stop '+ err.code);
53     });
54   }
55   ```
56
57## Status Listening
58
59During camera application development, you can listen for the status of metadata objects and output stream.
60
61- 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.
62
63  ```ts
64  function onMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void {
65    metadataOutput.on('metadataObjectsAvailable', (err: BusinessError, metadataObjectArr: Array<camera.MetadataObject>) => {
66      console.info(`metadata output metadataObjectsAvailable`);
67    });
68  }
69  ```
70
71  > **NOTE**
72  >
73  > 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.
74
75- 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 Error Codes](../reference/apis/js-apis-camera.md#cameraerrorcode).
76
77  ```ts
78  function onMetadataError(metadataOutput: camera.MetadataOutput): void {
79    metadataOutput.on('error', (metadataOutputError: BusinessError) => {
80      console.info(`Metadata output error code: ${metadataOutputError.code}`);
81    });
82  }
83  ```
84