Interface (Flash)

Flash inherits from FlashQuery.

It provides APIs related to the flash.

NOTE

  • The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
  • The initial APIs of this interface are supported since API version 11.

Modules to Import

import { camera } from '@kit.CameraKit';

setFlashMode11+

setFlashMode(flashMode: FlashMode): void

Sets a flash mode.

Before the setting, do the following checks:

  1. Use hasFlash to check whether the camera device has flash.
  2. Use isFlashModeSupported to check whether the camera device supports the flash mode.

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

Name Type Mandatory Description
flashMode FlashMode Yes Flash mode. If the input parameter is null or undefined, it is treated as 0 and the flash is turned off.

Error codes

For details about the error codes, see Camera Error Codes.

ID Error Message
7400103 Session not config.

Example

import { BusinessError } from '@kit.BasicServicesKit';

function setFlashMode(photoSession: camera.PhotoSession): void {
  try {
    photoSession.setFlashMode(camera.FlashMode.FLASH_MODE_AUTO);
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The setFlashMode call failed. error code: ${err.code}`);
  }
}

getFlashMode11+

getFlashMode(): FlashMode

Obtains the flash mode in use.

Atomic service API: This API can be used in atomic services since API version 19.

System capability: SystemCapability.Multimedia.Camera.Core

Return value

Type Description
FlashMode Flash mode obtained. If the operation fails, undefined is returned and an error code defined in CameraErrorCode is thrown.

Error codes

For details about the error codes, see Camera Error Codes.

ID Error Message
7400103 Session not config.

Example

import { BusinessError } from '@kit.BasicServicesKit';

function getFlashMode(photoSession: camera.PhotoSession): camera.FlashMode | undefined {
  let flashMode: camera.FlashMode | undefined = undefined;
  try {
    flashMode = photoSession.getFlashMode();
  } catch (error) {
    // If the operation fails, error.code is returned and processed.
    let err = error as BusinessError;
    console.error(`The getFlashMode call failed.error code: ${err.code}`);
  }
  return flashMode;
}

onFlashStateChange24+

onFlashStateChange(callback: Callback<FlashState>): void

Subscribes to flash light status change events.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 24.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

Name Type Mandatory Description
callback Callback<FlashState> Yes Callback used to return the flash light status.

Example

function onFlashStateChange(photoSession: camera.PhotoSession): void {
  photoSession.onFlashStateChange((flashState: camera.FlashState) => {
    console.info(`Flash state changed: ${flashState}`);
  });
}

offFlashStateChange24+

offFlashStateChange(callback?: Callback<FlashState>): void

Unsubscribes from flash light status change events.

Model restriction: This API can be used only in the stage model.

Atomic service API: This API can be used in atomic services since API version 24.

System capability: SystemCapability.Multimedia.Camera.Core

Parameters

Name Type Mandatory Description
callback Callback<FlashState> No Callback used to return the result. If this parameter is specified, the subscription to the specified event with the specified callback is canceled. (The callback object cannot be an anonymous function.) Otherwise, the subscriptions to the specified event with all the callbacks are canceled.

Example

function offFlashStateChange(photoSession: camera.PhotoSession): void {
  photoSession.offFlashStateChange();
}