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 擴增圖片和組合 TrackingStateTRACKINGTrackingMethodFULL_TRACKING。當追蹤的圖像從相機畫面中移出時,ARCore 變更 TrackingMethodLAST_KNOWN_POSE,同時繼續提供 該圖片

您的應用程式應根據預期用途,以不同方式使用這些列舉 確認是否屬於此情況

  • 固定圖片:大多數涉及固定圖片的用途 只要使用 TrackingState 就能判斷 是否會偵測到圖片,以及圖片的位置是否已知。 您可以忽略 TrackingMethod

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

用途 固定圖片 正在移動圖片
範例 掛在牆上的海報 公車旁的廣告
姿勢可以:
視為有效的
TrackingState == TRACKING TrackingState == TRACKING

TrackingMethod == FULL_TRACKING

另請參閱