Định cấu hình một phiên ARCore để tạo trải nghiệm thực tế tăng cường cho ứng dụng của bạn.
Phiên là gì?
Tất cả các quy trình AR, chẳng hạn như theo dõi chuyển động, tìm hiểu môi trường và ước lượng ánh sáng, đều diễn ra trong một phiên ARCore. Session là điểm truy cập chính vào ARCore API. Lớp này quản lý trạng thái hệ thống thực tế tăng cường và xử lý vòng đời của phiên, cho phép ứng dụng tạo, định cấu hình, bắt đầu hoặc dừng một phiên. Quan trọng nhất là tính năng này cho phép ứng dụng nhận các khung hình để truy cập vào hình ảnh camera và tư thế của thiết bị.
Bạn có thể dùng phiên này để định cấu hình các tính năng sau:
- Ước tính ánh sáng
- Neối neo trên đám mây
- Hình ảnh tăng cường
- Khuôn mặt được tăng cường
- Depth API
- Vị trí đặt tức thì
- ARCore Geospatial API
Kiểm tra để đảm bảo bạn đã cài đặt ARCore và đây là phiên bản mới nhất
Trước khi tạo Session, hãy xác minh rằng ARCore đã được cài đặt và là phiên bản mới nhất. Nếu ARCore chưa được cài đặt, quá trình tạo phiên sẽ không thành công và mọi hoạt động cài đặt hoặc nâng cấp ARCore sau đó đều yêu cầu khởi động lại ứng dụng.
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.
}
}
}
Tạo một phiên
Tạo và định cấu hình một phiên trong 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)
}
Đóng một phiên
Session sở hữu một lượng đáng kể bộ nhớ vùng nhớ khối xếp gốc. Nếu không đóng phiên một cách rõ ràng, ứng dụng của bạn có thể hết bộ nhớ gốc và gặp sự cố. Khi không cần phiên thực tế tăng cường nữa, hãy gọi close() để giải phóng tài nguyên. Nếu ứng dụng của bạn chứa một hoạt động duy nhất có hỗ trợ AR, hãy gọi close() từ phương thức onDestroy() của hoạt động.
Java
// Release native heap memory used by an ARCore session.
session.close();
Kotlin
// Release native heap memory used by an ARCore session.
session.close()