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

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

ما هي الجلسة؟

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

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

التأكد من أنّ تطبيق 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);

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