Augmented reality is a hot topic in Android these days. We can make an interactive application by getting sensor data from a mobile device. Then we can overlay some useful data on our application screen. In this first part, a location based augmented reality Android tutorial, we will be directly accessing the camera to get the live preview.

In the next part of these tutorials, we will learn about getting data from a sensor. We will display that data on our log-cat and on the camera screen. Also, we will get the nearby location data from the Google Places API and we will augment that data on our screen.  Each nearby location will be identified as a P.O.I. (Point Of Interest) or a marker. We will also learn about calculating the orientation of devices using the accelerometer and magnetic sensor .

About our augmented reality Android tutorial

The aim of this Android augmented reality tutorial series is to calculate the device orientation using sensors which will help us to find the direction of north. Then we will get the latitude and longitude from the GPS. After that, we will find the nearby JSON data from Google Places API.


Setting up the camera

We will make our own Android augmented reality framework. We need permission for directly accessing the camera. We will be the one controlling the camera as we will augment outside world information on our camera screen. First, create a new Android application. Now go to your Android activity layout XML file and replace all content with the following code.
Snippet 1

Camera Permission – Also make sure to add the following permission in your Android manifest file to use the device camera and its features.
Snippet 2

Now let’s move to the coding part in java. Go to your activity file and add these class variables:

  1. The SurfaceHolder is an interface which allows us to control the surface size and monitors changes to the surface.
  2. The SurfaceView provides a surface on which we will show the camera screen to the user.
  3. The Camera class is the primary API for controlling the camera of the device. This class is used to take pictures and videos.
  4. We use inPreview to check if our application is accessing the camera or not.

Snippet 3

Now you need to implement the SurfaceHolder.Callback interface.
Snippet 4

Here’s the complete code to setup the camera for the augmented reality Android framework:
Code 1

This was all about getting started with the camera for augmented reality. Please let me know if you have any suggestions or doubts.