Skip to content
This repository has been archived by the owner on May 18, 2022. It is now read-only.

A.8. Step by Step Through Initializing

Hadi Tavakoli edited this page Aug 2, 2019 · 6 revisions

AR ANE Initialization

When starting with Augmented Reality, you may think it's a huge task and very hard to deal with. But you will be amazed how easy and straight it is to start the AR camera and bring in your own AR objects into the scene.

If you have not yet run the sample demo project, we strongly suggest you to do that first. When you are sure that your project setup is all correct and you can successfully compile your app with the AR ANE enabled, it will be the time to learn more about how you must treat the Actionscript side of this gorgeous ANE.

To give you an outline of the tasks you should do, follow these steps. Then we will explain each step in more details.

  1. use AR.init to init the ANE
  2. use AR.checkFeatureSupport to check if the features you need are supported
  3. use AR.config to specify the AR settings before camera opens
  4. use AR.launchAR to open the AR window with your specified html world

Step 0

Does your app have the required permissions? When dealing with Augmented Reality, you have to have camera permission for sure but depending on what type of AR experience you are planning to develop for your app, you may also need GPS permissions. Before you launch your AR window, you must use the PermissionCheck ANE to ask for the required permissions. The following example shows you how to ask for camera and then the GPS permissions:

if(PermissionCheck.check(PermissionCheck.SOURCE_CAMERA) == PermissionCheck.PERMISSION_GRANTED)
{
	onCameraRequestResult(PermissionCheck.PERMISSION_GRANTED);
}
else
{
	PermissionCheck.request(PermissionCheck.SOURCE_CAMERA, onCameraRequestResult);
}

function onCameraRequestResult($obj:Object):void
{
	if($obj.state != PermissionCheck.PERMISSION_GRANTED)
	{
		trace("AR ANE needs Camera access to work properly");
		return;
	}
	
	if(OverrideAir.os == OverrideAir.ANDROID && PermissionCheck.check(PermissionCheck.SOURCE_LOCATION) == PermissionCheck.PERMISSION_GRANTED)
	{
		onLocationRequestResult(PermissionCheck.PERMISSION_GRANTED);
	}
	else if(OverrideAir.os == OverrideAir.IOS && PermissionCheck.check(PermissionCheck.SOURCE_LOCATION_WHEN_IN_USE) == PermissionCheck.PERMISSION_GRANTED)
	{
		onLocationRequestResult(PermissionCheck.PERMISSION_GRANTED);
	}
	else
	{
		if(OverrideAir.os == OverrideAir.ANDROID)
		{
			PermissionCheck.request(PermissionCheck.SOURCE_LOCATION, onLocationRequestResult);
		}
		else if(OverrideAir.os == OverrideAir.IOS)
		{
			PermissionCheck.request(PermissionCheck.SOURCE_LOCATION_WHEN_IN_USE, onLocationRequestResult);
		}
	}
}

function onLocationRequestResult($obj:Object):void
{
	if($obj.state != PermissionCheck.PERMISSION_GRANTED)
	{
		trace("AR ANE needs Location access to work properly");
		return;
	}
	
	trace("Cheers, All permissions are allowed for AR to work correctly.");
	
	// Yoohoo, all permissions are ready :)
}

Step 1

The first step is to initialize the ANE by passing the Android and iOS SDK keys you have received from Wikitude.

AR.init("AndroidKey", "iOSKey");

AR.listener.addEventListener(ArEvents.WORLD_LOAD_RESULT, onWorldLoadResult);
AR.listener.addEventListener(ArEvents.NATIVE_WINDOW_CLOSED, onArClosed);
AR.listener.addEventListener(ArEvents.CALIBRATION_NEEDED, onCalibrationNeeded);
AR.listener.addEventListener(ArEvents.CALIBRATION_DONE, onCalibrationDone);
AR.listener.addEventListener(ArEvents.JS_TALK, onJsTalk);
AR.listener.addEventListener(ArEvents.SCREENSHOT_RESULT, onScreenshotresult);

After you initialized the ANE, it's time to add the listeners for all the different events that the ANE might dispatch.

Step 2

The second step is to use the ANE to check if the features you need for your AR experience is supported on the current running device or not. If it's not, you must tell your so without running the AR window. This will prevent a poor experience for your user to avoid seeing errors in runtime. This is also useful so you can maybe disable some of your AR experience based on what features are not supported on your user's device.

var features:Array = [];
features.push(Features.GEO);
features.push(Features.IMAGE_TRACKING);
features.push(Features.INSTANT_TRACKING);
features.push(Features.OBJECT_TRACKING);

AR.checkFeatureSupport(features, onCheckResult);

function onCheckResult($status:int, $msg:String):void
{
	if($status == AR.RESULT_OK)
	{
		trace("Device supports all requested AR features");
	}
	else if($status == AR.RESULT_ERROR)
	{
		trace("Device does not support all features: " + $msg);
	}
}

the $msg variable will tell you what features have not been supported by the device.

Step 3

You're almost there, before you can launch your AR experience, you need to set the camera initial configuration. You can do this by creating an object from the Config class like this: var _arSettings:Config = new Config();. Then, use the instance to set your configuration. Some of the settings are the same on both platform but some of them are specific to each platform.

_arSettings.camFocusMode = CameraFocusMode.CONTINUOUS;
_arSettings.camPosition = CameraPosition.BACK;
_arSettings.camResolution = CameraResolution.SD_640x480;

// for best performance, you must activated only the AR features which you will use.
_arSettings.hasGeo = true;
_arSettings.hasInstant = true;
_arSettings.hasIR = true;
_arSettings.hasObject = true;

if(OverrideAir.os == OverrideAir.ANDROID)
{
	// Android specific settings
	_arSettings.android.cam2Enabled = false;
	_arSettings.android.fullscreenMode = true;
	_arSettings.android.camFocusDistanceAndroid = 1;
}
else if(OverrideAir.os == OverrideAir.IOS)
{
	// iOS specific settings
	_arSettings.ios.camFocusDistance = -1.0;
	_arSettings.ios.camFocusRange = FocusRange.NONE;
	_arSettings.ios.framerate = 30;
	_arSettings.ios.excludeBinnedVideo = true;
}

AR.config(_arSettings);

Step 4

The final step is to launch your AR experience. you can load the AR from your app assets or from the documents directory or even from an url.

Hint: If you are loading the AR from File.documentsDirectory on Android, make sure you have already asked users for the storage access.

AR.launchAR("ARWorld/index.html");

If you have finished building a cool AR app with Adobe AIR, we'd be happy to see it :)