ARCore-Sitzung unter Android konfigurieren

Konfigurieren Sie eine ARCore-Sitzung, um AR-Erlebnisse für Ihre App zu erstellen.

Was ist eine Sitzung?

Alle AR-Prozesse wie Bewegungserkennung, Umgebungserkennung und Beleuchtungsschätzung finden innerhalb einer ARCore-Sitzung statt. Session ist der Haupteinstiegspunkt für die ARCore API. Er verwaltet den AR-Systemstatus und verwaltet den Sitzungslebenszyklus. Die Anwendung kann somit eine Sitzung erstellen, konfigurieren, starten oder beenden. Vor allem aber ermöglicht sie der App, Frames zu empfangen, die den Zugriff auf das Kamerabild und die Geräteposition ermöglichen.

In der Sitzung können die folgenden Funktionen konfiguriert werden:

Prüfen, ob ARCore installiert und auf dem neuesten Stand ist

Prüfe vor dem Erstellen eines Session, ob ARCore installiert und auf dem neuesten Stand ist. Wenn ARCore nicht installiert ist, schlägt die Sitzungserstellung fehl und jede nachfolgende Installation oder ein Upgrade von ARCore erfordert einen Neustart der App.

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

Sitzung erstellen

Erstellen und konfigurieren Sie eine Sitzung in 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)
}

Sitzung schließen

Session besitzt einen großen nativen Heap-Speicher. Wenn die Sitzung nicht explizit geschlossen wird, kann dies dazu führen, dass Ihrer Anwendung nicht mehr der native Arbeitsspeicher ausgeht und Ihre Anwendung abstürzt. Wenn die AR-Sitzung nicht mehr benötigt wird, rufen Sie close() auf, um Ressourcen freizugeben. Wenn Ihre App eine einzelne AR-fähige Aktivität enthält, rufen Sie close() über die Methode onDestroy() der Aktivität auf.

Java

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

Kotlin

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

Nächste Schritte