Skip to main content

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
 }

 @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.


      Comments

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

        ReplyDelete
      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 ;)

        ReplyDelete

      Post a Comment

      Popular posts from this blog

      What You Must Know Before Establishing a Recovery Plan ?

      In today's rapidly evolving digital landscape, organizations are increasingly adopting the zero trust model, primarily due to the expanding attack surface that leaves critical systems and data exposed. This shift is also fueled by the heightened sophistication of cyber-attacks, which have become more complex and harder to detect, surpassing traditional security measures. Additionally, the existing operating models within organizations are often inconsistent, typically characterized by distributed and siloed environments.    This fragmentation creates vulnerabilities and makes it challenging to implement uniform security protocols. The zero trust model addresses these challenges by assuming that threats exist both inside and outside the network, necessitating continuous verification of all users and devices. Its adoption represents a proactive stance in the ongoing battle against cyber threats, ensuring a more robust and resilient organizational security posture. The Evolution ...

      A comprehensive guide to ransomware distribution in VMware environments

      In a virtualized on-premises environment based on VMware, ransomware distribution scenarios can be somewhat unique due to the nature of virtualization technology. However, many of the traditional attack vectors still apply. Here are some ransomware distribution scenarios specific to a VMware-based virtualized environment: Phishing Attacks Targeting Administrators: Administrators with access to the VMware environment might receive phishing emails. If they fall for these and their credentials are compromised, attackers can gain access to the virtualized environment. Exploiting Vulnerabilities in VMware Software: If VMware software or the underlying operating system is not kept up-to-date with security patches, vulnerabilities can be exploited by attackers to deliver ransomware into the virtualized environment. Compromised Remote Management Tools: Tools used for remote management of the virtualized environment, such as vSphere, can be a target. If these tools are compromised, attackers ca...

      Edge Computing Demystified Book

      After a while I'm back and pleased  to share in this post my first book around Edge computing Technologies. Edge computing has been a very hot and interesting topic nowadays for communication service provider and Enterprise so far. Augmented Reality / Virtual Reality, Smart cities, Healthcare, industrial IoT and many others use cases require a change in the way we operate and host application in the cloud.  IA, Big Data and analytics are often used today to understand the behavior of the customer and even the health of services. Real-time and high throughput demand are the characteristic of the new business services. Edge computing technology promises to resolve different challenges and brings compute, storage and bandwidth close to the data source. I tried in ‘the Edge Computing Demystified’ book to explain Edge computing technology referring to different use cases from communication service provider and enterprise industry. I h...