弱光增强

Android 15 引入了低光增强功能,这是一种新的自动曝光模式, 相机 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_MODEON_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
);