在 Android 中配置 ARCore 会话

配置 ARCore 会话,为您的应用打造 AR 体验。

什么是会话?

所有 AR 流程(例如动作跟踪) 环境理解和光估测都是在 ARCore 内部执行的, 会话。Session 是 ARCore 的主要入口点 API。它管理 AR 系统状态和处理会话生命周期, 应用以创建、配置、启动或停止会话。最重要的是 使应用能够接收允许访问相机图像的帧 设备姿势。

该会话可用于配置以下功能:

验证 ARCore 已安装并是最新版本

在创建 Session 之前,请确认 ARCore 已安装且是最新版本。如果 未安装 ARCore,会话创建失败,后续安装 或升级 ARCore 时都需要重启应用。

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

创建会话

在 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)
}

关闭会话

Session 拥有大量原生堆内存。未明确说明 关闭会话可能会导致应用耗尽原生内存并崩溃。时间 则无需再调用 AR 会话, 还有 close() 可释放 资源。如果您的应用只包含一个支持 AR 的 activity,请调用 close() 从 activity 的 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()

后续步骤