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

Google Zayıf Işık Güçlendirme'yi açıp 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 arama nesnesi oluşturma

Düşük ışıkta iyileştirme oturumu oluşturduğunuzda, oturuma LowLightBoostCallback arayüzünü uygulayan bir nesne iletmeniz gerekir. Bu nesnenin işlevleri, oturumun bağlantısı kesildiğinde veya oturum 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ı özel bir yöntemi tanımlar. Oturum oluşturma bölümünde açıklandığı gibi, düşük ışıkta çekim oturumunu oluşturduğunuzda bu yöntemi çağırırsınız.
  • Geri çağırma, oturumun bağlantısı kesildiğinde veya oturum yok edildiğinde çağrılır. Oturum oluşturulduğunda bu işlev ç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

Zayıf ışık oturumu 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() işlevine bir LowLightBoostOptions nesnesi iletirsiniz. Bu nesne, hedef yüzey, kullanılacak kameranın kimliği ve önizlemenin boyutları gibi şeyleri belirtir.
  • Bu kod, Camera2 kamerasına bağlantı açtığınızı ve bu bilgileri cameraId, previewWidth, previewHeight değerlerini ayarlamak için kullandığınızı varsayar. Daha fazla bilgi için Camera2 belgelerini inceleyin.
  • enableLowLightBoost, düşük ışıkta iyileştirme özelliğinin başlatılıp başlatılmayacağını 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 oturum yok edildiğinde çağrılır.
  • LowLightBoostClient.createSession() yöntemi, Task nesnesini döndürür. Bu nesneyi, başarı ve hata dinleyicilerini ayarlamak için kullanırsınız. Videoyu başarı dinleyicisi içinde yakalayın.
  • Dinleyicileri çalıştırmak için bir Executor belirtebilirsiniz. Bir Executor belirtmezseniz dinleyiciler ana ileti dizisinde çalışır. Bu kodda, lowLightBoostExecutor öğesinin uygun bir Executor olduğunu varsayıyoruz.

Yakalama sonuçlarını iletme

Google Düşük Işık Güçlendirme, uygulanacak doğru aydınlatma miktarını belirlemek için belirli kamera meta verilerine ihtiyaç duyar. TotalCaptureResult değerini processCaptureResult() yöntemine iletmeniz gerekir. TotalCaptureResult değerini onCaptureCompleted() geri çağırma yönteminde alabilirsiniz.

Kotlin

  val captureCallback = CameraCaptureSession.CaptureCallback() {
    override fun onCaptureCompleted(
      session: CameraCaptureSession,
      request: CaptureRequest,
      result: TotalCaptureResult
    ) {
      super.onCaptureCompleted(session, request, result)
      lowLightBoostSession?.processCaptureResult(result)
    }
  }

Java

  CameraCaptureSession.CaptureCallback captureCallback =
    new CameraCaptureSession.CaptureCallback() {
      @Override
      public void onCaptureCompleted(
        @NonNull CameraCaptureSession session,
        @NonNull CaptureRequest request,
        @NonNull TotalCaptureResult result) {
          super.onCaptureCompleted(session, request, result)
          if (lowLightBoostSession != null) {
            lowLightBoostSession.processCaptureResult(result);
          }
        }
      };

Bu kodla ilgili önemli noktalar

  • Bu kod yalnızca Google LLB ile alakalı CaptureCallback kodunu gösterir. Bu geri çağırmalarda başka kodlarınız da olabilir.
  • TotalCaptureResult değerinin iletilmesi, Google LLB'nin otomatik pozlama verilerini ve düşük ışık artırma için gerekli olan diğer meta verileri analiz etmesine olanak tanır. Bu sayede sahne algılama işlenir ve kareye ne kadar artırma uygulanacağı belirlenir.
  • Kamera oturumunu oluştururken captureCallback nesnesini iletmeniz gerekir. Örneğin, `setSingleRepeatingRequest()` ile.

Kamera önizlemesini başlatma

Zayıf ışık oturumu oluşturduktan sonra kamera önizleme akışını başlatabilirsiniz. Bunu, Oturum oluşturma bölümünde açıklandığı gibi, düşük ışık oturumuna ilettiğiniz onSuccess() geri çağırma işlevi içinde yapmanız gerekir. Aşağıdaki kodda videonun nasıl kaydedileceğ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şturma bölümünde oluşturduğunuz oturumdur.
  • setSceneDetectorCallback(), SceneDetectorCallback arayüzünü uygulayan bir geri çağırma nesnesini tanımlar. Oturum, sahne parlaklığı değiştiğinde bu nesnenin onSceneBrightnessChanged() yöntemini çağırır. Uygulamanız, kameranın kullanıcı arayüzünü uygun şekilde ayarlamalıdır.
  • Geri çağırmayı çalıştırmak için bir Executor belirtebilirsiniz. Bir Executor belirtmezseniz geri çağırma işlemi ana iş parçacığında çalışır. Bu kodda, lowLightBoostExecutor öğesinin uygun bir Executor olduğunu varsayıyoruz.
  • lowLightBoostSession.getCameraSurface(), yakalanan videoyla birlikte Surface değerini döndürür.

Oturumu serbest bırakma

Kamera artık etkin olmadığında LowLightBoostSession.release() numaralı telefonu arayarak düşük ışık artırma oturumunu serbest bırakın. Özellikle, etkinliğiniz yok edildiğinde oturumu serbest bıraktığınızdan emin olmanız gerekir. Bunu, etkinliğinizin onDestroy() yönteminde 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 yayınlandıktan sonra yöntemlerinden hiçbirini çağırmamalısınız. Bu kodda olduğu gibi, oturumu işaret eden tüm değişkenleri temizlemeniz gerekir.