Configure uma sessão do ARCore para criar experiências de RA para seu app.
O que é uma sessão?
Todos os processos de RA, como rastreamento de movimento,
compreensão ambiental e estimativa de iluminação, acontecem dentro de uma sessão
de ARCore. Session
é o ponto de entrada principal para a API ARCore. Ele gerencia o estado do sistema de RA e processa o ciclo de vida da sessão, permitindo
que o app crie, configure, inicie ou interrompa uma sessão. Mais importante ainda, ele
permite que o app receba frames que permitem acesso à imagem da câmera e
à posição do dispositivo.
A sessão pode ser usada para configurar os seguintes recursos:
- Estimativa de iluminação
- Cloud Anchors
- Imagens aumentadas
- Rostos aumentados
- API Depth
- Posicionamento instantâneo
- API ARCore Geospatial
Verificar se o ARCore está instalado e atualizado
Antes de criar um Session
, verifique se o ARCore está instalado e atualizado. Se
o ARCore não estiver instalado, a criação da sessão vai falhar e qualquer instalação
ou upgrade subsequente do ARCore exigirá uma reinicialização do app.
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.
}
}
}
Criar uma sessão
Criar e configurar uma sessão no 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)
}
Encerrar uma sessão
Session
tem uma quantidade significativa de memória de heap nativa. Não fechar explicitamente
a sessão pode fazer com que o app fique sem memória nativa e falhe. Quando
a sessão de RA não for mais necessária, chame
close()
para liberar
recursos. Se o app tiver uma única atividade ativada para RA, chame close()
do método onDestroy()
da atividade.
Java
// Release native heap memory used by an ARCore session.
session.close();
Kotlin
// Release native heap memory used by an ARCore session.
session.close()