harmony 鸿蒙Using the Flashlight (ArkTS)
Using the Flashlight (ArkTS)
To use the flashlight mode, you manipulate your phone 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: - When the rear camera is used and FlashMode is set to off, the flashlight cannot be turned on. - When the front camera is used, the flashlight can be turned on and remains steady on. - When you switch from the front camera to the rear camera, the flashlight will be automatically turned off if it was turned on previously.
How to Develop
Read Camera for the API reference.
Import the camera module, which provides camera-related attributes and methods.
import { camera } from '@kit.CameraKit'; import { BusinessError } from '@kit.BasicServicesKit';
Call isTorchSupported in the CameraManager class 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; }
Call isTorchModeSupported in the CameraManager class 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 set the torch mode. errorCode = ' + err.code); } return isTorchModeSupport; }
Call setTorchMode in the CameraManager class to set the flashlight mode, and then getTorchMode in the CameraManager class to obtain the flashlight mode in use.
NOTE
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 supportd 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}`);
});
}
你可能感兴趣的鸿蒙文章
harmony 鸿蒙Basic Camera Animation (ArkTS)
harmony 鸿蒙Practices for Camera Recovery from the Background (ArkTS)
harmony 鸿蒙Deferred Photo Delivery Practices (ArkTS)
harmony 鸿蒙Deferred Photo Delivery (ArkTS)
harmony 鸿蒙Practices for High-Performance Photo Capture (for System Applications Only) (ArkTS)
harmony 鸿蒙High-Performance Photo Capture (for System Applications Only) (ArkTS)
harmony 鸿蒙Depth Data (for System Applications Only) (ArkTS)
- 所属分类: 后端技术
- 本文标签:
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦