To track multiple images using ARCore in Android Studio, you can follow these steps:
implementation 'com.google.ar:core:1.18.0'
implementation 'com.google.ar.sceneform:core:1.18.0'
Create an ARCore session by calling the ARCore's Session constructor and passing the current activity context as an argument.
Define a list of image targets that you want to track using ARCore. You can create an image database by adding the images in the assets folder and create an instance of ImageDatabase using the builder. For example,
val imageDatabase =
ImageDatabase.builder(context)
.addReferenceImage("image_name_1.jpg")
.addReferenceImage("image_name_2.jpg")
.addReferenceImage("image_name_3.jpg")
.build()
configure()
method and passing the Config
object as the argument.
The Config
object can be used to set up the image tracking settings such as the detection mode and the image database to be used.In the Config object, set the image database to be used with the setAugmentedImageDatabase() method.
val config = Config(session)
.apply {
augmentedImageDatabase = imageDatabase
}
session.configure(config)
After the session is configured, you can start the ARCore's frame and image tracking using the onDrawFrame
method of the GLSurfaceView object.
In the onDrawFrame
method, you can call the session's update()
method to update the image tracking state for each frame.
val arFrame = session.update()
Iterate over the augmented images detected in the current frame using the arFrame.getUpdatedTrackables()
method.
This method returns a list of all the trackable images detected in the frame along with their tracking state.
For each detected image, you can check its tracking state and render any augmented content accordingly.
for (trackable in arFrame.getUpdatedTrackables(AugmentedImage::class.java)) {
if (trackable.trackingState == TrackingState.TRACKING) {
// Render the augmented content
}
}
Following these steps, you can track multiple images using ARCore in Android Studio.
Asked: 2023-06-17 07:41:42 +0000
Seen: 13 times
Last updated: Jun 17 '23