Hello mohamedepic,
Here are the step to do this and also display a Toast of which trackable is found:
1. Update ImageTargets.cpp as follows
int lastTrackableId = -1;
JNIEXPORT void JNICALL
Java_com_qualcomm_QCARSamples_ImageTargets_ImageTargetsRenderer_renderFrame(JNIEnv* env, jobject obj)
{
...
// 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();
}
QCAR::Renderer::getInstance().end();
}
2. Update ImageTargetsRenderer.java as follows
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);
}
3. Add the Handler to resolve the error in ImageTargetsrenderer.java. At the beginning of the class definition add.
public static Handler mainActivityHandler;
4. 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);
}
}
};
Thank you,
-Peter
Hi.. I am trying to achieve the same and I am able to open a web browser but when I come back to the app the tracker is not recognized..m i missing something..following is my rederfame function: