独自のアプリで拡張画像を使用する方法を学びます。
前提条件
続行する前に、基本的な AR コンセプトと ARCore セッションを構成する方法を理解してください。
画像データベースを作成する
各画像データベースには、最大 1,000 個の画像の情報を保存できます。
AugmentedImageDatabase
を作成するには、次の 2 つの方法があります。
- 保存した画像データベースを読み込みます。必要に応じて、参照画像を追加します。
- 新しい空のデータベースを作成します。次に、参照画像を 1 つずつ追加します。
保存した画像データベースを読み込む
AugmentedImageDatabase.deserialize()
を使用して、既存の画像データベースを読み込みます。
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. }
val imageDatabase = this.assets.open("example.imgdb").use { AugmentedImageDatabase.deserialize(session, it) }
イメージ データベースは、開発中に arcoreimg
コマンドライン ツールを使用して作成できます。また、メモリに読み込まれたデータベースに対して AugmentedImageDatabase.serialize()
を呼び出して作成することもできます。
新しい空のデータベースを作成する
ランタイムで空の画像データベースを作成するには、AugmentedImageDatabase
コンストラクタを使用します。
AugmentedImageDatabase imageDatabase = new AugmentedImageDatabase(session);
val imageDatabase = AugmentedImageDatabase(session)
既存のデータベースに画像を追加する
画像データベースに画像を追加するには、各画像に対して AugmentedImageDatabase.addImage()
を呼び出し、オプションの widthInMeters
を指定します。
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);
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 セッションを構成します。
Config config = new Config(session); config.setAugmentedImageDatabase(imageDatabase); session.configure(config);
val config = Config(session) config.augmentedImageDatabase = imageDatabase session.configure(config)
セッション中、ARCore はカメラ画像の特徴点を画像データベースの特徴点と照合して画像を検索します。
一致した画像を取得するには、フレーム更新ループで更新された AugmentedImage
をポーリングします。
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(). } } }
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
は無視できます。画像の移動。アプリで動く画像を追跡する必要がある場合は、
TrackingState
とTrackingMethod
の両方を使用して、画像が検出されたかどうか、その位置が既知かどうかを判断します。
ユースケース | 固定画像 | 動画 |
---|---|---|
例 | 壁に貼られたポスター | バスの側面にある広告 |
ポーズが有効とみなされるのは、次の場合です。 |
TrackingState == TRACKING |
TrackingState == TRACKING
および TrackingMethod == FULL_TRACKING
|
関連情報
ARCore SDK の拡張画像のサンプル プロジェクト。