手电筒使用(ArkTS)
手电筒模式的使用是通过操作手机启用手电筒功能,使设备的手电筒功能持续保持常亮状态。
在使用相机应用并操作手电筒功能时,存在以下几种情况说明:
- 当使用后置摄像头并设置闪光灯模式[FlashMode]关闭时,手电筒功能无法启用。
- 当使用前置摄像头时,手电筒可以正常启用并保持常亮状态。
- 从前置摄像头切换至后置摄像头时,如果手电筒原本处于开启状态,它将会被自动关闭。
开发步骤
-
导入camera接口,接口中提供了相机相关的属性和方法,导入方法如下。
import { camera } from '@kit.CameraKit'; import { BusinessError } from '@kit.BasicServicesKit'; ts
-
通过[CameraManager]类中的[isTorchSupported]方法,检测当前设备是否支持手电筒功能。
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; }