Augmented Reality In Android

Posted By : Aman Srivastava | 07-Jan-2021

Arcore is the android SDK to use augmented reality within android mobile applications.ARCore SDKs make AR features available on ARCore supported devices that have Google Play Services for AR (ARCore) installed. 

 

To enable ArCore you need to perform the following steps : 

  1. Add Ar entries to the manifest file.
  2. Add dependencies to build.gradle.
  3. Implementation.

 

Add To Manifest - 

You need to add the following permission and metadata to your AndroidManifest.xml file for ARCore configuration in your application:


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

 

And inside application tag - 


<meta-data
            android:name="com.google.ar.core"
            android:value="required" />

 

Also Read: Unlocking the potential of Immersive VR using Unity

 

Add Dependencies

 

In the project's build.Gradle : 


classpath 'com.google.ar.sceneform:plugin:1.0.1'
 

In app's build.Gradle : 


implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.0.0'

In app's Gradle file : 


apply plugin: 'com.google.ar.sceneform.plugin'


 

Implementation

 

  • Add models(OBJ and GLTX extension) into project : 

3D models to render must have either .obj or .gltx extension, you can download the sample models from https://poly.google.com/

Create sampledata directory in your project and add .obj and .mtl downloaded files.

Then right-click on the obj file and choose import as sceneform assets.

As a result, the Renderable AR sceneform assets gets automatically created. Also the following lines at the end of the app’s build.gradle will be automatically added:


sceneform.asset('sampledata/Coffee Cup_final.obj',
        'default',
        'sampledata/Coffee Cup_final.sfa',
        'src/main/assets/Coffee Cup_final')

 

  • Add ArFragment into your XML file : 

<fragment android:id="@+id/sceneForm" android:name="com.google.ar.sceneform.ux.ArFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />

  • Initialize ArFragment into MainActivity.java file : 

arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.sceneForm);

  • Create a method addObject() to get hit points on the plane using motion tracking :
private void addObject(Uri model) {
    Frame frame = fragment.getArSceneView().getArFrame();
    android.graphics.Point pt = getScreenCenter();
    List<HitResult> hits;
    if (frame != null) {
        hits = frame.hitTest(pt.x, pt.y);
        for (HitResult hit : hits) {
            Trackable trackable = hit.getTrackable();
            if (trackable instanceof Plane &&
                    ((Plane) trackable).isPoseInPolygon(hit.getHitPose())) {
                modelLoader.loadModel(hit.createAnchor(), model);
                break;

            }
        }
    }
}

The call of addObject() will be from the onCreate method with Uri of the model as a param :

addObject(Uri.parse("Mesh_Zebra.sfb"));

 

  • Now once the plane is detected we need to load the model, so let's create the class ModelLoader with a method loadModel() inside it : 
public class ModelLoader {
    private final WeakReference<MainActivity> owner;
    private static final String TAG = "ModelLoader";

    ModelLoader(WeakReference<MainActivity> owner) {
        this.owner = owner;
    }

    void loadModel(Anchor anchor, Uri uri) {
        if (owner.get() == null) {
            Log.d(TAG, "Activity is null.  Cannot load model.");
            return;
        }
        ModelRenderable.builder()
                .setSource(owner.get(), uri)
                .build()
                .handle((renderable, throwable) -> {
                    MainActivity activity = owner.get();
                    if (activity == null) {
                        return null;
                    } else if (throwable != null) {
                        activity.onException(throwable);
                    } else {
                        activity.addNodeToScene(anchor, renderable);
                    }
                    return null;
                });

        return;
    }
}
  • Now as soon as the model is loaded we need to place the model to the scene, so let's make a method for that : 
public void addNodeToScene(Anchor anchor, ModelRenderable renderable) {
    AnchorNode anchorNode = new AnchorNode(anchor);
    TransformableNode node = new TransformableNode(fragment.getTransformationSystem());
    node.setRenderable(renderable);
    node.setParent(anchorNode);
    fragment.getArSceneView().getScene().addChild(anchorNode);
    node.select();
}

 

Conclusion 

 

You will find your 3D model loaded on your screen as soon as the scene is detected.

 

Note: Make sure to use the latest version of sceneform package in your project.

 

Also Read: Understanding The Lifecycle of Custom View In Android

 

Choose Oodles For AR App Development

 

We are a seasoned AR VR app development company that specializes in building high-quality virtual reality apps that deliver immersive user experiences. Our development team uses advanced frameworks and tools like Unity, Vuforia, Argon.js, and React VR to build high-graphics VR apps that maximize user engagement. Explore our augmented reality app development services here. For more information, contact us at info@oodlestechnologies.com.

Related Tags

About Author

Author Image
Aman Srivastava

Aman is a passionate Mobile Application Developer, who likes to explore and implement new technologies.

Request for Proposal

Name is required

Comment is required

Sending message..