Android で ARCore セッションを構成する

アプリの AR エクスペリエンスを構築する ARCore セッションを設定します。

セッションとは

モーション トラッキングなど、すべての AR プロセス 環境の理解と照明の推定は、ARCore 内で行われます。 あります。Session は ARCore へのメインのエントリ ポイントです。 APIAR システムの状態を管理し、セッションのライフサイクルを処理するため、 セッションを作成、構成、開始、停止する。最も重要なのは アプリがカメラ画像へのアクセスを可能にするフレームを受信し、 デバイスのポーズ。

このセッションを使用すると、次の機能を構成できます。

ARCore がインストールされ、最新の状態であることを確認する

Session を作成する前に、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 対応アクティビティが 1 つ含まれている場合は、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()

次のステップ