创建和使用低光增强客户端

如需使用 Google 弱光增强功能,您需要弱光增强客户端。 您可以使用该客户端检查是否已安装弱光增强模块,以及应用所运行的设备和相机是否支持 Google 弱光增强功能。您还将使用该客户端创建 LowLightBoostSession。(您将使用此会话来开启和关闭低光增强功能。)您还可以设置监听器,以便在启用低光增强功能时接收回调。

LowLightBoostClient 方法不会直接指示成功或失败。而是返回 Task 对象。您可以使用 Task 设置成功和失败监听器。这样,这些方法就可以异步发出成功或失败信号,这是必要的,因为这些方法需要与 Google Play 服务进行通信。

依赖项

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'
}

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);

此代码的要点

检查是否安装了弱光增强模块

有了客户端后,您可以确认设备上是否安装了低光增强模块。以下代码会检查模块是否已安装:

Kotlin

// Handle the Google Play services Task API with Kotlin coroutines
// (kotlinx-coroutines-play-services)
launch {
  try {
    val isInstalled: Boolean = lowLightBoostClient
      .isModuleInstalled(context).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(context).await()

    if (isDeviceSupported) {
      Log.d(TAG, "Device is supported")
      // Show download indicator, if needed.

      try {
        val isInstallSuccessful: Boolean = lowLightBoostClient
          .installModule(context,
                        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);
      });
}

此代码的要点

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() 来完成的。您需要自行编写该方法。

检查是否支持弱光增强

有了客户端后,您可以检查应用所运行设备是否支持低光增强功能。以下代码会检查是否支持低光增强功能:

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 对象。您可以使用此对象设置成功和失败监听器。在成功监听器内创建弱光增强会话