Android 適用的擴增圖片開發人員指南

瞭解如何在自己的應用程式中使用擴增圖片。

必要條件

請務必先瞭解基本 AR 概念,以及如何設定 ARCore 工作階段,然後再繼續操作。

建立圖片資料庫

每個圖片資料庫最多可儲存 1,000 張圖片的資訊。

建立 AugmentedImageDatabase 的方法有兩種:

  • 載入已儲存的圖片資料庫。然後視需要新增更多參考圖片。
  • 建立新的空白資料庫。然後逐一新增參考圖片。

載入已儲存的圖片資料庫

使用 AugmentedImageDatabase.deserialize() 載入現有的圖片資料庫:

Java

AugmentedImageDatabase imageDatabase;
try (InputStream inputStream = this.getAssets().open("example.imgdb")) {
  imageDatabase = AugmentedImageDatabase.deserialize(session, inputStream);
} catch (IOException e) {
  // The Augmented Image database could not be deserialized; handle this error appropriately.
}

Kotlin

val imageDatabase = this.assets.open("example.imgdb").use {
  AugmentedImageDatabase.deserialize(session, it)
}

您可以在開發期間使用 arcoreimg 指令列工具,或是在記憶體中載入的資料庫上呼叫 AugmentedImageDatabase.serialize() 來建立映像檔資料庫。

建立新的空白資料庫

如要在執行階段建立空白圖片資料庫,請使用 AugmentedImageDatabase 建構函式

Java

AugmentedImageDatabase imageDatabase = new AugmentedImageDatabase(session);

Kotlin

val imageDatabase = AugmentedImageDatabase(session)

將圖片新增至現有資料庫

為每個圖片呼叫 AugmentedImageDatabase.addImage(),並指定選用的 widthInMeters,將圖片加入圖片資料庫。

Java

Bitmap bitmap;
try (InputStream bitmapString = getAssets().open("dog.jpg")) {
  bitmap = BitmapFactory.decodeStream(bitmapString);
} catch (IOException e) {
  // The bitmap could not be found in assets; handle this error appropriately.
  throw new AssertionError("The bitmap could not be found in assets.", e);
}

// If the physical size of the image is not known, use addImage(String, Bitmap) instead, at the
// expense of an increased image detection time.
float imageWidthInMeters = 0.10f; // 10 cm
int dogIndex = imageDatabase.addImage("dog", bitmap, imageWidthInMeters);

Kotlin

val bitmap = assets.open("dog.jpg").use { BitmapFactory.decodeStream(it) }
// If the physical size of the image is not known, use addImage(String, Bitmap) instead, at the
// expense of an increased image detection time.
val imageWidthInMeters = 0.10f // 10 cm
val dogIndex = imageDatabase.addImage("dog", bitmap, imageWidthInMeters)

傳回的索引之後可用來識別偵測到的參考圖片。

啟用圖片追蹤

將工作階段設定設為以所需圖片資料庫設置的工作階段設定,藉此設定 ARCore 工作階段開始追蹤圖片:

Java

Config config = new Config(session);
config.setAugmentedImageDatabase(imageDatabase);
session.configure(config);

Kotlin

val config = Config(session)
config.augmentedImageDatabase = imageDatabase
session.configure(config)

練習期間,ARCore 會比對相機圖片與圖片資料庫中的特徵點,尋找圖片。

如要取得相符的圖片,請輪詢影格更新迴圈中更新的 AugmentedImage

Java

Collection<AugmentedImage> updatedAugmentedImages =
    frame.getUpdatedTrackables(AugmentedImage.class);
for (AugmentedImage img : updatedAugmentedImages) {
  if (img.getTrackingState() == TrackingState.TRACKING) {
    // Use getTrackingMethod() to determine whether the image is currently
    // being tracked by the camera.
    switch (img.getTrackingMethod()) {
      case LAST_KNOWN_POSE:
        // The planar target is currently being tracked based on its last
        // known pose.
        break;
      case FULL_TRACKING:
        // The planar target is being tracked using the current camera image.
        break;
      case NOT_TRACKING:
        // The planar target isn't been tracked.
        break;
    }

    // You can also check which image this is based on img.getName().
    if (img.getIndex() == dogIndex) {
      // TODO: Render a 3D version of a dog in front of img.getCenterPose().
    } else if (img.getIndex() == catIndex) {
      // TODO: Render a 3D version of a cat in front of img.getCenterPose().
    }
  }
}

Kotlin

val updatedAugmentedImages = frame.getUpdatedTrackables(AugmentedImage::class.java)

for (img in updatedAugmentedImages) {
  if (img.trackingState == TrackingState.TRACKING) {
    // Use getTrackingMethod() to determine whether the image is currently
    // being tracked by the camera.
    when (img.trackingMethod) {
      AugmentedImage.TrackingMethod.LAST_KNOWN_POSE -> {
        // The planar target is currently being tracked based on its last known pose.
      }
      AugmentedImage.TrackingMethod.FULL_TRACKING -> {
        // The planar target is being tracked using the current camera image.
      }
      AugmentedImage.TrackingMethod.NOT_TRACKING -> {
        // The planar target isn't been tracked.
      }
    }

    // You can also check which image this is based on AugmentedImage.getName().
    when (img.index) {
      dogIndex -> TODO("Render a 3D version of a dog at img.getCenterPose()")
      catIndex -> TODO("Render a 3D version of a cat at img.getCenterPose()")
    }
  }
}

支援不同用途

ARCore 偵測到擴增圖片時,會為該擴增圖片建立 Trackable,並將 TrackingState 設為 TRACKING,並將 TrackingMethod 設為 FULL_TRACKING。當追蹤的圖片移出相機檢視畫面時,ARCore 會將 TrackingMethod 變更為 LAST_KNOWN_POSE,同時繼續提供圖片的方向和位置。

應用程式應根據預定用途,以不同方式使用這些列舉。

  • 已修正的圖片。大部分的用途有固定位置的圖片 (意即不應移動) 都能直接使用 TrackingState 來判斷是否已偵測到圖片,以及圖片位置是否已知。您可以忽略 TrackingMethod

  • 動態圖片。如果應用程式需要追蹤移動的圖片,請同時使用 TrackingStateTrackingMethod 來判斷是否已偵測到圖片,以及圖片位置是否已知。

用途 已修正的圖片 正在移動圖片
範例 掛在牆上的海報 公車旁的廣告
如果有
TrackingState == TRACKING TrackingState == TRACKING

TrackingMethod == FULL_TRACKING

另請參閱