ArCore for building Android AR applications.

Build App using ARCore

ArCore is a platform for building Android AR applications. It enables your phone to sense its environment. 

ARCore works on 3 principles:

  • Motion Tracking: It allows the phone to understand its current position relative to the real world.
  • Understanding the environment: It allows the phone to detect the size and location of all types of surfaces: vertical, horizontal, and angled.
  • Light Estimation: It allows the phones to sense the environment’s lighting conditions.

As the user moves their phone in the actual world, ARCore can recognize its surroundings and create a digital simulation of the real world in which it can place objects. In order to maintain track of its location with respect to the real environment, ARCore uses motion tracking to identify characteristics.

Sceneform

ARCore is an engine that aids SDKs in rendering the objects, not an SDK in and of itself. As a result, Google launched Sceneform SDK to allow developers to create Android AR apps without having to understand OpenGL in order to take advantage of these capabilities.

Building AR Application

  • Open Android Studio, and create a new project.
  • Adding Sceneform plugin, File -> Settings -> Plugins
  • Now type Sceneform in the search bar, install, and restart IDE.
  • Adding dependencies in build.gradle implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.9.0'
  • Add permissions in manifest
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.camera.ar" android:required="true"/>
  • Add metadata in the application tag
<meta-data
   android:name="com.google.ar.core"
   android:value="required" />
  • Creating XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   tools:context=".MainActivity">

   <fragment
       android:id="@+id/ux_fragment"
       android:name="com.google.ar.sceneform.ux.ArFragment"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="9" />

   <LinearLayout
       android:id="@+id/gallery_layout"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       android:orientation="horizontal" />

</LinearLayout>
  • Java Code

Inside onCreate()

if (!checkIsSupportedDeviceOrFinish(this)) {
   return;
}


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

arFragment.setOnTapArPlaneListener(
       (HitResult hitResult, Plane plane, MotionEvent motionEvent) -> {
           Anchor anchor = hitResult.createAnchor();
           placeObject(arFragment, anchor, Uri.parse("model.sfb"));

       });
  • Adding models
private void placeObject(ArFragment arFragment, Anchor anchor, Uri uri) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
       ModelRenderable.builder()
               .setSource(arFragment.getContext(), uri)
               .build()
               .thenAccept(modelRenderable -> addNodeToScene(arFragment, anchor, modelRenderable))
               .exceptionally(throwable -> {
                           Toast.makeText(arFragment.getContext(), "Error:" + throwable.getMessage(), Toast.LENGTH_LONG).show();
                           return null;
                       }

               );
   }

}

private void addNodeToScene(ArFragment arFragment, Anchor anchor, Renderable renderable) {
   AnchorNode anchorNode = new AnchorNode(anchor);
   TransformableNode node = new TransformableNode(arFragment.getTransformationSystem());
   node.setRenderable(renderable);
   node.setParent(anchorNode);
   arFragment.getArSceneView().getScene().addChild(anchorNode);
   node.select();
}


public static boolean checkIsSupportedDeviceOrFinish(final Activity activity) {
   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
       Log.e(TAG, "Sceneform requires Android N or later");
       Toast.makeText(activity, "Sceneform requires Android N or later", Toast.LENGTH_LONG).show();
       activity.finish();
       return false;
   }
   String openGlVersionString =
           ((ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE))
                   .getDeviceConfigurationInfo()
                   .getGlEsVersion();
   if (Double.parseDouble(openGlVersionString) < MIN_OPENGL_VERSION) {
       Log.e(TAG, "Sceneform requires OpenGL ES 3.0 later");
       Toast.makeText(activity, "Sceneform requires OpenGL ES 3.0 or later", Toast.LENGTH_LONG)
               .show();
       activity.finish();
       return false;
   }
   return true;
}

Sreyas IT Solutions is a leading mobile app development company, with vast experience in custom development and design. We are also very well capable of developing e-commerce and other web applications along with mobile apps.

Recent Blogs


Posted

in

,

by

Tags:

To Know Us Better

Browse through our work.

Explore The Technology Used

Learn about the cutting-edge technology and techniques we use to create innovative software solutions.