如要使用 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);
這段程式碼的重點
LowLightBoost
類別提供靜態方法getClient
,可傳回LowLightBoostClient
的例項。
檢查是否已安裝低光源增強模組
有了用戶端後,您可以確認裝置上是否安裝低光增強模組。下列程式碼會檢查模組是否已安裝:
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);
});
}
這段程式碼的重點
- 呼叫
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()
來完成這項操作。您必須自行編寫該方法。
檢查是否支援低光源增強功能
有了用戶端後,您可以檢查應用程式執行裝置是否支援低光增強功能。下列程式碼會檢查是否支援低光增強功能:
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
物件。您可以使用這個物件設定成功和失敗事件監聽器。在成功事件監聽器中建立低光源增強工作階段。