了解如何在您自己的应用中使用增强图像。
前提条件
确保您了解 AR 基础概念 以及如何在继续之前配置 ARCore 现场录像。
创建图片数据库
每个图像数据库最多可存储 1,000 张图像的信息。
- 加载保存的图片数据库。然后,根据需要添加更多参考图像。
- 创建一个新的空数据库。然后逐一添加参考图像。
加载已保存的图片数据库
使用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 会话配置为开始跟踪图像 config 到配置了所需映像数据库的一个实例:
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
|
另请参阅
增强图像示例项目 。