設定 ARCore 工作階段,為您的應用程式打造 AR 體驗。
什麼是工作階段?
所有 AR 程序,例如動作追蹤
透過 ARCore 技術瞭解環境知識,以及估算亮度
會很有幫助Session
是 ARCore 的主要進入點
也能使用 Google Cloud CLI 或
Compute Engine API可管理 AR 系統狀態並處理工作階段生命週期
應用程式,建立、設定、開始或停止工作階段。最重要的是
應用程式即可接收允許存取相機圖片的影格
姿勢。
這個工作階段可用於設定下列功能:
確認 ARCore 已安裝且為最新版本
建立 Session
前,請確認 ARCore 已安裝且為最新版本。如果
未安裝 ARCore,工作階段建立失敗,且後續安裝也失敗
或升級 ARCore。
Java
// Verify that ARCore is installed and using the current version.
private boolean isARCoreSupportedAndUpToDate() {
ArCoreApk.Availability availability = ArCoreApk.getInstance().checkAvailability(this);
switch (availability) {
case SUPPORTED_INSTALLED:
return true;
case SUPPORTED_APK_TOO_OLD:
case SUPPORTED_NOT_INSTALLED:
try {
// Request ARCore installation or update if needed.
ArCoreApk.InstallStatus installStatus = ArCoreApk.getInstance().requestInstall(this, true);
switch (installStatus) {
case INSTALL_REQUESTED:
Log.i(TAG, "ARCore installation requested.");
return false;
case INSTALLED:
return true;
}
} catch (UnavailableException e) {
Log.e(TAG, "ARCore not installed", e);
}
return false;
case UNSUPPORTED_DEVICE_NOT_CAPABLE:
// This device is not supported for AR.
return false;
case 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.
case UNKNOWN_ERROR:
case UNKNOWN_TIMED_OUT:
// There was an error checking for AR availability. This may be due to the device being offline.
// Handle the error appropriately.
}
}
Kotlin
// Verify that ARCore is installed and using the current version.
fun isARCoreSupportedAndUpToDate(): Boolean {
return when (ArCoreApk.getInstance().checkAvailability(this)) {
Availability.SUPPORTED_INSTALLED -> true
Availability.SUPPORTED_APK_TOO_OLD, Availability.SUPPORTED_NOT_INSTALLED -> {
try {
// Request ARCore installation or update if needed.
when (ArCoreApk.getInstance().requestInstall(this, true)) {
InstallStatus.INSTALL_REQUESTED -> {
Log.i(TAG, "ARCore installation requested.")
false
}
InstallStatus.INSTALLED -> true
}
} catch (e: UnavailableException) {
Log.e(TAG, "ARCore not installed", e)
false
}
}
Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE ->
// This device is not supported for AR.
false
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.
}
Availability.UNKNOWN_ERROR, 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.
}
}
}
建立工作階段
在 ARCore 中建立並設定工作階段。
Java
public void createSession() {
// Create a new ARCore session.
session = new Session(this);
// Create a session config.
Config config = new Config(session);
// Do feature-specific operations here, such as enabling depth or turning on
// support for Augmented Faces.
// Configure the session.
session.configure(config);
}
Kotlin
fun createSession() {
// Create a new ARCore session.
session = Session(this)
// Create a session config.
val config = Config(session)
// Do feature-specific operations here, such as enabling depth or turning on
// support for Augmented Faces.
// Configure the session.
session.configure(config)
}
關閉工作階段
Session
擁有大量的原生堆積記憶體。未明確指定
關閉工作階段可能會導致應用程式超出原生記憶體容量,因而當機。時間
不再需要 AR 工作階段,請呼叫
將於 close()
釋出
再複習一下,機構節點
是所有 Google Cloud Platform 資源的根節點如果應用程式含有單一支援 AR 的活動,請呼叫 close()
來自活動的 onDestroy()
方法
Java
// Release native heap memory used by an ARCore session.
session.close();
Kotlin
// Release native heap memory used by an ARCore session.
session.close()