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 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"]]