1# 手电筒使用(ArkTS)
2
3手电筒模式的使用是通过操作手机启用手电筒功能,使设备的手电筒功能持续保持常亮状态。
4
5在使用相机应用并操作手电筒功能时,存在以下几种情况说明:
6- 当使用后置摄像头并设置闪光灯模式[FlashMode](../../reference/apis-camera-kit/js-apis-camera.md#flashmode)关闭时,手电筒功能无法启用。
7- 当使用前置摄像头时,手电筒可以正常启用并保持常亮状态。
8- 从前置摄像头切换至后置摄像头时,如果手电筒原本处于开启状态,它将会被自动关闭。
9
10## 开发步骤
11
12详细的API说明请参考[Camera API参考](../../reference/apis-camera-kit/js-apis-camera.md)。
13
141. 导入camera接口,接口中提供了相机相关的属性和方法,导入方法如下。
15
16    ```ts
17    import { camera } from '@kit.CameraKit';
18    import { BusinessError } from '@kit.BasicServicesKit';
19    ```
20
212. 通过[CameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager)类中的[isTorchSupported](../../reference/apis-camera-kit/js-apis-camera.md#istorchsupported11)方法,检测当前设备是否支持手电筒功能。
22
23    ```ts
24    function isTorchSupported(cameraManager: camera.CameraManager) : boolean {
25        let torchSupport: boolean = false;
26        try {
27            torchSupport = cameraManager.isTorchSupported();
28        } catch (error) {
29            let err = error as BusinessError;
30            console.error('Failed to torch. errorCode = ' + err.code);
31        }
32        console.info('Returned with the torch support status:' + torchSupport);
33        return torchSupport;
34    }
35    ```
36
373. 通过[CameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager)类中的[isTorchModeSupported](../../reference/apis-camera-kit/js-apis-camera.md#istorchmodesupported11)方法,检测是否支持指定的手电筒模式[TorchMode](../../reference/apis-camera-kit/js-apis-camera.md#torchmode11)。
38
39    ```ts
40    function isTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : boolean {
41        let isTorchModeSupport: boolean = false;
42        try {
43            isTorchModeSupport = cameraManager.isTorchModeSupported(torchMode);
44        } catch (error) {
45            let err = error as BusinessError;
46            console.error('Failed to set the torch mode. errorCode = ' + err.code);
47        }
48        return isTorchModeSupport;
49    }
50    ```
51
524. 通过[CameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager)类中的[setTorchMode](../../reference/apis-camera-kit/js-apis-camera.md#settorchmode11)方法,设置当前设备的手电筒模式。以及通过[CameraManager](../../reference/apis-camera-kit/js-apis-camera.md#cameramanager)类中的[getTorchMode](../../reference/apis-camera-kit/js-apis-camera.md#gettorchmode11)方法,获取当前设备的手电筒模式。
53
54    > **说明:**
55    > 在使用[getTorchMode](../../reference/apis-camera-kit/js-apis-camera.md#gettorchmode11)方法前,需要先注册监听手电筒的状态变化,请参考[状态监听](camera-torch-use.md#状态监听)。
56
57    ```ts
58    function setTorchModeSupported(cameraManager: camera.CameraManager, torchMode: camera.TorchMode) : void {
59        cameraManager.setTorchMode(torchMode);
60        let isTorchMode = cameraManager.getTorchMode();
61        console.info(`Returned with the torch mode supportd mode: ${isTorchMode}`);
62    }
63    ```
64
65
66## 状态监听
67
68在相机应用开发过程中,可以随时监听手电筒状态,包括手电筒打开、手电筒关闭、手电筒不可用、手电筒恢复可用。手电筒状态发生变化,可通过回调函数获取手电筒模式的变化。
69
70通过注册torchStatusChange事件,通过回调返回监听结果,callback返回TorchStatusInfo参数,参数的具体内容可参考相机管理器回调接口实例[TorchStatusInfo](../../reference/apis-camera-kit/js-apis-camera.md#torchstatusinfo11)。
71
72
73```ts
74function onTorchStatusChange(cameraManager: camera.CameraManager): void {
75    cameraManager.on('torchStatusChange', (err: BusinessError, torchStatusInfo: camera.TorchStatusInfo) => {
76        if (err !== undefined && err.code !== 0) {
77            console.error(`Callback Error, errorCode: ${err.code}`);
78            return;
79        }
80        console.info(`onTorchStatusChange, isTorchAvailable: ${torchStatusInfo.isTorchAvailable}, isTorchActive: ${torchStatusInfo.
81            isTorchActive}, level: ${torchStatusInfo.torchLevel}`);
82    });
83}
84```