Configurare una sessione ARCore in Android

Configura una sessione ARCore per creare esperienze AR per la tua app.

Che cos'è una sessione?

Tutti i processi AR, come il rilevamento del movimento, la comprensione dell'ambiente e la stima dell'illuminazione, che avvengono all'interno di un ARCore durante la sessione. Session è il punto di accesso principale ad ARCore tramite Google Cloud CLI o tramite l'API Compute Engine. Gestisce lo stato del sistema AR e il ciclo di vita delle sessioni, l'app per creare, configurare, avviare o interrompere una sessione. Ma soprattutto, consente all'app di ricevere frame che consentono di accedere all'immagine della fotocamera e posa del dispositivo.

La sessione può essere utilizzata per configurare le seguenti funzionalità:

Verificare che ARCore sia installato e aggiornato

Prima di creare un Session, verifica che ARCore sia installato e aggiornato. Se ARCore non è installato, la creazione della sessione non riesce e qualsiasi installazione successiva o l'upgrade di ARCore richiede il riavvio dell'app.

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

Crea una sessione

Crea e configura una sessione in 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)
}

Chiudere una sessione

Session possiede una quantità significativa di memoria heap nativa. Mancata pubblicazione la chiusura della sessione potrebbe causare l'esaurimento della memoria nativa e l'arresto anomalo dell'app. Quando la sessione AR non è più necessaria, richiama close() per rilasciare Google Cloud. Se la tua app contiene una singola attività compatibile con AR, chiama il numero close() dal metodo onDestroy() dell'attività.

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

Passaggi successivi