如需使用 Google 弱光增强功能,您需要一个弱光增强客户端。
您可以使用该客户端检查是否已安装弱光增强模块,以及运行应用的设备和相机是否支持 Google 弱光增强功能。您还将使用该客户端创建 LowLightBoostSession。(您将使用该会话来开启和关闭弱光增强功能。)您还可以设置监听器,以便在低光增强功能处于启用状态时接收回调。
LowLightBoostClient 方法不会直接指示成功或失败。而是返回一个 Task 对象。您可以使用 Task 设置成功和失败监听器。这样,这些方法就可以异步指示成功或失败,这是必要的,因为这些方法需要与 Google Play 服务进行通信。
依赖项
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'
}
LowLightBoostClient 由 Google Play 服务 com.google.android.gms.cameralowlight 软件包提供。如需了解如何访问 Google Play 服务 API,请参阅 Google Play 服务文档。
创建客户端
您需要一个弱光增强客户端才能执行其他操作。以下代码会创建一个客户端:
Kotlin
val lowLightBoostClient = LowLightBoost.getClient(context)
Java
LowLightBoostClient lowLightBoostClient = LowLightBoost.getClient(context);
此代码的要点
LowLightBoost类提供静态方法getClient,该方法会返回LowLightBoostClient的实例。
检查是否支持弱光增强功能
获得客户端后,您可以检查运行应用的设备是否支持弱光增强功能。以下代码会检查是否支持弱光增强功能:
Kotlin
launch { try { // Await the result of the Task in a non-blocking way. val isSupported: Boolean = lowLightBoostClient .isCameraSupported(cameraId).await() Log.d(TAG, "isCameraSupported: $isSupported") if (isSupported) { // Create the low light boost session here. } } catch (e: Exception) { Log.e(TAG, "isCameraSupported failed", e) } }
Java
lowLightBoostClient
.isCameraSupported(cameraId)
.addOnSuccessListener(
lowLightBoostExecutor,
(isSupported) -> {
Log.d(TAG, "isCameraSupported: " + isSupported);
if (isSupported) {
// Create the low light boost session here
}
)
此代码的要点
cameraId假定为在其他位置创建的 Camera2 摄像头的 ID。LowLightBoostClient.isCameraSupported()会检查 Camera2 相机是否支持弱光增强功能。在某些情况下,设备可能支持低光增益,但其某个摄像头可能不支持,因此您需要同时检查这两者。LowLightBoostClient.isCameraSupported()方法会返回一个Task对象。您可以使用此对象来设置成功和失败监听器。在成功监听器内创建弱光增强会话。
检查是否已安装弱光增强模块
获得客户端后,您可以确认设备上是否安装了弱光增强模块。以下代码会检查模块是否已安装:
Kotlin
// Handle the Google Play services Task API with Kotlin coroutines // (kotlinx-coroutines-play-services). launch { try { val isInstalled: Boolean = lowLightBoostClient .isModuleInstalled().await() if (isInstalled) { Log.d(TAG, "Module is installed") try { openCamera(cameraId) } catch (e: CameraAccessException) { Log.e(TAG, "Failed to open camera", e) } } else { Log.d(TAG, "Module is not installed") launchInstallRequest() } } catch (e: Exception) { Log.e(TAG, "Failed to check module availability", e) } }
Java
lowLightBoostClient
.isModuleInstalled(context)
.addOnSuccessListener(
(isInstalled) -> {
if (isInstalled) {
Log.d(TAG, "Module is installed");
try {
openCamera(cameraId);
} catch (CameraAccessException e) {
Log.e(TAG, "Failed to open camera", e);
}
} else {
Log.d(TAG, "Module is not installed");
launchInstallRequest();
}
})
.addOnFailureListener(
(e) -> {
Log.e(TAG, "Failed to check module availability", e);
});
此代码的要点
- 此代码会打开一个相机拍摄会话,连接到由
cameraId标识的相机。如需了解详情,请参阅 Camera2 文档。 LowLightBoostClient.isModuleInstalled()方法会返回一个Task对象。您可以使用此对象设置成功和失败监听器。- 使用
Task.addOnSuccessListener()设置一个监听器,以便在对isModuleInstalled()的调用成功时调用该监听器。重要的是,如果成功监听器被调用,这只是告诉您客户端已成功查明模块是否安装在设备上。在监听器的正文中,您需要检查模块是否已实际安装。 - 如果尚未安装该模块,此代码段会通过调用
launchInstallRequest()方法来安装该模块。该方法在安装弱光增强模块中的代码段中定义。
安装弱光增强模块
如果设备上尚未安装弱光增强模块,您需要从 Google Play 服务下载并安装该模块。以下代码展示了如何执行此操作:
Kotlin
private suspend fun launchInstallRequest() { Log.v(TAG, "Launching install request") try { // Check if this device can support Google LLB. val isDeviceSupported: Boolean = lowLightBoostClient .isDeviceSupported().await() if (isDeviceSupported) { Log.d(TAG, "Device is supported") // Show download indicator, if needed. try { val isInstallSuccessful: Boolean = lowLightBoostClient .installModule( createInstallStatusCallback() ).await() if (isInstallSuccessful) { Log.d(TAG, "Module installed") // Hide download indicator, if needed. try { openCamera() } catch (e: CameraAccessException) { Log.e(TAG, "Failed to open camera", e) } } else { Log.d(TAG, "Module install failed") } } catch (e: Exception) { Log.e(TAG, "An error occurred installing the module:", e) } } else { Log.d(TAG, "Device is not supported") } } catch (e: Exception) { Log.e(TAG, "An error occurred checking device support:", e) } }
Java
private void launchInstallRequest() {
Log.v(TAG, "Launching install request");
// Check if this device can support Google LLB.
lowLightBoostClient
.isDeviceSupported(context)
.addOnSuccessListener(
(isDeviceSupported) -> {
if (isDeviceSupported) {
Log.d(TAG, "Device is supported");
// Show download indicator, if needed.
lowLightBoostClient
.installModule(
this,
createInstallStatusCallback()
)
.addOnSuccessListener(
(result) -> {
if (result) {
Log.d(TAG, "Module installed");
// Hide download indicator, if needed.
try {
openCamera();
} catch (CameraAccessException e) {
Log.e(TAG, "Failed to open camera", e);
}
} else {
Log.d(TAG, "Module install failed");
}
}
);
} else {
Log.d(TAG, "Device is not supported");
}
})
.addOnFailureListener(
(e) -> {
Log.e(TAG, "Failed to check device support", e);
});
}
此代码的要点
- 当您调用
LowLightBoostClient.installModule()时,您会传递一个实现LowLightBoostClient.InstallStatusCallback的回调对象。installModule()会调用该回调中的方法来指示下载状态。例如,如果下载暂停,installModule()会调用回调对象的onDownloadPause()方法。 在此代码段中,回调对象由
createInstallStatusCallback()方法创建。您需要自行编写该方法,如下所示:
Kotlin
private fun createInstallStatusCallback(): LowLightBoostClient.InstallStatusCallback = object : LowLightBoostClient.InstallStatusCallback { override fun onDownloadPending() { Log.d(TAG, "onDownloadPending") // Code here... } override fun onDownloadStart() { Log.d(TAG, "onDownloadStart") // Code here... } // other overrides here... // ... }
Java
private InstallStatusCallback createInstallStatusCallback() {
new LowLightBoostClient.InstallStatusCallback() {
@Override
public void onDownloadPending() {
Log.d(TAG, "onDownloadPending");
// Code here...
}
@Override
public void onDownloadStart() {
Log.d(TAG, "onDownloadStart");
// Code here...
}
// other overrides here...
}
LowLightBoostClient.isDeviceSupported()会检查 Android 设备和操作系统是否支持 Google 弱光增强功能。否则,请勿下载该模块。LowLightBoostClient.installModule()方法会返回一个Task对象。您可以使用此对象设置成功和失败监听器。安装完成后,成功监听器会通过打开相机来验证安装。在代码段中,这是通过调用
openCamera()完成的。您需要自行编写该方法。