低光增强

Android 15 引入了 *低光增强*,这是一种新的自动曝光模式,可供 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
);