הגדרת סשן ARCore ב-Android NDK

צריך להגדיר סשן ב-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);

השלבים הבאים