使用弱光增强会话可开启和关闭 Google 弱光增强功能。
Kotlin
dependencies {
val low_light_boost_version = "16.0.1-beta04"
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.10.2")
implementation("com.google.android.gms:play-services-base:18.7.0")
implementation("com.google.android.gms:play-services-camera-low-light-boost:${low_light_boost_version}")
implementation("com.google.android.gms:play-services-tasks:18.3.0")
}
Groovy
dependencies {
def low_light_boost_version = "16.0.1-beta04"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.10.2'
implementation 'com.google.android.gms:play-services-base:18.7.0'
implementation 'com.google.android.gms:play-services-camera-low-light-boost:${low_light_boost_version}'
implementation 'com.google.android.gms:play-services-tasks:18.3.0'
}
LowLightBoostSession 由 Google Play 服务
com.google.android.gms.cameralowlight 软件包提供。如需了解如何访问 Google Play 服务
API,请参阅 Google
Play 服务文档。
创建回调对象
创建弱光增强会话时,您需要向其传递一个实现 LowLightBoostCallback 接口的对象。当会话断开连接或
被销毁时,系统会调用此对象的功能。以下代码展示了如何创建回调:
Kotlin
private fun createLowLightBoostCallback(): LowLightBoostCallback =
object : LowLightBoostCallback() {
override fun onSessionDestroyed() {
Log.d(TAG, "onSessionDestroyed")
lowLightBoostSession = null
}
override fun onSessionDisconnected(statusCode: Int) {
Log.d(TAG, "onSessionDisconnected: error=$statusCode")
lowLightBoostSession = null
}
}
Java
private LowLightBoostCallback createLowLightBoostCallback() {
LowLightBoostCallback lowLightBoostCallback = new LowLightBoostCallback() {
@Override
public void onSessionDestroyed() {
Log.d(TAG, "onSessionDestroyed");
lowLightBoostSession = null;
}
@Override
public void onSessionDisconnected(int statusCode) {
Log.d(TAG, "onSessionCreationFailed: error=" + statusCode);
lowLightBoostSession = null;
}
}
return lowLightBoostCallback;
}
有关此代码的关键点
- 此代码定义了一个私有方法
createLowLightBoostCallback(),用于 创建回调对象。您将在实际创建弱光增强会话时调用该方法,如创建会话中所述。 - 当会话断开连接或被销毁时,系统会调用回调。创建会话时,系统
不会调用回调。如需检查会话是否已
成功创建,请检查
Task对象,该对象由LowLightBoostClient.createSession返回。
创建会话
如需创建弱光会话,请调用方法
LowLightBoostClient.createSession。
Kotlin
val options = LowLightBoostOptions(
previewSurface,
cameraId,
previewWidth,
previewHeight,
enableLowLightBoost
)
launch {
try {
val lowLightBoostSession = lowLightBoostClient
.createSession(options, createLowLightBoostCallback()).await()
Log.d(TAG, "Session created successfully")
// Get the surface from the LLB session;
// give it to camera so camera can write frames to it
} catch (e: CancellationException) {
Log.w(TAG, "Session creation was canceled", e)
lowLightBoostSession = null
} catch (e: ApiException) {
Log.e(TAG, "Session creation failed with ApiException:", e)
lowLightBoostSession = null
} catch (e: Exception) {
Log.e(TAG, "Session creation failed with Exception", e)
lowLightBoostSession = null
}
}
Java
LowLightBoostOptions options = new LowLightBoostOptions(
previewSurface,
cameraId,
previewWidth,
previewHeight,
enableLowLightBoost);
lowLightBoostClient
.createSession(options, createLowLightBoostCallback())
.addOnSuccessListener(
lowLightBoostExecutor,
(session) -> {
Log.d(TAG, "Session created successfully");
// Get the surface from the LLB session;
// give it to camera so camera can write frames to it
})
.addOnFailureListener(
lowLightBoostExecutor,
(e) -> {
ApiException apiException = (ApiException) e;
Log.d(TAG, "Session creation failed: " + e);
lowLightBoostSession = null;
})
.addOnCompleteListener(
lowLightBoostExecutor,
(task) -> Log.d(TAG, "Session creation complete"))
.addOnCanceledListener(
lowLightBoostExecutor,
() -> {
throw new RuntimeException("Session creation canceled");
});
有关此代码的关键点
- 您需要向
createSession()传递一个LowLightBoostOptions对象,以 配置会话。此对象指定了目标 Surface、要使用的相机的 ID 以及预览的尺寸等内容。 - 此代码假定您已打开与 Camera2 相机的连接,
并使用该信息设置了
cameraId, previewWidth, previewHeight的值。如需了解详情,请参阅 Camera2 文档。 enableLowLightBoost是一个布尔值,用于指定弱光增强功能 应开启还是关闭。createLowLightBoostCallback是您编写的用于创建回调 对象的方法。当会话断开连接或被销毁时,系统会调用此对象。- 方法
LowLightBoostClient.createSession()会返回一个Task对象。您可以使用此对象设置成功和失败监听器。 在成功监听器内捕获视频。 - 您可以指定一个
Executor来运行监听器。如果您未 指定Executor,监听器将在主线程上运行。在此代码中, 我们假定lowLightBoostExecutor是合适的Executor。
传入捕获结果
Google 弱光增强功能需要某些相机元数据,才能了解要应用的正确亮度
。您必须将 TotalCaptureResult 传入
processCaptureResult() 方法。您可以在
onCaptureCompleted() 回调方法中获取 TotalCaptureResult。
Kotlin
val captureCallback = CameraCaptureSession.CaptureCallback() {
override fun onCaptureCompleted(
session: CameraCaptureSession,
request: CaptureRequest,
result: TotalCaptureResult
) {
super.onCaptureCompleted(session, request, result)
lowLightBoostSession?.processCaptureResult(result)
}
}
Java
CameraCaptureSession.CaptureCallback captureCallback =
new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(
@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request,
@NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result)
if (lowLightBoostSession != null) {
lowLightBoostSession.processCaptureResult(result);
}
}
};
有关此代码的关键点
- 此代码仅显示与 Google
LLB 相关的
CaptureCallback代码。您可能在这些回调中还有其他代码。 - 传入
TotalCaptureResult可让 Google LLB 分析 自动曝光数据和其他元数据,这些数据对于弱光增强功能 处理场景检测和确定要应用于 帧的增强量是必需的。 - 您应在创建相机
会议时传递
captureCallback对象,例如使用 `setSingleRepeatingRequest()。
启动相机预览
创建弱光会话后,您可以启动相机预览
流。您应在传递给弱光会话的 onSuccess() 回调中执行此操作,如创建会话中所述。以下代码展示了如何捕获
视频:
Kotlin
MainActivity.this.lowLightBoostSession =
lowLightBoostSession
MainActivity.this.lowLightBoostSession
.setSceneDetectorCallback(
(lowLightBoostSession, boostStrength) -> {
Log.d(TAG, "onSceneBrightnessChanged: " +
"boostStrength=$boostStrength")
// boostStrength > 0.5 indicates a low light scene.
// Update UI accordingly.
},
lowLightBoostExecutor
)
try {
startCaptureSession(
lowLightBoostSession.getCameraSurface())
// Start a Camera2 session here. Pass the LLB surface
// to the camera so the camera can write frames to it.
} catch (e: CameraAccessException) {
Log.e(TAG, "Failed to start capture session", e)
// Must try again or start the capture session without LLB.
}
Java
MainActivity.this.lowLightBoostSession =
lowLightBoostSession;
MainActivity.this.lowLightBoostSession
.setSceneDetectorCallback(
(lowLightBoostSession, boostStrength) -> {
Log.d(TAG, "onSceneBrightnessChanged: " +
"boostStrength=" + boostStrength);
// boostStrength > 0.5 indicates a low light scene.
// Update UI accordingly.
},
lowLightBoostExecutor
);
try {
startCaptureSession(
lowLightBoostSession.getCameraSurface());
// Start a Camera2 session here. Pass the LLB surface
// to the camera so the camera can write frames to it.
} catch (CameraAccessException e) {
Log.e(TAG, "Failed to start capture session", e);
// Must try again or start the capture session without LLB.
}
有关此代码的关键点
lowLightBoostSession是您在 创建会话 中创建的会话。setSceneDetectorCallback()定义了一个实现SceneDetectorCallback接口的回调对象。当场景亮度发生变化时,会话会调用该对象的onSceneBrightnessChanged()方法。 您的实现应相应地调整相机的界面。- 您可以指定一个
Executor来运行回调。如果您未 指定Executor,回调将在主线程上运行。在此代码中, 我们假定lowLightBoostExecutor是合适的Executor。 lowLightBoostSession.getCameraSurface()会返回包含捕获的视频的Surface。
释放会话
当相机不再处于活动状态时,请通过
调用 LowLightBoostSession.release() 释放弱光增强会话。特别是,您应确保在 activity 被销毁时释放会话。您可以通过在 activity 的 onDestroy() 方法中调用
该方法来完成此操作:
Kotlin
override protected void onDestroy() {
super.onDestroy()
if (lowLightBoostSession != null) {
lowLightBoostSession.release()
lowLightBoostSession = null
}
}
Java
@Override
protected void onDestroy() {
super.onDestroy();
if (lowLightBoostSession != null) {
lowLightBoostSession.release();
lowLightBoostSession = null;
}
}
有关此代码的关键点
- 释放会话后,您不应调用其任何方法。您 应清除指向会话的所有变量,如本代码所示。