This section applies only to the Augmented Reality SDK v4 +.
The CraftAR Android Augmented Reality SDK allows you to create AR apps that render the experiences created with the CraftAR service. If you’re not yet familiar with the general steps, read How to add augmented reality into your app.
If you want to see an example that combines on-device collections with Tracking, take a look at the open-source samples available in our Github repository (on-device AR example).
In this article we explain how to create Augmented Reality apps using on-device collections. This allows to embed the AR items and their contents into the app and start Tracking without the need to load the items and the contents from the network.
An on-device Augmented Reality app using the iOS native SDK can be implemented following three steps.
- Add the on-device collection to the device;
- set up the SDK in your UIVIewController;
- trigger Augmented Reality experience.
This tutorial covers the second and third steps. For the first step, have a look at the tutorial on Managing on-device collections for the Android SDKs
Set up the SDK in your CraftARActivity
Once you have set up the CraftARSDK into your Android project, and added the collection bundle to your app , it’s time to implement the CraftARActivity that will show the experience.
1. Create a CraftARCameraView in your layout. This is the view that will show the AR experience.
1
2
3
4
5
6
7
8
9
10
11
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyCraftARActivity">
<com.craftar.CraftARCameraView
android:layout_width="match_parent"
android:layout_height="match_parent"
</com.craftar.CraftARCameraView>
</RelativeLayout>
|
2. Make your Activity extend from CraftARActivity, start the camera capture, and obtain the CraftARTracking instance
You will have to implement the methods onPostCreate(), onPreviewStarted(), and onCameraOpenFailed(). Obtain the CraftARSDK instance and start the camera capture:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
public class MyCraftARActivity extends CraftARActivity{
CraftARSDK mCraftARSDK;
CraftARTracking mCraftARTracking;
@Override
public void onPostCreate() {
View mainLayout= (View) getLayoutInflater().inflate(R.layout.activity_main, null);
setContentView(mainLayout);
mCraftARSDK = CraftARSDK.Instance();
/**
* Initialise the SDK with your Application Context before doing any other
* operation with any other module. You can do this from any Activity
* (for example the Splash Activity); */
mCraftARSDK.init(getApplicationContext());
mCraftARSDK.startCapture((CraftARActivity)this); //Pass a reference to the Activity
...
}
@Override
public void onPreviewStarted(int frameWidth, int frameHeight) {
// startCapture() was completed successfully, and the Camera Preview has been started
//Obtain the CraftARTracking instance. Note that until onPreviewStarted() is called, the
//CraftARTracking instance is not ready to perform any operation. You must wait for
// the preview to start before doing any call to the CraftARTracking object.
mCraftARTracking = CraftARTracking.Instance();
}
@Override
public void onCameraOpenFailed(){
// startCapture() failed, so the Camera Preview could not be started
}
}
|
Triggering Augmented Reality experience
Once your ViewController has the necessary protocols and instance of Tracking, it’s time to add code to start using the iOS Augmented Reality SDK for a mobile app.
In this case, we will load the items from an on-device collection we have previously added to the device. The following snippet shows how to traverse a collection’s items and add them for Tracking.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
private void loadCollection(CraftAROnDeviceCollection collection) {
// Get all item UUIDs in the collection
for(String itemUUID: collection.listItems()){
// Get the item and check that it is an AR item
CraftARItem item = collection.getItem(itemUUID);
if(item.isAR()){
CraftARItemAR itemAR = (CraftARItemAR)item;
try {
// Add the item to the tracking
Log.d(TAG, "Adding item "+item.getItemName()+" for tracking");
mCraftARTracking.addItem(itemAR);
} catch (CraftARSDKException e) {
e.printStackTrace();
}
}
}
// Start tracking the items from this collection.
mCraftARTracking.startTracking();
}
|
When the ARItems are loaded and startTracking() has been invoked, you can point to the object and the Augmented Reality experience will be shown.