یک جلسه ARCore را در Android پیکربندی کنید

یک جلسه ARCore را برای ایجاد تجربیات AR برای برنامه خود پیکربندی کنید.

جلسه چیست؟

تمام فرآیندهای واقعیت افزوده ، مانند ردیابی حرکت، درک محیط و تخمین روشنایی، در داخل یک جلسه ARCore اتفاق می‌افتند. Session نقطه ورود اصلی به API ARCore است. این جلسه وضعیت سیستم واقعیت افزوده را مدیریت می‌کند و چرخه عمر جلسه را مدیریت می‌کند و به برنامه اجازه می‌دهد یک جلسه را ایجاد، پیکربندی، شروع یا متوقف کند. مهمتر از همه، این جلسه به برنامه امکان می‌دهد فریم‌هایی را دریافت کند که امکان دسترسی به تصویر دوربین و موقعیت دستگاه را فراهم می‌کنند.

از این جلسه می‌توان برای پیکربندی ویژگی‌های زیر استفاده کرد:

تأیید کنید که ARCore نصب و به‌روز شده است

قبل از ایجاد یک Session ، مطمئن شوید که ARCore نصب و به‌روز است. اگر ARCore نصب نشده باشد، ایجاد جلسه با شکست مواجه می‌شود و هرگونه نصب یا ارتقاء بعدی ARCore نیاز به راه‌اندازی مجدد برنامه دارد.

جاوا

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

کاتلین

// 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

جاوا

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

کاتلین

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 مقدار قابل توجهی از حافظه هیپ بومی را در اختیار دارد. عدم بستن صریح session ممکن است باعث شود برنامه شما با کمبود حافظه بومی مواجه شود و از کار بیفتد. وقتی دیگر به session AR نیازی نیست، close() را برای آزادسازی منابع فراخوانی کنید. اگر برنامه شما حاوی یک activity با قابلیت AR است، close() را از متد onDestroy() activity فراخوانی کنید.

جاوا

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

کاتلین

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

مراحل بعدی