ดูวิธีใช้ Augmented Images ในแอปของคุณเอง
ข้อกำหนดเบื้องต้น
ตรวจสอบว่าคุณเข้าใจแนวคิด 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
ได้รูปภาพเคลื่อนไหว หากแอปของคุณต้องติดตามภาพเคลื่อนไหว ให้ใช้ทั้ง 2 อย่าง
TrackingState
และTrackingMethod
เพื่อพิจารณาว่ารูปภาพนั้นได้รับ และตรวจพบตำแหน่งหรือไม่
กรณีการใช้งาน | รูปภาพคงที่ | กำลังย้ายรูปภาพ |
---|---|---|
ตัวอย่าง | โปสเตอร์แขวนบนผนัง | โฆษณาด้านข้างรถเมล์ |
โพสท่าสามารถ ถือว่าถูกต้องเมื่อ |
TrackingState == TRACKING |
TrackingState == TRACKING
และ TrackingMethod == FULL_TRACKING |
ดูเพิ่มเติม
Augmented Images โปรเจ็กต์ตัวอย่าง ใน ARCore SDK