Augmented Reality Android App Tutorial – Programming Examples From Start To Finish

Augmented Reality Android App Tutorial – Programming Examples From Start To Finish.

Augmented reality (AR) is a technology that allows you to overlay digital objects on top of the real world, using the camera and sensors on a mobile device. AR has become increasingly popular in recent years and has many applications, including gaming, education, marketing, and more. In this tutorial, we will explore how to develop an AR application for Android devices using the ARCore library.

Augmented Reality Android App Tutorial
Augmented Reality Android App Tutorial

Prerequisites To develop an AR application for Android, you will need the following:

  1. Android Studio: This is the official IDE for Android development, and it includes all the tools you need to build an AR application.
  2. ARCore: This is a library developed by Google that enables AR on Android devices. ARCore provides APIs for motion tracking, environmental understanding, and light estimation.
  3. A compatible Android device: ARCore requires a device that supports AR. You can check the list of compatible devices on the ARCore website.

Getting Started. To get started, open Android Studio and create a new project. Choose “Empty Activity” as the project template, and give your project a name.

Next, add the ARCore library to your project. To do this, open the build.gradle file for your app module, and add the following lines to the dependencies block:

python
implementation 'com.google.ar:core:1.25.0'

This will add the ARCore library to your project. Next, we will add the ARCore permissions to our app. Open the AndroidManifest.xml file and add the following permissions:

php
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.ar" android:required="true" />

These permissions allow your app to access the camera and use ARCore.

Creating an AR Scene Now that we have set up our project and added the necessary dependencies and permissions, let’s create an AR scene. An AR scene is a virtual representation of the real world, which is created by the ARCore library.

To create an AR scene, we need to add a surface to the scene. This is done by adding an ARCore session to our app. The session is responsible for tracking the user’s movements and mapping the environment.

To add an ARCore session, we need to create an instance of the ArFragment class, which is provided by the ARCore library. The ArFragment class is a pre-built fragment that provides a surface for AR content.

Add the following code to your activity’s layout XML file to include the ArFragment:

python
<fragment
android:name="com.google.ar.sceneform.ux.ArFragment"
android:id="@+id/ar_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Next, we need to create a reference to the ArFragment in our activity:

scss
ArFragment arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ar_fragment);

We can now use the ArFragment to create an AR scene. To create the scene, we will add a 3D object to the scene. ARCore provides a library called Sceneform, which makes it easy to add 3D objects to an AR scene.

Adding a 3D Object to the Scene To add a 3D object to the scene, we will create a new Java class called “AugmentedImageNode.” This class will be responsible for creating and rendering the 3D object.

First, we need to create a reference to the image that we want to track in the AR scene. For this tutorial, we will use a sample image provided by Google.

Create a new directory in your project called “assets,” and add the sample image to this directory. Then, create a new Java class called “AugmentedImageNode” and add the following code:

java
public class AugmentedImageNode extends AnchorNode {
private static final String TAG = "AugmentedImageNode";
private AugmentedImage augmentedImage;
private Node node;
public AugmentedImageNode(Context context) {
node = new Node();
MaterialFactory.makeOpaqueWithColor(context, new Color(android.graphics.Color.RED))
.thenAccept(material -> {
ModelRenderable cubeRenderable = ShapeFactory.makeCube(new Vector3(0.1f, 0.1f, 0.1f), new Vector3(0.0f, 0.05f, 0.0f), material);
node.setRenderable(cubeRenderable);
});
this.addChild(node);
}public void setImage(AugmentedImage image) {
this.augmentedImage = image;
}public AugmentedImage getImage() {
return augmentedImage;
}

public Node getNode() {
return node;
}
}

This class creates a red cube and adds it to the scene as a child of an anchor node. The anchor node is a point in the real world that the ARCore library tracks. When the anchor node moves, the 3D object moves with it.

Next, we need to modify our ArFragment class to track the image and add the 3D object to the scene. Add the following code to your ArFragment:

scss
@Override
public void onAttach(Context context) {
super.onAttach(context);
AugmentedImageDatabase augmentedImageDatabase = new AugmentedImageDatabase(session);
Bitmap bitmap = loadAugmentedImageBitmap(context);
augmentedImageDatabase.addImage(“image_name”, bitmap);config.setAugmentedImageDatabase(augmentedImageDatabase);
session.configure(config);
}private Bitmap loadAugmentedImageBitmap(Context context) {
AssetManager assetManager = context.getAssets();
InputStream inputStream;
try {
inputStream = assetManager.open(“sample_image.jpg”);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return BitmapFactory.decodeStream(inputStream);
}

@Override
public void onUpdate(FrameTime frameTime) {
Frame frame = arFragment.getArSceneView().getArFrame();
Collection<AugmentedImage> augmentedImages = frame.getUpdatedTrackables(AugmentedImage.class);
for (AugmentedImage augmentedImage : augmentedImages) {
if (augmentedImage.getTrackingState() == TrackingState.TRACKING) {
if (augmentedImage.getName().equals(“image_name”)) {
if (!augmentedImageMap.containsKey(augmentedImage)) {
AugmentedImageNode node = new AugmentedImageNode(getContext());
node.setImage(augmentedImage);
augmentedImageMap.put(augmentedImage, node);
arFragment.getArSceneView().getScene().addChild(node);
}
}
} else if (augmentedImage.getTrackingState() == TrackingState.STOPPED) {
augmentedImageMap.remove(augmentedImage);
}
}
}

This code loads the sample image from the assets directory, creates an augmented image database, and adds the image to the database. It then configures the AR session with the database.

In the onUpdate method, the code gets the current frame from the AR session and checks for any updated augmented images. If an augmented image is detected and it matches the image in the database, a new AugmentedImageNode is created and added to the scene.

Running the Application Now that we have created an AR scene, we can run the application on a compatible Android device. Connect your device to your computer and click the “Run” button in Android Studio.

Thanks For Reading This Post On “Augmented Reality Android App Tutorial.”

Leave a Reply

Your email address will not be published. Required fields are marked *