Android NDK で ARCore セッションを構成する

ARCore セッションを構成して、アプリの AR エクスペリエンスを構築します。

セッションとは

モーション トラッキング、環境理解、照明の推定など、すべての AR プロセスは ARCore セッション内で行われます。ArSession は、ARCore API へのメインのエントリ ポイントです。AR システムの状態を管理し、セッションのライフサイクルを処理して、アプリがセッションを作成、構成、開始、停止できるようにします。最も重要な点は、アプリがカメラ画像とデバイスのポーズにアクセスできるフレームを受信できるようになることです。

このセッションを使用して、次の機能を構成できます。

ARCore がインストールされ、最新の状態であることを確認します。

ArSession を作成する前に、ARCore がインストールされ、最新であることを確認します。ARCore がインストールされていない場合、セッションの作成に失敗し、その後のインストールまたはアップグレードでアプリの再起動が必要になります。

/*
 * Check if ARCore is currently usable, i.e. whether ARCore is supported and
 * up to date.
 */
int32_t is_arcore_supported_and_up_to_date(void* env, void* context) {
  ArAvailability availability;
  ArCoreApk_checkAvailability(env, context, &availability);
  switch (availability) {
    case AR_AVAILABILITY_SUPPORTED_INSTALLED:
      return true;
    case AR_AVAILABILITY_SUPPORTED_APK_TOO_OLD:
    case AR_AVAILABILITY_SUPPORTED_NOT_INSTALLED: {
      ArInstallStatus install_status;
      // ArCoreApk_requestInstall is processed asynchronously.
      CHECK(ArCoreApk_requestInstall(env, context, true, &install_status) ==
            AR_SUCCESS);
      return false;
    }
    case AR_AVAILABILITY_UNSUPPORTED_DEVICE_NOT_CAPABLE:
      // This device is not supported for AR.
      return false;
    case AR_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.
      handle_check_later();
      return false;
    case AR_AVAILABILITY_UNKNOWN_ERROR:
    case AR_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.
      handle_unknown_error();
      return false;

    default:  // All enum cases have been handled.
      return false;
  }
}

セッションを作成する

ARCore でセッションを作成して設定します。

// Create a new ARCore session.
ArSession* ar_session = NULL;
CHECK(ArSession_create(env, context, &ar_session) == AR_SUCCESS);

// Create a session config.
ArConfig* ar_config = NULL;
ArConfig_create(ar_session, &ar_config);

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

// Configure the session.
CHECK(ArSession_configure(ar_session, ar_config) == AR_SUCCESS);

セッションを終了する

ArSession は大量のネイティブ ヒープメモリを所有しています。セッションを明示的に閉じないと、アプリがネイティブ メモリを使い果たしてクラッシュする可能性があります。AR セッションが不要になった場合は、ArSession_destroy() を呼び出してリソースを解放します。

// Release memory used by the AR session.
ArSession_destroy(session);

次のステップ