Android - How can I build a basic Vuforia app

May 23, 2014 - 12:44pm #1

This article provides some guidelines on how to build a basic Vuforia-enabled Android application, starting from an empty Activity. This only covers basic setup steps, such as Vuforia initialization, tracker initialization and starting / stopping the camera. For more details about specific features, you can consult the developer guide and look at the sample code in the Vuforia samples.

This article refers to common concepts and API of the Android Activity life-cycle, such as creating, pausing, resuming and destroying an Activity.

For more information about the Android Activity life-cycle, and general Android programming topics, please refer to the official Android developer guide:

https://developer.android.com/guide/index.html

http://developer.android.com/training/basics/activity-lifecycle/index.html

Vuforia initialization

The onCreate() method is called when your Activity is created; this is the place where you typically put most of your application initialization code; as such, this is also where you will usually put the code to initialize Vuforia, using the Vuforia.init() method, e.g.:

Vuforia.setInitParameters( myActivity, Vuforia.GL_20 );
do
{
    mProgressValue = Vuforia.init();
}
while (mProgressValue >= 0 && mProgressValue < 100);

Since the initialization of Vuforia may take some time, it is recommended to wrap the code above inside an AsyncTask and execute it asynchronously. The Vuforia samples show a correct implementation of this.

Initializing the Trackers

Upon completion of the Vuforia initialization, the next step consists in initializing the Trackers; note that you may want to use an ImageTracker, or a MarkerTracker, or a TextTracker, depending on the specific features you plan to use in your app. You can also initialize multiple Trackers at the same time (for example the ImageTracker and the MarkerTracker, if you want to use both Image Targets and Frame Markers in your app). The following code snippet shows how you can initialize the ImageTracker:

TrackerManager tManager = TrackerManager.getInstance();
Tracker tracker = tManager.initTracker(ImageTracker.getClassType());

Tracker Data Loading

Once the trackers are successfully initialized (but not yet started), the next step is to load some data; for example, you may want to load one or more Datasets if your application relies on Image Targets. Once again, since the data loading process can take some time (depending on the amount of data to be loaded), you should consider using an AsyncTask to perform this task; the Vuforia samples provide a good implementation example.

Setting up the OpenGL view and starting the camera

Upon completing the data loading, the next phase will typically consist in initializing the camera and starting the camera; however, before even starting the camera, you need to create an OpenGL view and add it to your Activity, as shown in this code example:

int depthSize = 16;
int stencilSize = 0;
boolean translucent = Vuforia.requiresAlpha();

mGlView = new SampleGLView(this);
mGlView.init(translucent, depthSize, stencilSize);

mRenderer = new ImageTargetRenderer();
mGlView.setRenderer(mRenderer);

addContentView(mGlView, new LayoutParams(LayoutParams.MATCH_PARENT,
           LayoutParams.MATCH_PARENT));

You can refer to the Vuforia samples for the detailed implementation of the OpenGL view and the related renderer.

Once the OpenGL view is set up, the camera can be started and the video-background can be configured:

CameraDevice.getInstance().init(camera);

configureVideoBackground();

CameraDevice.getInstance().selectVideoMode(
                CameraDevice.MODE.MODE_DEFAULT))

CameraDevice.getInstance().start());

Vuforia.setFrameFormat(PIXEL_FORMAT.RGB565, true);

Pausing/Resuming the Activity

The onPause() method is called when the Activity is paused; this is where you should add some code to stop the camera, pause Vuforia and hide the OpenGL view, as shown in this example:

@Override
protected void onPause()
{
    super.onPause();

    stopCamera();

    if (mGlView != null)
    {
        mGlView.setVisibility(View.INVISIBLE);
        mGlView.onPause();
    }

    Vuforia.onPause();
}

In the code above, note in particular the call to Vuforia.onPause().

 

Similarly, the onResume() method is called when the Activity is resumed; this is where you should put some code to resume Vuforia, restart the camera and show the OpenGL view, for instance:

@Override
protected void onResume()
{
    super.onResume();

    Vuforia.onResume();

    if (mGlView != null)
    {
       mGlView.setVisibility(View.VISIBLE);
       mGlView.onResume();
    }  

    startCamera();       
}

In the code above, note in particular the call to Vuforia.onResume().

 

Activity destruction

When your activity is terminated, the onDestroy() method is called; this is where you should put your code to deinitialize Vuforia, stopping camera and trackers, deinitializing the trackers and unloading any data, e.g.:

@Override
protected void onDestroy()
{
    super.onDestroy();
       
    stopTracker();
    stopCamera();
       
    deinitTracker();

     Vuforia.deinit();
}

In the code above, note in particular the call to Vuforia.deinit ().

Please refer to the Vuforia samples for all the implementation details.

 

 

Topic locked