在 Android NDK 中配置 ARCore 会话
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
配置 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);
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eARCore sessions manage all AR processes like motion tracking and environmental understanding, and are accessed via the \u003ccode\u003eArSession\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eBefore starting a session, verify ARCore is installed and updated using \u003ccode\u003eArCoreApk_checkAvailability\u003c/code\u003e to ensure smooth functionality.\u003c/p\u003e\n"],["\u003cp\u003eSessions can be configured to enable features like Depth API, Augmented Faces, or Cloud Anchors, providing flexibility for various AR experiences.\u003c/p\u003e\n"],["\u003cp\u003eTo avoid memory leaks and potential crashes, release session resources by calling \u003ccode\u003eArSession_destroy()\u003c/code\u003e when the session is no longer needed.\u003c/p\u003e\n"]]],[],null,["# Configure an ARCore session in Android NDK\n\nConfigure an ARCore session to build AR experiences for your app.\n\nWhat is a session?\n------------------\n\nAll [AR processes](/ar/discover/concepts), such as motion tracking,\nenvironmental understanding, and lighting estimation, happen inside an ARCore\nsession. [`ArSession`](/ar/reference/c/group/ar-session) is the main entry point to the ARCore\nAPI. It manages the AR system state and handles the session lifecycle, allowing\nthe app to create, configure, start, or stop a session. Most importantly, it\nenables the app to receive frames that allow access to the camera image and\ndevice pose.\n\nThe session can be used to configure the following features:\n\n- [Lighting Estimation](/ar/develop/lighting-estimation)\n- [Cloud Anchors](/ar/develop/cloud-anchors)\n- [Augmented Images](/ar/develop/augmented-images)\n- [Augmented Faces](/ar/develop/augmented-faces)\n- [Depth API](/ar/develop/depth)\n- [Instant Placement](/ar/develop/instant-placement)\n- [ARCore Geospatial API](/ar/develop/geospatial)\n\nVerify that ARCore is installed and up to date\n----------------------------------------------\n\nBefore creating an [`ArSession`](/ar/reference/c/group/ar-session), verify that ARCore is installed and up to date.\nIf ARCore isn't installed, session creation fails and any subsequent\ninstallation or upgrade of ARCore requires an app restart.\n\n\u003cbr /\u003e\n\n```c\n/*\n * Check if ARCore is currently usable, i.e. whether ARCore is supported and\n * up to date.\n */\nint32_t is_arcore_supported_and_up_to_date(void* env, void* context) {\n ArAvailability availability;\n ArCoreApk_checkAvailability(env, context, &availability);\n switch (availability) {\n case AR_AVAILABILITY_SUPPORTED_INSTALLED:\n return true;\n case AR_AVAILABILITY_SUPPORTED_APK_TOO_OLD:\n case AR_AVAILABILITY_SUPPORTED_NOT_INSTALLED: {\n ArInstallStatus install_status;\n // ArCoreApk_requestInstall is processed asynchronously.\n CHECK(ArCoreApk_requestInstall(env, context, true, &install_status) ==\n AR_SUCCESS);\n return false;\n }\n case AR_AVAILABILITY_UNSUPPORTED_DEVICE_NOT_CAPABLE:\n // This device is not supported for AR.\n return false;\n case AR_AVAILABILITY_UNKNOWN_CHECKING:\n // ARCore is checking the availability with a remote query.\n // This function should be called again after waiting 200 ms\n // to determine the query result.\n handle_check_later();\n return false;\n case AR_AVAILABILITY_UNKNOWN_ERROR:\n case AR_AVAILABILITY_UNKNOWN_TIMED_OUT:\n // There was an error checking for AR availability.\n // This may be due to the device being offline.\n // Handle the error appropriately.\n handle_unknown_error();\n return false;\n\n default: // All enum cases have been handled.\n return false;\n }\n}\n```\n\nCreate a session\n----------------\n\nCreate and configure a session in ARCore. \n\n```c\n// Create a new ARCore session.\nArSession* ar_session = NULL;\nCHECK(ArSession_create(env, context, &ar_session) == AR_SUCCESS);\n\n// Create a session config.\nArConfig* ar_config = NULL;\nArConfig_create(ar_session, &ar_config);\n\n// Do feature-specific operations here, such as enabling depth or turning on\n// support for Augmented Faces.\n\n// Configure the session.\nCHECK(ArSession_configure(ar_session, ar_config) == AR_SUCCESS);\n```\n\nClose a session\n---------------\n\n`ArSession` owns a significant amount of native heap memory. Failure to\nexplicitly close the session may cause your app to run out of native memory and\ncrash. When the AR session is no longer needed, call\n[`ArSession_destroy()`](/ar/reference/c/group/ar-session#arsession_destroy)\nto release resources. \n\n```c\n// Release memory used by the AR session.\nArSession_destroy(session);\n```\n\nNext steps\n----------\n\n- [ARCore quickstart for Android NDK](/ar/develop/c/quickstart)"]]