您可以使用弱光增强会话来开启和关闭 Google 弱光增强功能。
Kotlin
dependencies {
val low_light_boost_version = "16.0.0-beta01"
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.0-beta01"
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()
,用于创建回调对象。您将在实际创建低光增强会话时调用该方法,如创建会话中所述。 - 当会话断开连接或被销毁时,系统会调用回调。系统不会在创建会话时调用该方法。如需检查会话是否已成功创建,请检查
LowLightBoostClient.createSession
返回的Task
对象。
创建会话
如需创建低光会话,请调用 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");
});
此代码的要点
- 您可以将
LowLightBoostOptions
对象传递给createSession()
以配置会话。此对象用于指定目标 Surface、要使用的相机的 ID 以及预览的尺寸等内容。 - 此代码假定您已打开与 Camera2 相机的连接,并使用该信息设置了
cameraId, previewWidth, previewHeight
的值。如需了解详情,请参阅 Camera2 文档。 enableLowLightBoost
是一个布尔值,用于指定是否应启动低光增强功能。createLowLightBoostCallback
是您编写的用于创建回调对象的方法。当会话断开连接或被销毁时,系统会调用此对象。LowLightBoostClient.createSession()
方法会返回一个Task
对象。您可以使用此对象设置成功和失败监听器。在成功监听器内捕获视频。- 您可以指定
Executor
来运行监听器。如果您未指定Executor
,监听器将在主线程上运行。在此代码中,我们假设lowLightBoostExecutor
是合适的Executor
。
开始相机预览
创建低光会话后,您可以开始相机预览流式传输。您应在传递给低光照明会话的 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;
}
}
此代码的要点
- 会话释放后,您不应调用其任何方法。您应清除指向会话的所有变量,如以下代码所示。