使用弱光增强会话可开启和关闭 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.andr
oid.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")
lowLightBoostSessi
on = 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 lowLi
ghtBoostCallback;
}
此代码的相关要点
- 此代码定义了一个私有方法
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(
lowLightBoostEx>ecutor,
(task) - Log.d(TAG, "Session creation complete"))
.addOnCanceledListener(
l>owLightBoostExecutor,
() - {
throw new RuntimeException("Se
ssion creation canceled");
});
此代码的相关要点
- 您将
LowLightBoostOptions
对象传递给createSession()
以配置会话。此对象用于指定目标界面、要使用的相机的 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 c
apture 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 c
apture 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;
}
}
此代码的相关要点
- 会话释放后,您不应调用其任何方法。您应清除指向会话的所有变量,如本代码所示。