目录

Metadata (ArkTS)

Before developing a camera application, you must request required permissions.

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

Metadata uses a tag (key) to find the corresponding data during parameter transfers and configurations, reducing memory copy operations.

When to Use

Detect faces in images and return high-precision face bounding box coordinates. Applications can integrate post-processing algorithms based on face detection results, making them suitable for a variety of scenarios.

How to Develop

For detailed API descriptions, see @ohos.multimedia.camera (Camera Management).

  1. Import the modules.

    import { camera } from '@kit.CameraKit';
    import { BusinessError } from '@kit.BasicServicesKit';
  2. Obtain the metadata types supported by the current device from supportedMetadataObjectTypes in CameraOutputCapability, and then use createMetadataOutput to create a metadata output stream.

    function getMetadataOutput(cameraManager: camera.CameraManager, cameraOutputCapability: camera.CameraOutputCapability): camera.MetadataOutput | undefined {
      let metadataObjectTypes: Array<camera.MetadataObjectType> = cameraOutputCapability.supportedMetadataObjectTypes;
      let metadataOutput: camera.MetadataOutput | undefined = undefined;
      try {
        metadataOutput = cameraManager.createMetadataOutput(metadataObjectTypes);
      } catch (error) {
        let err = error as BusinessError;
        console.error(`Failed to createMetadataOutput, error code: ${err.code}`);
      }
      return metadataOutput;
    }
  3. Call the Session.start method to start metadata data output, and then obtain data through the metadataObjectsAvailable event callback. If the API call fails, a corresponding error code is returned. For the error code type, see CameraErrorCode.

    For details about how to obtain preview output, see Camera Preview (ArkTS).

    async function startMetadataOutput(previewOutput: camera.PreviewOutput, metadataOutput: camera.MetadataOutput, cameraManager: camera.CameraManager): Promise<void> {
      let cameraArray: Array<camera.CameraDevice> = [];
      try {
        cameraArray = cameraManager.getSupportedCameras();
        if (cameraArray.length == 0) {
          console.error('no camera.');
          return;
        }
        // In this sample code, the first camera is selected. You need to select a camera as required.
        const cameraDevice: camera.CameraDevice = cameraArray[0];
        // Obtain the supported modes.
        let sceneModes: Array<camera.SceneMode> = cameraManager.getSupportedSceneModes(cameraDevice);
        let isSupportPhotoMode: boolean = sceneModes.indexOf(camera.SceneMode.NORMAL_PHOTO) >= 0;
        if (!isSupportPhotoMode) {
          console.error('photo mode not support');
          return;
        }
        let cameraInput: camera.CameraInput | undefined = undefined;
        cameraInput = cameraManager.createCameraInput(cameraDevice);
        if (cameraInput === undefined) {
          console.error('cameraInput is undefined');
          return;
        }
        // Open the camera.
        await cameraInput.open();
        let session = cameraManager.createSession(camera.SceneMode.NORMAL_PHOTO);
        if (!session) {
          console.error('session is null');
          return;
        }
        let photoSession: camera.PhotoSession = session as camera.PhotoSession;
        photoSession.beginConfig();
        photoSession.addInput(cameraInput);
        photoSession.addOutput(previewOutput);
        photoSession.addOutput(metadataOutput);
        await photoSession.commitConfig();
        await photoSession.start();
      } catch (error) {
        console.error('startMetadataOutput call failed');
      }
    }
  4. Call the Session.stop method to stop metadata data output. If the API call fails, a corresponding error code is returned. For the error code type, see CameraErrorCode.

    function stopMetadataOutput(session: camera.Session): void {
      session.stop().then(() => {
        console.info('Callback returned with session stopped.');
      }).catch((err: BusinessError) => {
        console.error(`Failed to session stop, error code: ${err.code}`);
      });
    }

Status Listening

During camera application development, you can listen for the status of metadata objects and output stream.