Konfigurowanie sesji ARCore na Androidzie

Skonfiguruj sesję ARCore, aby tworzyć doświadczenia AR w swojej aplikacji.

Co to jest sesja?

Wszystkie procesy AR, takie jak śledzenie ruchu, zrozumienie środowiska czy oszacowanie oświetlenia, odbywają się w ramach sesji ARCore. Session to główny punkt wejścia do interfejsu ARCore API. Zarządza stanem systemu AR i obsługuje cykl życia sesji, umożliwiając aplikacji tworzenie, konfigurowanie, uruchamianie i zatrzymywanie sesji. Co najważniejsze, umożliwia ona otrzymywanie przez aplikację ramek umożliwiających dostęp do zdjęcia z aparatu i pozycji urządzenia.

Sesja może służyć do konfigurowania tych funkcji:

Sprawdź, czy aplikacja ARCore jest zainstalowana i aktualna

Zanim utworzysz Session, sprawdź, czy aplikacja ARCore jest zainstalowana i aktualna. Jeśli nie masz zainstalowanej aplikacji ARCore, utworzenie sesji się nie powiedzie, a każda instalacja lub aktualizacja ARCore wymaga ponownego uruchomienia aplikacji.

Java

// Verify that ARCore is installed and using the current version.
private boolean isARCoreSupportedAndUpToDate() {
  ArCoreApk.Availability availability = ArCoreApk.getInstance().checkAvailability(this);
  switch (availability) {
    case SUPPORTED_INSTALLED:
      return true;

    case SUPPORTED_APK_TOO_OLD:
    case SUPPORTED_NOT_INSTALLED:
      try {
        // Request ARCore installation or update if needed.
        ArCoreApk.InstallStatus installStatus = ArCoreApk.getInstance().requestInstall(this, true);
        switch (installStatus) {
          case INSTALL_REQUESTED:
            Log.i(TAG, "ARCore installation requested.");
            return false;
          case INSTALLED:
            return true;
        }
      } catch (UnavailableException e) {
        Log.e(TAG, "ARCore not installed", e);
      }
      return false;

    case UNSUPPORTED_DEVICE_NOT_CAPABLE:
      // This device is not supported for AR.
      return false;

    case UNKNOWN_CHECKING:
      // ARCore is checking the availability with a remote query.
      // This function should be called again after waiting 200 ms to determine the query result.
    case UNKNOWN_ERROR:
    case UNKNOWN_TIMED_OUT:
      // There was an error checking for AR availability. This may be due to the device being offline.
      // Handle the error appropriately.
  }
}

Kotlin

// Verify that ARCore is installed and using the current version.
fun isARCoreSupportedAndUpToDate(): Boolean {
  return when (ArCoreApk.getInstance().checkAvailability(this)) {
    Availability.SUPPORTED_INSTALLED -> true
    Availability.SUPPORTED_APK_TOO_OLD, Availability.SUPPORTED_NOT_INSTALLED -> {
      try {
        // Request ARCore installation or update if needed.
        when (ArCoreApk.getInstance().requestInstall(this, true)) {
          InstallStatus.INSTALL_REQUESTED -> {
            Log.i(TAG, "ARCore installation requested.")
            false
          }
          InstallStatus.INSTALLED -> true
        }
      } catch (e: UnavailableException) {
        Log.e(TAG, "ARCore not installed", e)
        false
      }
    }

    Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE ->
      // This device is not supported for AR.
      false

    Availability.UNKNOWN_CHECKING -> {
      // ARCore is checking the availability with a remote query.
      // This function should be called again after waiting 200 ms to determine the query result.
    }
    Availability.UNKNOWN_ERROR, Availability.UNKNOWN_TIMED_OUT -> {
      // There was an error checking for AR availability. This may be due to the device being offline.
      // Handle the error appropriately.
    }
  }
}

Utwórz sesję

Utwórz i skonfiguruj sesję w ARCore.

Java

public void createSession() {
  // Create a new ARCore session.
  session = new Session(this);

  // Create a session config.
  Config config = new Config(session);

  // Do feature-specific operations here, such as enabling depth or turning on
  // support for Augmented Faces.

  // Configure the session.
  session.configure(config);
}

Kotlin

fun createSession() {
  // Create a new ARCore session.
  session = Session(this)

  // Create a session config.
  val config = Config(session)

  // Do feature-specific operations here, such as enabling depth or turning on
  // support for Augmented Faces.

  // Configure the session.
  session.configure(config)
}

Zamykanie sesji

Session posiada znaczną ilość pamięci natywnej sterty. Jeśli nie zamkniesz tej sesji, może to spowodować wyczerpanie pamięci natywnej i awarię aplikacji. Gdy sesja AR nie jest już potrzebna, wywołaj close(), aby zwolnić zasoby. Jeśli Twoja aplikacja zawiera 1 aktywność z obsługą AR, wywołaj close() za pomocą jej metody onDestroy().

Java

// Release native heap memory used by an ARCore session.
session.close();

Kotlin

// Release native heap memory used by an ARCore session.
session.close()

Dalsze kroki