示例参考如下:import { BusinessError } from '@kit.BasicServicesKit'; import { common } from '@kit.AbilityKit'; import { camera } from '@kit.CameraKit'; let context = getContext(this) as common.BaseContext; function setTorchMode(torchMode: camera.TorchMode): void { let cameraManager: camera.CameraManager = camera.getCameraManager(context); try { cameraManager.setTorchMode(torchMode); } catch (error) { // 失败返回错误码error.code并处理 let err = error as BusinessError; console.error(`The setTorchMode call failed. error code: ${err.code}`); } } @Entry @Component struct Page9 { @State message: string = 'Hello World'; @State Torch: boolean = true build() { Row() { Column() { Button() { Text("flash") .fontColor(Color.Black) .alignSelf(ItemAlign.Center) .onClick(() => { this.Torch = !this.Torch if (this.Torch) { setTorchMode(camera.TorchMode.ON) } else { setTorchMode(camera.TorchMode.OFF) } }) } .width(100) .height(100) } .width('100%') } .height('100%') } }
示例参考如下: