低光照增强

Android 15 引入了**低光增强**(Low Light Boost),这是一种新的自动曝光模式,可供 Camera 2夜间模式相机扩展 使用。低光增强会在低光照条件下自动调整预览流的亮度。这与夜间模式相机扩展创建静态图像的方式不同,因为夜间模式会组合一系列照片来创建单个增强图像。虽然夜间模式非常适合创建静态图像,但它无法创建连续的帧流,而低光增强可以。因此,低光增强使新的相机功能成为可能,例如:

  • 提供增强的图像预览,以便用户能够更好地构图他们的低光照照片。
  • 在低光照条件下扫描二维码。

如果您启用低光增强,它会在光线较暗时自动开启,在光线充足时自动关闭。

应用程序可以在低光照条件下录制预览流,以保存亮度更高的视频。

您可以使用 Camera2 或通过 相机扩展 使用低光增强。本文档介绍了如何使用 Camera2 来使用低光增强。如果设备支持,您也可以将低光增强与夜间模式相机扩展一起使用。

检查可用性

在使用低光增强之前,请检查设备是否支持它。如果可用,低光增强将是 camera2.CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES 中列出的曝光模式之一。(低光增强是其自身的自动曝光设置,因为其他自动曝光设置与低光增强执行的预览亮度调整不兼容。)

因此,要检查低光增强是否可用,请调用 CameraCharacteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES) 并检查返回的模式是否包含 ON_LOW_LIGHT_BOOST_BRIGHTNESS_PRIORITY

Kotlin

val characteristics = cameraManager.getCameraCharacteristics(cameraId)
val autoExposureModes =
    characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES)!!
val lowLightBoostSupported = autoExposureModes.contains(
        CameraMetadata.CONTROL_AE_MODE_ON_LOW_LIGHT_BOOST_BRIGHTNESS_PRIORITY)

if (lowLightBoostSupported) {
  // Enable Low Light Boost (next section)
} else {
  // Proceed without Low Light Boost
}

Java

CameraCharacteristics characteristics =
    mCameraManager.getCameraCharacteristics(cameraId);
int[] autoExposureModes =
    characteristics.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_MODES);
boolean lowLightBoostSupported = autoExposureModes.contains(
        CameraMetadata.CONTROL_AE_MODE_ON_LOW_LIGHT_BOOST_BRIGHTNESS_PRIORITY);

if (lowLightBoostSupported) {
  // Enable Low Light Boost (next section)
} else {
  // Proceed without Low Light Boost
}

启用低光增强

要在 Camera2 会话中启用低光增强,请将 CaptureRequest.CONTROL_AE_MODE 设置为 ON_LOW_LIGHT_BOOST_BRIGHTNESS_PRIORITY。执行此操作后,您需要确认低光增强是否已开启;您可以通过检查 CaptureResult.CONTROL_AE_MODE 字段来实现。您需要进行检查,因为低光增强与所有相机配置都不兼容。例如,由于 FPS 方面的考虑,高速录制不支持低光增强。如果未开启低光增强,您可能需要更改相机配置并重试。

Kotlin

val captureRequestBuilder = camera.createCaptureRequest(
  CameraDevice.TEMPLATE_PREVIEW)
if (isLowLightBoostAvailable(cameraId)) {
  captureRequestBuilder.set(
    CaptureRequest.CONTROL_AE_MODE,
    CameraMetadata.CONTROL_AE_MODE_ON_LOW_LIGHT_BOOST_BRIGHTNESS_PRIORITY
  )
}
// other capture request params

session.setRepeatingRequest(
  captureRequestBuilder.build(),
  object : CaptureCallback() {
    @Override
    fun onCaptureCompleted(session: CameraCaptureSession,
        request: CaptureRequest, result: TotalCaptureResult) {
      // verify Low Light Boost AE mode set successfully
      result.get(CaptureResult.CONTROL_AE_MODE) ==
          CameraMetadata.CONTROL_AE_MODE_ON_LOW_LIGHT_BOOST_BRIGHTNESS_PRIORITY
    }
  },
  cameraHandler
)

Java

CaptureRequest.Builder captureRequestBuilder =
  mCamera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
if (isLowLightBoostAvailable(cameraId)) {
  captureRequestBuilder.set(
    CaptureRequest.CONTROL_AE_MODE,
    CameraMetadata.CONTROL_AE_MODE_ON_LOW_LIGHT_BOOST_BRIGHTNESS_PRIORITY);
}
// other capture request params

mSession.setRepeatingRequest(
  captureRequestBuilder.build(),
  new CaptureCallback() {
    @Override
    public void onCaptureCompleted(CameraCaptureSession session,
        CaptureRequest request, TotalCaptureResult result) {
      // verify Low Light Boost AE mode set successfully
      result.get(CaptureResult.CONTROL_AE_MODE) ==
          CameraMetadata.CONTROL_AE_MODE_ON_LOW_LIGHT_BOOST_BRIGHTNESS_PRIORITY;
    }
  },
  mCameraHandler
);

监控低光增强

低光增强会在低光照条件下使预览流变亮,如果环境已经足够明亮以进行正常捕获,则不会产生任何影响。您可以通过检查 CaptureResult.CONTROL_LOW_LIGHT_BOOST_STATE 字段来确认低光增强当前是否处于活动状态。如果您已开启低光增强并且它当前处于活动状态,则该字段将设置为 CameraMetadata.CONTROL_LOW_LIGHT_BOOST_STATE_ACTIVE。然后,您可以显示一个月亮图标或其他一些指示预览正在变亮的指示。

Kotlin

session.setRepeatingRequest(
  captureRequestBuilder.build(),
  object : CaptureCallback() {
    @Override
    fun onCaptureCompleted(session: CameraCaptureSession,
        request: CaptureRequest, result: TotalCaptureResult) {
      // check if Low Light Boost is active or inactive
      if (result.get(CaptureResult.CONTROL_LOW_LIGHT_BOOST_STATE) ==
        CameraMetadata.CONTROL_LOW_LIGHT_BOOST_STATE_ACTIVE) {
        // Low Light Boost state is active
        // Show Moon Icon
      } else {
        // Low Light Boost state is inactive or AE mode is not set
        // to Low Light Boost
        // Hide Moon Icon
      }
    }
  },
  cameraHandler
)

Java

mSession.setRepeatingRequest(
  captureRequestBuilder.build(),
  new CaptureCallback() {
    @Override
    public void onCaptureCompleted(CameraCaptureSession session,
        CaptureRequest request, TotalCaptureResult result) {
      // check if Low Light Boost is active or inactive
      if (result.get(CaptureResult.CONTROL_LOW_LIGHT_BOOST_STATE) ==
        CameraMetadata.CONTROL_LOW_LIGHT_BOOST_STATE_ACTIVE) {
        // Low Light Boost state is active
        // Show Moon Icon
      } else {
        // Low Light Boost state is inactive or AE mode is not set
        // to Low Light Boost
        // Hide Moon Icon
      }
    }
  },
  mCameraHandler
);