הגדרת סשן 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 יש כמות משמעותית של זיכרון ערימה (heap) מקורי. הפעולה נכשלה סגירה מפורשת של הסשן עלולה לגרום לכך שנגמר הזיכרון המקורי של האפליקציה לקרוס. כשאין יותר צורך בסשן AR, ArSession_destroy() כדי לשחרר משאבים.

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

השלבים הבאים