dimanche 13 mars 2011

Augmented Reality Part 1

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.

  • 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

 public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  // TODO Auto-generated method stub

 public void surfaceCreated(SurfaceHolder arg0) {
  // TODO Auto-generated method stub

 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();
    • Initialize the camera when the Surface View is creating : 

     public void surfaceCreated(SurfaceHolder _holder) {
      camera = Camera.open();
      try {
      } 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);
      } catch (Exception e) {

    • Stop the camera when  the view is hiden: 
       public void surfaceDestroyed(SurfaceHolder holder) {

    • 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"
        >                                                                                                                                                    <view
            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"

      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.

      2 commentaires:

      1. Good job, it seems to be very useful for my future research :)

      2. ohhh nice, Soon inch ALLAH i will publish Part 2 of this tutorial which focus in Accelerometer and GPS location.

        You are welcome to the blog ;)