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 지원 활동이 하나뿐인 경우 활동의 onDestroy() 메서드에서 close()를 호출합니다.

Java

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

Kotlin

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

다음 단계