1e41f4b71Sopenharmony_ci# Camera Metadata (ArkTS) 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 parameter transfers and configurations, reducing memory copy operations. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci## How to Develop 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ciRead [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci1. Import the modules. 12e41f4b71Sopenharmony_ci ```ts 13e41f4b71Sopenharmony_ci import { camera } from '@kit.CameraKit'; 14e41f4b71Sopenharmony_ci import { BusinessError } from '@kit.BasicServicesKit'; 15e41f4b71Sopenharmony_ci ``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci2. Obtain the metadata types supported by the current device from **supportedMetadataObjectTypes** in the [CameraOutputCapability](../../reference/apis-camera-kit/js-apis-camera.md#cameraoutputcapability) class, and then use [createMetadataOutput](../../reference/apis-camera-kit/js-apis-camera.md#createmetadataoutput) to create a metadata output stream. 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci ```ts 20e41f4b71Sopenharmony_ci function getMetadataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): camera.MetadataOutput | undefined { 21e41f4b71Sopenharmony_ci let metadataObjectTypes: Array<camera.MetadataObjectType> = cameraOutputCapability.supportedMetadataObjectTypes; 22e41f4b71Sopenharmony_ci let metadataOutput: camera.MetadataOutput | undefined = undefined; 23e41f4b71Sopenharmony_ci try { 24e41f4b71Sopenharmony_ci metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes); 25e41f4b71Sopenharmony_ci } catch (error) { 26e41f4b71Sopenharmony_ci let err = error as BusinessError; 27e41f4b71Sopenharmony_ci console.error(`Failed to createMetadataOutput, error code: ${err.code}`); 28e41f4b71Sopenharmony_ci } 29e41f4b71Sopenharmony_ci return metadataOutput; 30e41f4b71Sopenharmony_ci } 31e41f4b71Sopenharmony_ci ``` 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci3. Call [Session.start](../../reference/apis-camera-kit/js-apis-camera.md#start11) to start outputting metadata, and obtain the data through subscription to the **'metadataObjectsAvailable'** event. If the call fails, an error code is returned. For details, see [CameraErrorCode](../../reference/apis-camera-kit/js-apis-camera.md#cameraerrorcode). 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci For details about how to obtain preview output, see [Camera Preview (ArkTS)](camera-preview.md). 36e41f4b71Sopenharmony_ci ```ts 37e41f4b71Sopenharmony_ci async function startMetadataOutput(previewOutput: camera.PreviewOutput, metadataOutput: camera.MetadataOutput, cameraManager: camera.CameraManager): Promise<void> { 38e41f4b71Sopenharmony_ci let cameraArray: Array<camera.CameraDevice> = []; 39e41f4b71Sopenharmony_ci cameraArray = cameraManager.getSupportedCameras(); 40e41f4b71Sopenharmony_ci if (cameraArray.length == 0) { 41e41f4b71Sopenharmony_ci console.error('no camera.'); 42e41f4b71Sopenharmony_ci return; 43e41f4b71Sopenharmony_ci } 44e41f4b71Sopenharmony_ci // Obtain the supported modes. 45e41f4b71Sopenharmony_ci let sceneModes: Array<camera.SceneMode> = cameraManager.getSupportedSceneModes(cameraArray[0]); 46e41f4b71Sopenharmony_ci let isSupportPhotoMode: boolean = sceneModes.indexOf(camera.SceneMode.NORMAL_PHOTO) >= 0; 47e41f4b71Sopenharmony_ci if (!isSupportPhotoMode) { 48e41f4b71Sopenharmony_ci console.error('photo mode not support'); 49e41f4b71Sopenharmony_ci return; 50e41f4b71Sopenharmony_ci } 51e41f4b71Sopenharmony_ci let cameraInput: camera.CameraInput | undefined = undefined; 52e41f4b71Sopenharmony_ci cameraInput = cameraManager.createCameraInput(cameraArray[0]); 53e41f4b71Sopenharmony_ci if (cameraInput === undefined) { 54e41f4b71Sopenharmony_ci console.error('cameraInput is undefined'); 55e41f4b71Sopenharmony_ci return; 56e41f4b71Sopenharmony_ci } 57e41f4b71Sopenharmony_ci // Open a camera. 58e41f4b71Sopenharmony_ci await cameraInput.open(); 59e41f4b71Sopenharmony_ci let session: camera.PhotoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession; 60e41f4b71Sopenharmony_ci session.beginConfig(); 61e41f4b71Sopenharmony_ci session.addInput(cameraInput); 62e41f4b71Sopenharmony_ci session.addOutput(previewOutput); 63e41f4b71Sopenharmony_ci session.addOutput(metadataOutput); 64e41f4b71Sopenharmony_ci await session.commitConfig(); 65e41f4b71Sopenharmony_ci await session.start(); 66e41f4b71Sopenharmony_ci } 67e41f4b71Sopenharmony_ci ``` 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci4. Call [Session.stop](../../reference/apis-camera-kit/js-apis-camera.md#stop11) to stop outputting metadata. If the call fails, an error code is returned. For details, see [CameraErrorCode](../../reference/apis-camera-kit/js-apis-camera.md#cameraerrorcode). 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci ```ts 72e41f4b71Sopenharmony_ci function stopMetadataOutput(session: camera.Session): void { 73e41f4b71Sopenharmony_ci session.stop().then(() => { 74e41f4b71Sopenharmony_ci console.info('Callback returned with session stopped.'); 75e41f4b71Sopenharmony_ci }).catch((err: BusinessError) => { 76e41f4b71Sopenharmony_ci console.error(`Failed to session stop, error code: ${err.code}`); 77e41f4b71Sopenharmony_ci }); 78e41f4b71Sopenharmony_ci } 79e41f4b71Sopenharmony_ci ``` 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci## Status Listening 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ciDuring camera application development, you can listen for the status of metadata objects and output stream. 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_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. 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci ```ts 88e41f4b71Sopenharmony_ci function onMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void { 89e41f4b71Sopenharmony_ci metadataOutput.on('metadataObjectsAvailable', (err: BusinessError, metadataObjectArr: Array<camera.MetadataObject>) => { 90e41f4b71Sopenharmony_ci if (err !== undefined && err.code !== 0) { 91e41f4b71Sopenharmony_ci return; 92e41f4b71Sopenharmony_ci } 93e41f4b71Sopenharmony_ci console.info('metadata output metadataObjectsAvailable'); 94e41f4b71Sopenharmony_ci }); 95e41f4b71Sopenharmony_ci } 96e41f4b71Sopenharmony_ci ``` 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci > **NOTE** 99e41f4b71Sopenharmony_ci > 100e41f4b71Sopenharmony_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. 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_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 [CameraErrorCode](../../reference/apis-camera-kit/js-apis-camera.md#cameraerrorcode). 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci ```ts 105e41f4b71Sopenharmony_ci function onMetadataError(metadataOutput: camera.MetadataOutput): void { 106e41f4b71Sopenharmony_ci metadataOutput.on('error', (metadataOutputError: BusinessError) => { 107e41f4b71Sopenharmony_ci console.error(`Metadata output error code: ${metadataOutputError.code}`); 108e41f4b71Sopenharmony_ci }); 109e41f4b71Sopenharmony_ci } 110e41f4b71Sopenharmony_ci ``` 111