กำหนดค่าเซสชัน ARCore ใน Android

กำหนดค่าเซสชัน ARCore เพื่อสร้างประสบการณ์ AR สำหรับแอปของคุณ

เซสชันคืออะไร

กระบวนการ AR ทั้งหมด เช่น การติดตามการเคลื่อนไหว ความเข้าใจด้านสิ่งแวดล้อมและการประมาณแสงจะเกิดขึ้นภายใน ARCore เซสชัน Session เป็นจุดแรกเข้าหลักไปยัง ARCore API จะช่วยจัดการสถานะของระบบ AR และจัดการวงจรเซสชัน แอปในการสร้าง กำหนดค่า เริ่ม หรือหยุดเซสชัน ที่สำคัญที่สุดคือ ทำให้แอปได้รับเฟรมที่อนุญาตการเข้าถึงรูปภาพจากกล้อง ท่าทางของอุปกรณ์

ใช้เซสชันเพื่อกำหนดค่าฟีเจอร์ต่อไปนี้ได้

ตรวจสอบว่าติดตั้ง ARCore แล้วและเป็นเวอร์ชันล่าสุด

ก่อนสร้าง Session โปรดยืนยันว่าติดตั้ง ARCore เป็นเวอร์ชันล่าสุดแล้ว ถ้า ไม่ได้ติดตั้ง ARCore สร้างเซสชันไม่สำเร็จและมีการติดตั้งหลังจากนั้น หรือการอัปเกรด ARCore ต้องรีสตาร์ทแอป

JavaKotlin
// 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.
 
}
}
// 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

JavaKotlin
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);
}
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() จะเปิดตัว ที่ไม่ซับซ้อน หากแอปมีกิจกรรมที่เปิดใช้ AR กิจกรรมเดียว ให้โทรหา close() จากเมธอด onDestroy() ของกิจกรรม

JavaKotlin
// Release native heap memory used by an ARCore session.
session
.close();
// Release native heap memory used by an ARCore session.
session
.close()

ขั้นตอนถัดไป