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-camera-kit/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 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. 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 [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). 34 35 For details about how to obtain preview output, see [Camera Preview (ArkTS)](camera-preview.md). 36 ```ts 37 async function startMetadataOutput(previewOutput: camera.PreviewOutput, metadataOutput: camera.MetadataOutput, cameraManager: camera.CameraManager): Promise<void> { 38 let cameraArray: Array<camera.CameraDevice> = []; 39 cameraArray = cameraManager.getSupportedCameras(); 40 if (cameraArray.length == 0) { 41 console.error('no camera.'); 42 return; 43 } 44 // Obtain the supported modes. 45 let sceneModes: Array<camera.SceneMode> = cameraManager.getSupportedSceneModes(cameraArray[0]); 46 let isSupportPhotoMode: boolean = sceneModes.indexOf(camera.SceneMode.NORMAL_PHOTO) >= 0; 47 if (!isSupportPhotoMode) { 48 console.error('photo mode not support'); 49 return; 50 } 51 let cameraInput: camera.CameraInput | undefined = undefined; 52 cameraInput = cameraManager.createCameraInput(cameraArray[0]); 53 if (cameraInput === undefined) { 54 console.error('cameraInput is undefined'); 55 return; 56 } 57 // Open a camera. 58 await cameraInput.open(); 59 let session: camera.PhotoSession = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO) as camera.PhotoSession; 60 session.beginConfig(); 61 session.addInput(cameraInput); 62 session.addOutput(previewOutput); 63 session.addOutput(metadataOutput); 64 await session.commitConfig(); 65 await session.start(); 66 } 67 ``` 68 694. 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 [Camera Error Codes](../reference/apis-camera-kit/js-apis-camera.md#cameraerrorcode). 70 71 ```ts 72 function stopMetadataOutput(session: camera.Session): void { 73 session.stop().then(() => { 74 console.info('Callback returned with session stopped.'); 75 }).catch((err: BusinessError) => { 76 console.error(`Failed to session stop, error code: ${err.code}`); 77 }); 78 } 79 ``` 80 81## Status Listening 82 83During camera application development, you can listen for the status of metadata objects and output stream. 84 85- 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. 86 87 ```ts 88 function onMetadataObjectsAvailable(metadataOutput: camera.MetadataOutput): void { 89 metadataOutput.on('metadataObjectsAvailable', (err: BusinessError, metadataObjectArr: Array<camera.MetadataObject>) => { 90 console.info('metadata output metadataObjectsAvailable'); 91 }); 92 } 93 ``` 94 95 > **NOTE** 96 > 97 > 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. 98 99- 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). 100 101 ```ts 102 function onMetadataError(metadataOutput: camera.MetadataOutput): void { 103 metadataOutput.on('error', (metadataOutputError: BusinessError) => { 104 console.error(`Metadata output error code: ${metadataOutputError.code}`); 105 }); 106 } 107 ``` 108