了解如何在您自己的应用中使用增强图像。
前提条件
确保您了解基本 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
。动态图片。如果您的应用需要跟踪移动图像,请使用
TrackingState
和TrackingMethod
来确定是否已检测到图像以及其位置是否已知。
使用场景 | 已修正的图片 | 正在移动图片 |
---|---|---|
示例 | 墙上挂着一张海报 | 公交车车厢上的广告 |
在以下情况下,姿态可以 视为有效: |
TrackingState == TRACKING |
TrackingState == TRACKING
和 TrackingMethod == FULL_TRACKING
|
另请参阅
ARCore SDK 中的增强图像示例项目。