在 Android SDK (Kotlin/Java) 上使用设备的闪存单元
在 AR 会话期间启用设备的闪光单元有助于提高可见度。
检查当前摄像头配置是否支持闪光灯
并非所有相机配置都支持启用闪光灯。在启用闪光灯或向用户提供启用闪光灯的选项之前,请确保闪光灯单元适用于当前的相机配置:
boolean flashAvailable;
try {
CameraManager cameraManager =
(CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics =
cameraManager.getCameraCharacteristics(session.getCameraConfig().getCameraId());
flashAvailable = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
} catch (Exception e) {
flashAvailable = false;
}
val flashAvailable =
runCatching {
val cameraManager = context.getSystemService(Context.CAMERA_SERVICE) as CameraManager
val characteristics = cameraManager.getCameraCharacteristics(session.cameraConfig.cameraId)
characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE)
}
.getOrNull() ?: false
启用闪光灯
使用 Config.FlashMode.TORCH
配置 AR 会话,以启用闪光灯单元:
if (flashAvailable) {
Config config = session.getConfig();
config.setFlashMode(Config.FlashMode.TORCH);
session.configure(config);
}
if (flashAvailable) {
session.configure(session.config.apply { flashMode = Config.FlashMode.TORCH })
}
停用闪光灯
使用 Config.FlashMode.OFF
配置 AR 会话,以停用闪光灯单元:
Config config = session.getConfig();
config.setFlashMode(Config.FlashMode.OFF);
session.configure(config);
session.configure(session.config.apply { flashMode = Config.FlashMode.OFF })
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-02-28。
[null,null,["最后更新时间 (UTC):2025-02-28。"],[[["Enabling the device's flash during an AR session can enhance visibility in low-light environments."],["Before enabling the flash, verify if the active camera configuration supports it using `CameraCharacteristics.FLASH_INFO_AVAILABLE`."],["Use `Config.FlashMode.TORCH` to enable the flash and `Config.FlashMode.OFF` to disable it within the AR session configuration."],["Attempting to enable the flash on an unsupported camera configuration will have no adverse effect."]]],["To use the device's flash in an AR session, first verify if the current camera configuration supports it using `CameraCharacteristics.FLASH_INFO_AVAILABLE`. If available, enable the flash by setting the AR session's configuration to `Config.FlashMode.TORCH`. This is done through a camera manager service. If not available setting it will have no effect. To disable the flash, set the session's configuration to `Config.FlashMode.OFF`. Code examples in both Java and Kotlin are provided.\n"]]