This is possible but somewhat tricky. If you're new to Android, you may want to start by creating the user interface without the Augmented Reality SDK, to learn how the Android UI works.
Then, I suggest copying the ImageTargets sample project as a starting point for an AR application. Add your custom user interface elements to it.
There are a few steps required for making changes to the Android UI when a trackable becomes active. Let's start with the native code. Here is a simple example for passing a message from native to Java when a trackable first comes into view. Add this to ImageTargets.cpp:
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();
}
...
}
...
}
Next, here's the java method that the native code is calling. Add this to the ImageTargetsRenderer class in java:
// A handler object for sending messages to the main activity thread
public static Handler mainActivityHandler;
// Called from native to display a message
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);
}
Finally, you need to create and register this handler in the ImageTargets java class. The onResume method is a good place to do this:
protected void onResume()
{
super.onResume();
// Create a new handler for the renderer thread to use
// This is necessary as only the main thread can make changes to the UI
ImageTargetsRenderer.mainActivityHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
Context context = getApplicationContext();
String text = (String) msg.obj;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
};
}
This example should display a temporary message on the screen when a new target comes into view (either the stones or chips targets that the ImageTargets sample uses). Hopefully this will get you started.
- Kim
thanx, that will be usefull for me.. but, i mean the image is not as 3d object, but an image that overlay the camera..