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