如要使用 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
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(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()
完成。您必須自行撰寫該方法。