1# Depth Data (for System Applications Only) (ArkTS) 2 3Depth data reflects the spatial arrangement of image pixels in relation to the camera lens. It facilitates enhanced focus precision, background blurring effects, and the like. Depth data can be reported in the preview, photo capture, and video scenarios of camera applications. 4 5## How to Develop 6 7Read [Camera](../../reference/apis-camera-kit/js-apis-camera.md) for the API reference. 8 91. Import the camera module, which provides camera-related attributes and methods. 10 11 ```ts 12 import { camera } from '@kit.CameraKit'; 13 import { BusinessError } from '@kit.BasicServicesKit'; 14 ``` 15 162. Use **depthProfiles** in the [CameraOutputCapability](../../reference/apis-camera-kit/js-apis-camera.md#cameraoutputcapability) class to obtain the depth data capabilities, in the format of an **depthProfilesArray** array, supported by the current device. Call [createDepthDataOutput](../../reference/apis-camera-kit/js-apis-camera-sys.md#createdepthdataoutput) to create a depth data stream. 17 18 ```ts 19 function getDepthDataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): camera.DepthDataOutput | undefined { 20 let depthProfilesArray: Array<camera.Profile> = cameraOutputCapability.depthProfiles; 21 let depthDataOutput: camera.DepthDataOutput | undefined = undefined; 22 try { 23 depthDataOutput = cameraManager.createDepthDataOutput(depthProfilesArray[0]); 24 } catch (error) { 25 let err = error as BusinessError; 26 console.error(`Failed to create the DepthDataOutput instance. error: ${JSON.stringify(err)}`); 27 } 28 return depthDataOutput; 29 } 30 ``` 31 323. Call [start](../../reference/apis-camera-kit/js-apis-camera-sys.md#start) in the **depthDataOutput** class to start outputting the depth data stream. If the call fails, an error code is returned. For details about the error code types, see [CameraErrorCode](../../reference/apis-camera-kit/js-apis-camera.md#cameraerrorcode). 33 34 ```ts 35 async function startDepthDataOutput(depthDataOutput: camera.DepthDataOutput): Promise<void> { 36 if (!depthDataOutput) { 37 console.error('depthDataOutput Undefined'); 38 return; 39 } 40 try { 41 await depthDataOutput.start(); 42 } catch (err) { 43 const error = err as BusinessError; 44 console.error(`Failed to start depth data output. error: ${JSON.stringify(err)}`); 45 } 46 } 47 ``` 48 49## Status Listening 50 51During camera application development, you can listen for depth data and depth data output errors. 52 53- Register the fixed callback function **depthDataAvailable** to obtain the depth data. 54 55 ```ts 56 function onDepthDataAvailable(depthDataOutput: camera.DepthDataOutput): void { 57 depthDataOutput.on('depthDataAvailable', (err: BusinessError) => { 58 if (err !== undefined && err.code !== 0) { 59 return; 60 } 61 console.info('Depth data available'); 62 }); 63 } 64 ``` 65 66- Register the **'error'** event to listen for depth data output 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). 67 68 ```ts 69 function onDepthDataOutputError(depthDataOutput: camera.DepthDataOutput): void { 70 depthDataOutput.on('error', (depthDataOutputError: BusinessError) => { 71 console.error(`Depth data output error code: ${depthDataOutputError.code}`); 72 }); 73 } 74 ``` 75