1# Depth Data (for System Applications Only) (ArkTS) 2<!--Kit: Camera Kit--> 3<!--Subsystem: Multimedia--> 4<!--Owner: @qano--> 5<!--SE: @leo_ysl--> 6<!--TSE: @xchaosioda--> 7 8Depth 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. 9 10## How to Develop 11 12Read [Module Description](../../reference/apis-camera-kit/arkts-apis-camera.md) for the API reference. 13 141. Import the camera module, which provides camera-related attributes and methods. 15 16 ```ts 17 import { camera } from '@kit.CameraKit'; 18 import { BusinessError } from '@kit.BasicServicesKit'; 19 ``` 20 212. Use **depthProfiles** in the [CameraOutputCapability](../../reference/apis-camera-kit/arkts-apis-camera-i.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#createdepthdataoutput13) to create a depth data stream. 22 23 ```ts 24 function getDepthDataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): camera.DepthDataOutput | undefined { 25 let depthProfilesArray: Array<camera.DepthProfile> = cameraOutputCapability.depthProfiles; 26 if (!depthProfilesArray) { 27 console.error("createOutput depthProfilesArray is null"); 28 } 29 let depthDataOutput: camera.DepthDataOutput | undefined = undefined; 30 try { 31 depthDataOutput = cameraManager.createDepthDataOutput(depthProfilesArray[0]); 32 } catch (error) { 33 let err = error as BusinessError; 34 console.error(`Failed to create the DepthDataOutput instance. error: ${err}`); 35 } 36 return depthDataOutput; 37 } 38 ``` 39 403. Call [start](../../reference/apis-camera-kit/js-apis-camera-sys.md#start13) 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/arkts-apis-camera-e.md#cameraerrorcode). 41 42 ```ts 43 async function startDepthDataOutput(depthDataOutput: camera.DepthDataOutput): Promise<void> { 44 if (!depthDataOutput) { 45 console.error('depthDataOutput Undefined'); 46 return; 47 } 48 try { 49 await depthDataOutput.start(); 50 } catch (err) { 51 const error = err as BusinessError; 52 console.error(`Failed to start depth data output. error: ${err}`); 53 } 54 } 55 ``` 56 57## Status Listening 58 59During camera application development, you can listen for depth data and depth data output errors. 60 61- Register the fixed callback function **depthDataAvailable** to obtain the depth data. 62 63 ```ts 64 function onDepthDataAvailable(depthDataOutput: camera.DepthDataOutput): void { 65 depthDataOutput.on('depthDataAvailable', (err: BusinessError) => { 66 if (err !== undefined && err.code !== 0) { 67 return; 68 } 69 console.info('Depth data available'); 70 }); 71 } 72 ``` 73 74- 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/arkts-apis-camera-e.md#cameraerrorcode). 75 76 ```ts 77 function onDepthDataOutputError(depthDataOutput: camera.DepthDataOutput): void { 78 depthDataOutput.on('error', (depthDataOutputError: BusinessError) => { 79 console.error(`Depth data output error code: ${depthDataOutputError.code}`); 80 }); 81 } 82 ``` 83