使用低光源增強工作階段

使用低光源增強模式,開啟或關閉 Google 低光源增強功能。

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

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")
      lowLightBoostSession = 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 lowLightBoostCallback;
}

這段程式碼的重點

  • 此程式碼定義不公開方法 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(
    lowLightBoostExecutor,
    (task) -> Log.d(TAG, "Session creation complete"))
  .addOnCanceledListener(
    lowLightBoostExecutor,
    () -> {
      throw new RuntimeException("Session creation canceled");
    });

這段程式碼的重點

  • 您可以將 LowLightBoostOptions 物件傳遞至 createSession(),以設定工作階段。這個物件會指定目標表面、要使用的相機 ID 和預覽畫面的尺寸等。
  • 這段程式碼假設您已開啟與 Camera2 相機的連線,並使用該資訊設定 cameraId, previewWidth, previewHeight 的值。詳情請參閱 Camera2 說明文件
  • enableLowLightBoost 是布林值,用於指定是否要啟用低光增強功能。
  • createLowLightBoostCallback 是您用來建立回呼物件的程式碼。在會話中斷或遭到刪除時,系統會呼叫這個物件。
  • LowLightBoostClient.createSession() 方法會傳回 Task 物件。您可以使用這個物件設定成功和失敗事件監聽器。在成功事件監聽器中擷取影片。
  • 您可以指定 Executor 來執行事件監聽器。如果您未指定 Executor,監聽器會在主執行緒上執行。在這個程式碼中,我們假設 lowLightBoostExecutor 是適當的 Executor

開始相機預覽

建立低光源工作階段後,即可開始攝影機預覽串流。您應在傳遞至低光工作階段的 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 capture 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 capture session without LLB.
}

這段程式碼的重點

釋放工作階段

當攝影機不再處於活動狀態時,請呼叫 LowLightBoostSession.release() 釋放低光增強工作階段。特別是,您應確保在活動銷毀時釋放工作階段。您可以呼叫活動的 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;
  }
}

這段程式碼的重點

  • 工作階段釋出後,您不應呼叫任何方法。您應清除指向工作階段的所有變數,如這段程式碼所示。