Android NDK에서 ARCore 세션 구성

ARCore 세션을 구성하여 앱의 AR 환경을 빌드합니다.

세션이란 무엇인가요?

모션 추적, 환경 이해, 광원 추정과 같은 모든 AR 프로세스는 ARCore 세션 내에서 발생합니다. ArSession는 ARCore API의 기본 진입점입니다. AR 시스템 상태를 관리하고 세션 수명 주기를 처리하여 앱이 세션을 생성, 구성, 시작 또는 중지할 수 있도록 합니다. 가장 중요한 점은 앱이 카메라 이미지 및 기기 포즈에 액세스할 수 있는 프레임을 수신할 수 있다는 것입니다.

이 세션은 다음 기능을 구성하는 데 사용할 수 있습니다.

ARCore가 설치되어 있고 최신 상태인지 확인

ArSession를 만들기 전에 ARCore가 설치되어 있고 최신 상태인지 확인합니다. 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);

다음 단계