目录

Camera Device Management (ArkTS)

Before developing a camera app, you need to create a camera management instance by calling camera APIs.

How to Develop

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

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

    import { camera } from '@kit.CameraKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    import { common } from '@kit.AbilityKit';
  2. Call getCameraManager to obtain a CameraManager object.

    For details about how to obtain the context, see Obtaining the Context of UIAbility.

    function getCameraManager(context: common.BaseContext): camera.CameraManager | undefined {
      let cameraManager: camera.CameraManager;
      try {
        cameraManager = camera.getCameraManager(context);
      } catch (error) {
        let err = error as BusinessError;
        console.error(`getCameraManager error, errCode: ${err.code}`);
        return undefined;
      }
      return cameraManager;
    }

    If obtaining the object fails, the camera device may be occupied or unusable. If it is occupied, wait until it is released.

  3. Call the getSupportedCameras method in CameraManager to obtain the list of cameras supported by the current device. The list stores all camera IDs supported by the device. If the list is not empty, each ID in the list can be used to independently create a camera object. Otherwise, no camera is available on the current device, and subsequent operations cannot be performed.

    function getCameraDevices(cameraManager: camera.CameraManager): Array<camera.CameraDevice> {
      let cameraArray: Array<camera.CameraDevice> = cameraManager.getSupportedCameras();
      if (cameraArray != undefined && cameraArray.length > 0) {
        for (let index = 0; index < cameraArray.length; index++) {
          console.info('cameraId : ' + cameraArray[index].cameraId);  // Obtain the camera ID.
          console.info('cameraPosition : ' + cameraArray[index].cameraPosition);  // Obtain the camera position.
          console.info('cameraType : ' + cameraArray[index].cameraType);  // Obtain the camera type.
          console.info('connectionType : ' + cameraArray[index].connectionType);  // Obtain the camera connection type.
        }
        return cameraArray;
      } else {
        console.error("cameraManager.getSupportedCameras error");
        return [];
      }
    }

Status Listening

During camera application development, you can listen for the camera status, including the appearance of a new camera, removal of a camera, and availability of a camera. The camera ID and camera status are included in the callback function. When a new camera appears, the new camera can be added to the supported camera list.

Register the cameraStatus event to receive listening results through the callback. The callback returns a CameraStatusInfo parameter. For details about the parameter, see CameraStatusInfo.

function onCameraStatusChange(cameraManager: camera.CameraManager): void {
  cameraManager.on('cameraStatus', (err: BusinessError, cameraStatusInfo: camera.CameraStatusInfo) => {
    if (err !== undefined && err.code !== 0) {
      console.error(`Callback Error, errorCode: ${err.code}`);
      return;
    }
    // If a camera is connected through a USB cable, the callback function returns the CAMERA_STATUS_APPEAR status.
    if (cameraStatusInfo.status == camera.CameraStatus.CAMERA_STATUS_APPEAR) {
      console.info(`New Camera device appear.`);
    }
    // If the USB connection of the camera is interrupted, the callback function returns the CAMERA_STATUS_DISAPPEAR status.
    if (cameraStatusInfo.status == camera.CameraStatus.CAMERA_STATUS_DISAPPEAR) {
      console.info(`Camera device has been removed.`);
    }
    // If the camera is closed, the callback function returns the CAMERA_STATUS_AVAILABLE status.
    if (cameraStatusInfo.status == camera.CameraStatus.CAMERA_STATUS_AVAILABLE) {
      console.info(`Current Camera is available.`);
    }
    // If the camera is opened or occupied, the callback function returns the CAMERA_STATUS_UNAVAILABLE status.
    if (cameraStatusInfo.status == camera.CameraStatus.CAMERA_STATUS_UNAVAILABLE) {
      console.info(`Current Camera has been occupied.`);
    }
    console.info(`camera: ${cameraStatusInfo.camera.cameraId}`);
    console.info(`status: ${cameraStatusInfo.status}`);
  });
}