It's so exciting to add Augmented reality to your Android Application.There are some Open frameworks that allow you adding AR to your Android project but in this post we will discover in details how we can make our AR application using Camera, Sensor and GPS.
Augmented Reality is a term for a live direct or indirect view of a physical real world environment whose elements are augmented by computer-generated sensor input, such as sound or graphics.
There are many popular Applications of AR such as Layar, wikitube, Googles ...
With this post, we want make it easy as possible for new developers to begin and understand the AR technology.
In this first part of tutorial, you will create a custom surface view on which you'll load the camera preview.
Your can see the Camera view in the Android Emulator :
In the next part of this tutorial, we will see how to visualize your real objects in the camera view based in their GPS locations.

There are many popular Applications of AR such as Layar, wikitube, Googles ...
With this post, we want make it easy as possible for new developers to begin and understand the AR technology.
In this first part of tutorial, you will create a custom surface view on which you'll load the camera preview.
- After creating an Android project including Google Api for the use of the Location librairies, add the permission of Camera to your Manifest file.
<uses-permission android:name="android.permission.CAMERA" />
- Create now a view which draw the captured frames of the Camera.For this, add a new class CameraView which extends SurfaceView and hand it off to camera.
package com.houcem.reality;
import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraView extends SurfaceView implements SurfaceHolder.Callback{
public CameraView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
}
- Add now the differents elements of your view and intialize them in the constructor
private Camera camera;
private SurfaceHolder holder;
private Parameters parameters;
public CameraView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
holder = getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(this);
setKeepScreenOn(true);
}
- Initialize the camera when the Surface View is creating :
public void surfaceCreated(SurfaceHolder _holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(_holder);
} catch (Exception e) {
}
}
- Set the camera Parameters :
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
try {
parameters = camera.getParameters();
parameters.setPreviewSize(800, 480);
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (Exception e) {
}
}
- Stop the camera when the view is hiden:
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
}
- In your Layout xml file "main.xml" add your view to show the camera Surface :
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <view class="com.houcem.reality.CameraView" android:id="@+id/camera" android:layout_width="wrap_content" android:layout_height="wrap_content" android:keepScreenOn="true"/> </FrameLayout>
- Fianlly, show your View in Landscape mode and hide the the title bar by adding in your AndroidManifest file the following parameters :
<activity android:name=".MyAReality" android:label="@string/app_name" android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
Your can see the Camera view in the Android Emulator :
In the next part of this tutorial, we will see how to visualize your real objects in the camera view based in their GPS locations.
Good job, it seems to be very useful for my future research :)
ReplyDeleteohhh nice, Soon inch ALLAH i will publish Part 2 of this tutorial which focus in Accelerometer and GPS location.
ReplyDeleteYou are welcome to the blog ;)