Get Started         Best Practices         Developer Guide         FAQ        

Tutorial: how to use CraftAR On-Device Image Recognition for Cordova

Last Updated: Nov 29, 2017
This article applies both to the On-Device Image Recognition Cordova Plugin and to the CraftAR Pro SDK Cordova Plugin.

In order to add on-device collections to your Android app, it is necessary to:

  1. create an on-device collection in CraftAR;
  2. generate a collection bundle for your SDK version;
  3. download the bundle of that collection from CraftAR and add it to the app;
  4. you are ready to go and start using the on-device collection with the SDK

Once the collection is added to the device, you can start using the On-device Image Recognition SDK to perform visual search queries on the device. You can find more details for the first step in the tutorial about how to create an on-device collection in CraftAR. And for the second and third steps, please read the tutorial about how to Manage on-device collections for the Android SDKs.

An Image Recognition app using the native On-device Android Image Recognition SDK can be implemented by the following two steps. First, setup the on-device collection. Second, set up the page and the camera capture and run the on-device image recognition to get the results for each item that is recognized.

1. Set up the on-device collection in your app

Loading collections into memory

Once the collection is added to the local database, we can set the collection that will be used for on-device image recognition using the CraftAROnDeviceIR class.

Using the CraftAROnDeviceIR.setCollection() method and passing the collection, the plugin will load the collection to the On-device Image Recognition module and set it active for Image recognition.

CraftAROnDeviceIR.setCollection(collection, function (collection) {
    // Success callback
}, function (error) {
    // Error callback
}, function(progressFloat) {
     // ProgressCallback

Once you get the success callback, the on-device Image Recognition module is ready to receive images and produce recognition results.

Loading collections can take a few seconds, depending on the amount of images to load. The Plugin provides progress feedback for this process as well.

This process can be done in any page in your application. You just need to make sure you have access to the cordova.js script.

2 Setup the Plugin in your project

Once you have setup the CraftAR Cordova Plugin into your Cordova project, it’s time to implement the views that will allow to perform the Image Recognition on-device.

1. Start the camera capture in a new view

First of all, make sure the cordova.js script is included in your index.html start page.

<script src="cordova.js"></script>

After that, We need to start a new view. This view will allow the Plugin to open the camera in the background and display our UI as a transparent overlay.

document.addEventListener("deviceready", function () {
    CraftARSDK.startView(null, null, {"loadUrl" : "your_search_view.html"});
}, false);
2. Start the video capture

To be able to access the Plugin JS interface, the capture page needs to import the cordova.js script. Then you can listen for the 'deviceReady' event and use the SDK to start the camera capture.

<script src="cordova.js"></script>

Once the device is ready, you will have to start the capture using CraftAR SDK:

document.addEventListener("deviceready", function () {
}, false);

function startCraftAR() {
3. Prepare the SDK to process searches

The CraftARSDK class also manages the search processes by sending search events to the SearchController, in this case the CraftAROnDeviceIR instance. The CraftAROnDeviceIR instance performs visual searches in the collection of images that is previously set for this app. You can find more details with regards to how to set the bundles in the separate tutorial about how to manage collection bundles with the On-device Image Recognition SDK.

Set the CraftAROnDeviceIR class as the searchController. The searchController is the object that will manage the singleShotSearch(), startFinder() and stopFinder() calls of the CraftARSDK. We will also set the Callbacks for the search responses.

We can do this after getting the callback we set for when the capture is ready:

function didStartCapture() {
    CraftARSDK.searchController = CraftAROnDeviceIR.searchController;

2. Implementing the searches and parsing the results

Once you are ready with the previous steps, it’s time to add code to start scanning the real world.

Using the CraftARSDK class we can search the on-device image database in two modes: Finder Mode or Single Shot Mode.

Option A. Use Single Shot Mode to take a single picture

Call singleShotSearch to perform a search with a single image. By calling this method, the SDK triggers the still image capture from the camera and forwards the captured image to the search controller (in this case, the CraftAROnDeviceIR instance)

A common approach consists in triggering the search when the user performs an action on the UI (for example, clicking on a button). Ensure that you have initialized everything before allowing the user to perform the singleShotSearch, otherwise it will fail.


The response to a query triggers the callback passed to onSearchResults() with the results available in an array that is ready to parse. If the query failed, the callback passed to onSearchError() will be triggered. A query can fail for multiple reasons, but the most common one is when the query image does not have enough details. If you point to a completely uniform surface (i.e a white wall), you will receive this error.

function getResults(results) {
    if (results.length > 0) {
        var item = results[0].item;
    } else {
        alert("Nothing found!");

When the singleShotSearch() method is called, the camera is frozen until restartCapture() is called.

Option B. Use Finder Mode for continuous scanning

Call startFinder to start searching continuously without user intervention. This method forwards the camera frames to the search controller (in this case, the CraftAROnDeviceIR instance)


For every frame that is processed, the SDK generates a query and searches for matches in the local collection. The response to a query produces a callback to searchResults with the results available in an array that is ready to parse.

function getResults(results) {
    if (results.length > 0) {
        var item = results[0].item;

Couldn't find what you were looking for?
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
Invalid characters found