ضبط جلسة ARCore في Android NDK

يمكنك ضبط جلسة ARCore لإنشاء تجارب الواقع المعزّز لتطبيقك.

ما هي الجلسة؟

كل عمليات الواقع المعزّز، مثل تتبُّع الحركة فهم البيئة وتقدير الإضاءة داخل ARCore جلسة المراجعة. ArSession هي نقطة الدخول الرئيسية إلى ARCore واجهة برمجة التطبيقات. فهو يدير حالة نظام الواقع المعزّز ويتعامل مع مراحل نشاط الجلسة، لإنشاء جلسة أو إعدادها أو بدؤها أو إيقافها. والأهم من ذلك، يمكّن التطبيق من تلقي الإطارات التي تسمح بالوصول إلى صورة الكاميرا وضع الجهاز.

يمكن استخدام الجلسة لضبط الميزات التالية:

التأكُّد من أنّ تطبيق 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 قدرًا كبيرًا من الذاكرة الأصلية لأجزاء من الذاكرة. تعذّر قد يؤدي إغلاق الجلسة بشكل صريح إلى نفاد الذاكرة الأصلية في التطبيق والأعطال. عندما لا تكون هناك حاجة إلى جلسة الواقع المعزّز، اتصل ArSession_destroy() لإصدار الموارد.

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

الخطوات التالية