Düşük ışık güçlendirme oturumu kullanma

Google Düşük Işık Güçlendirme özelliğini açmak ve kapatmak için düşük ışık güçlendirme oturumu kullanın.

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 Hizmetleri com.google.android.gms.cameralowlight paketi tarafından sağlanır. Google Play Hizmetleri API'lerine erişme hakkında bilgi edinmek için Google Play Hizmetleri belgelerine bakın.

Geri çağırma nesnesi oluşturma

Düşük ışıkta artırma oturumunu oluşturduğunuzda, LowLightBoostCallback arayüzünü uygulayan bir nesne iletmeniz gerekir. Bu nesnenin işlevleri, oturumun bağlantısı kesildiğinde veya nesnenin kendisi yok edildiğinde çağrılır. Aşağıdaki kodda geri çağırma işlevinin nasıl oluşturulacağı gösterilmektedir:

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

Bu kodla ilgili önemli noktalar

  • Bu kod, geri çağırma nesnesini oluşturan createLowLightBoostCallback() adlı gizli bir yöntem tanımlar. Oturum oluşturma bölümünde açıklandığı gibi, düşük ışıkta artırma oturumunu gerçekten oluşturduğunuzda bu yöntemi çağırırsınız.
  • Geri çağırma işlevi, oturumun bağlantısı kesildiğinde veya oturum sona erdiğinde çağrılır. Oturum oluşturulduğunda çağrılmaz. Oturumun başarıyla oluşturulup oluşturulmadığını kontrol etmek için LowLightBoostClient.createSession tarafından döndürülen Task nesnesini inceleyin.

Oturum oluşturma

Düşük ışıklı oturum oluşturmak için LowLightBoostClient.createSession yöntemini çağırın.

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

Bu kodla ilgili önemli noktalar

  • Oturumu yapılandırmak için createSession()'a bir LowLightBoostOptions nesnesi iletebilirsiniz. Bu nesne, hedef yüzey, kullanılacak kameranın kimliği ve önizlemenin boyutları gibi bilgileri belirtir.
  • Bu kod, bir Camera2 kamerasına bağlantı açtığınız ve cameraId, previewWidth, previewHeight değerlerini ayarlamak için bu bilgileri kullandığınız varsayılır. Daha fazla bilgi için Camera2 belgelerine göz atın.
  • enableLowLightBoost, düşük ışıkta artırma özelliğinin etkinleştirilip etkinleştirilmeyeceğini belirten bir boole değeridir.
  • createLowLightBoostCallback, geri çağırma nesnesini oluşturmak için yazdığınız bir yöntemdir. Bu nesne, oturumun bağlantısı kesildiğinde veya nesne yok edildiğinde çağrılır.
  • LowLightBoostClient.createSession() yöntemi bir Task nesnesi döndürür. Başarı ve başarısızlık dinleyicilerini ayarlamak için bu nesneyi kullanırsınız. Videoyu başarı dinleyicisinin içine kaydedin.
  • Dinleyicileri çalıştırmak için bir Executor belirtebilirsiniz. Executor belirtmezseniz dinleyiciler ana iş parçacığında çalışır. Bu kodda, lowLightBoostExecutor'in uygun bir Executor olduğunu varsayıyoruz.

Kamera önizlemesini başlatma

Düşük ışıklı oturum oluşturduktan sonra kamera önizleme akışını başlatabilirsiniz. Bunu, Oturum oluşturma bölümünde açıklandığı gibi, düşük ışıklı oturuma ilettiğiniz onSuccess() geri arama işlevinde yapmanız gerekir. Aşağıdaki kodda, videonun nasıl çekileceği gösterilmektedir:

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

Bu kodla ilgili önemli noktalar

  • lowLightBoostSession, Oturum oluştur bölümünde oluşturduğunuz oturumdur.
  • setSceneDetectorCallback(), SceneDetectorCallback arayüzünü uygulayan bir geri çağırma nesnesi tanımlar. Oturum, sahne parlaklığı değiştiğinde söz konusu nesnenin onSceneBrightnessChanged() yöntemini çağırır. Uygulamanız, kameranın kullanıcı arayüzünü uygun şekilde ayarlamalıdır.
  • Geri çağırma işlevini çalıştırmak için bir Executor belirtebilirsiniz. Executor belirtmezseniz geri çağırma işlevi ana mesaj dizisinde çalışır. Bu kodda, lowLightBoostExecutor'in uygun bir Executor olduğunu varsayıyoruz.
  • lowLightBoostSession.getCameraSurface(), yakalanan videoyla birlikte Surface döndürür.

Oturumdan çıkma

Kamera artık etkin olmadığında LowLightBoostSession.release() komutunu göndererek düşük ışıkta artırma oturumunu sonlandırın. Özellikle, etkinliğiniz yok edildiğinde oturumu serbest bıraktığınızdan emin olmanız gerekir. Bunu, etkinliğinizin onDestroy() yöntemindeki yöntemi çağırarak yapabilirsiniz:

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

Bu kodla ilgili önemli noktalar

  • Oturum serbest bırakıldıktan sonra oturumun hiçbir yöntemini çağırmamanız gerekir. Bu kodda olduğu gibi, oturumu işaret eden tüm değişkenleri temizlemeniz gerekir.