Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To track multiple images using ARCore in Android Studio, you can follow these steps:

  1. Add ARCore dependencies to your project by adding the following lines in your app's build.gradle file:
implementation 'com.google.ar:core:1.18.0'
implementation 'com.google.ar.sceneform:core:1.18.0'
  1. Create an ARCore session by calling the ARCore's Session constructor and passing the current activity context as an argument.

  2. 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()
  1. Once a session is created, you can start the image tracking process by calling the ARCore session's 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)
  1. After the session is configured, you can start the ARCore's frame and image tracking using the onDrawFrame method of the GLSurfaceView object.

  2. 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()
  1. 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.

  2. 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.