This section applies only to the Augmented Reality SDK v4 +. Are you still using an older version? Previous versions of the SDKs will not receive updates anymore. If you need help transitioning to the newer version, you can get support through our support channel.
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.
An Augmented Reality app using the Android native SDK can be implemented following two steps. First, you need to set up the CraftARActivity and then you can trigger Augmented Reality experiences.
If you want to see an example that combines Cloud Image Recognition (see Tutorial: Use Cloud Image Recognition on Android ) with Tracking, take a look at the open source samples available in our Github repository: https://github.com/Catchoom/craftar-example-android.
Set up the SDK in your CraftARActivity
Once you have set up the CraftARSDK into your Android project, 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
}
}
|
Rendering Augmented Reality
To perform tracking, you have to load objects of the class CraftARItemAR to the tracking instance. A CraftARItemAR is a special type of CraftARItem that describes an AR experience. To obtain a CraftARItem:
- If you’re using Cloud Image Recognition, you can obtain the CraftARItems in the searchResults() callback after a succesful image recognition request.
- If you’re using On-Device Augmented Reality, you can list the available items in the collection using CraftAROnDeviceCollection.listItems(), and obtain an item using CraftAROnDeviceCollection.getItem(itemUUID)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
private void loadItemsAndStartTracking(ArrayList<CraftARItem> items) {
// Load the items for Tracking. Note that you can only do load items for Tracking
// after onPreviewStarted() has been invoked.
for(CraftARItem item:items){
if(item.isAR()){ //Check if the item is an Augmented Reality item
CraftARItemAR itemAR = (CraftARItemAR)item;
try {
mTracking.addItem(itemAR); //Load this item for the AR experience
}catch(CraftARSDKException e){
Log.e(TAG, "Error adding item "+itemAR.getItemName()+" for tracking");
}
}
}
mTracking.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.