ใช้หน่วยแฟลชของอุปกรณ์ใน 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
เปิดใช้งานหน่วยแฟลช
เปิดใช้หน่วยแฟลชโดยกำหนดค่าเซสชัน AR ด้วยConfig.FlashMode.TORCH
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 })
}
ปิดใช้แฟลช
ปิดใช้หน่วยแฟลชโดยกำหนดค่าเซสชัน AR ด้วย Config.FlashMode.OFF
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 Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-02-28 UTC
[null,null,["อัปเดตล่าสุด 2025-02-28 UTC"],[[["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"]]