Replace the for loop in the renderframe method with the following.
// Did we find any trackables this frame? for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++) { // Get the trackable: const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx); // Compare this trackable's id to a globally stored id // If this is a new trackable, find the displayMessage java method and // call it with the trackable's name if (trackable->getId() != lastTrackableId) { jstring js = env->NewStringUTF(trackable->getName()); jclass javaClass = env->GetObjectClass(obj); jmethodID method = env->GetMethodID(javaClass, "displayMessage", "(Ljava/lang/String;)V"); env->CallVoidMethod(obj, method, js); lastTrackableId = trackable->getId(); }
Define the lastTrackableId variable before the renderFrame function. int lastTrackableId = -1;
Add the following to ImageTargetsRenderer.java .
public void displayMessage(String text) { // We use a handler because this thread cannot change the UI Message message = new Message(); message.obj = text; mainActivityHandler.sendMessage(message); }
Add the following to the beginning of the ImageTargetsRenderer.java class definition.
public static Handler mainActivityHandler;
Add the Handler to ImageTargets.java in the onResume() Method
ImageTargetsRenderer.mainActivityHandler = new Handler() { @Override public void handleMessage(Message msg) { String text = (String) msg.obj; // The Toast display the name of the detected trackable on the screen Context context = getApplicationContext(); int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show(); // The following opens a pre-defined URL based on the name of trackable detected if (text.equalsIgnoreCase("stones")) { Uri uriUrl = Uri.parse("http://ar.qualcomm.at"); Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); startActivity(launchBrowser); } if (text.equalsIgnoreCase("chips")) { Uri uriUrl = Uri.parse("http://developer.qualcomm.com"); Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl); startActivity(launchBrowser); } } };