Depth Data (for System Applications Only) (ArkTS)

Depth 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.

How to Develop

Read Camera for the API reference.

  1. Import the camera module, which provides camera-related properties and methods.

    import { camera } from '@kit.CameraKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
  2. Use depthProfiles in CameraOutputCapability to obtain the depth data capabilities, in the format of an depthProfilesArray array, supported by the current device. Call createDepthDataOutput to create a depth data stream.

    function getDepthDataOutput(cameraManager: camera.CameraManager,
     cameraOutputCapability: camera.CameraOutputCapability): camera.DepthDataOutput | undefined {
       let depthProfilesArray: Array<camera.DepthProfile> = cameraOutputCapability.depthProfiles;
       if (!depthProfilesArray) {
         console.error("createOutput depthProfilesArray is null");
         return undefined;
       }
       let depthDataOutput: camera.DepthDataOutput | undefined = undefined;
       try {
         if (depthProfilesArray.length > 0) {
           depthDataOutput = cameraManager.createDepthDataOutput(depthProfilesArray[0]);
         } else {
           console.error("the length of depthProfilesArray<=0!");
           return undefined;
         }
       } catch (error) {
         let err = error as BusinessError;
         console.error(`Failed to create the DepthDataOutput instance. error: ${err}`);
       }
       return depthDataOutput;
    }
    
  3. Call start in depthDataOutput 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.

    async function startDepthDataOutput(depthDataOutput: camera.DepthDataOutput): Promise<void> {
      if (!depthDataOutput) {
        console.error('depthDataOutput Undefined');
        return;
      }
      try {
        await depthDataOutput.start();
      } catch (err) {
        const error = err as BusinessError;
        console.error(`Failed to start depth data output. error: ${err}`);
      }
    }
    

Status Listening

During camera application development, you can listen for depth data and depth data output errors.

  • Register the fixed callback function depthDataAvailable to obtain the depth data.

    function onDepthDataAvailable(depthDataOutput: camera.DepthDataOutput): void {
      depthDataOutput.on('depthDataAvailable', (err: BusinessError) => {
        if (err !== undefined && err.code !== 0) {
          return;
        }
        console.info('Depth data available');
      });
    }
    
  • 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.

    function onDepthDataOutputError(depthDataOutput: camera.DepthDataOutput): void {
      depthDataOutput.on('error', (depthDataOutputError: BusinessError) => {
        console.error(`Depth data output error code: ${depthDataOutputError.code}`);
      });
    }