目录

Using the Flashlight (ArkTS)

To use the flashlight mode, you manipulate your device to turn on the flashlight, which then stays on persistently.

When you use the flashlight mode with a camera application, the following situations may occur:

How to Develop

For detailed API descriptions, refer to @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';
  2. Call isTorchSupported in CameraManager to check whether the current device supports the flashlight.

    function isTorchSupported(cameraManager: camera.CameraManager) : boolean {
      let torchSupport: boolean = false;
      try {
        torchSupport = cameraManager.isTorchSupported();
      } catch (error) {
        let err = error as BusinessError;
        console.error('Failed to torch. errorCode = ' + err.code);
      }
      console.info('Returned with the torch support status:' + torchSupport);
      return torchSupport;
    }
  3. Call isTorchModeSupported in CameraManager to check whether a specific TorchMode is supported.

    function isTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : boolean {
      let isTorchModeSupport: boolean = false;
      try {
        isTorchModeSupport = cameraManager.isTorchModeSupported(torchMode);
      } catch (error) {
        let err = error as BusinessError;
        console.error('Failed to check the torch mode. errorCode = ' + err.code);
      }
      return isTorchModeSupport;
    }
  4. Call setTorchMode in CameraManager to set the flashlight mode, and then getTorchMode in CameraManager to obtain the flashlight mode in use.

    Before using getTorchMode, register a listener for the flashlight status. For details, see Status Listening.

    function setTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : void {
      cameraManager.setTorchMode(torchMode);
      let isTorchMode = cameraManager.getTorchMode();
      console.info(`Returned with the torch mode supported mode: ${isTorchMode}`);
    }

Status Listening

During camera application development, you can listen for the flashlight status, including on, off, unavailable, and available.

Register the 'torchStatusChange' event and return the listening result through a callback, which carries the TorchStatusInfo parameter. For details about the parameter, see TorchStatusInfo.

function onTorchStatusChange(cameraManager: camera.CameraManager): void {
  cameraManager.on('torchStatusChange', (err: BusinessError, torchStatusInfo: camera.TorchStatusInfo) => {
    if (err !== undefined && err.code !== 0) {
      console.error(`Callback Error, errorCode: ${err.code}`);
      return;
    }
    console.info(`onTorchStatusChange, isTorchAvailable: ${torchStatusInfo.isTorchAvailable}, isTorchActive: ${torchStatusInfo.
      isTorchActive}, level: ${torchStatusInfo.torchLevel}`);
  });
}