更改jni核心类中renderFrame函数可以实现成功识别后更改提示标签等功能。
当前高通给的例子识别后现实的默认的信息,比如3D模型等,如果想要现实自定义的信息,那必须要找到识别成功函数,这个函数在jni的核心 类中,以ImageTargets项目为例,那么则需要更改ImageTargets.cpp类中的renderFrame函数,具体方法如下:
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(); } ... } ... }
然后要在java层的 ImageTargetsRenderer类中补充相应的方法来显示提示信息,:
// 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); }
最后还要在主类ImageTargets 中初始化handler类并且定义具体处理机制:
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(); } }; }
只是测试,所以比较简易,没有考虑Toast会排序显示,就算程序退出!
显示2D图像可以参考:https://ar.qualcomm.at/arforums/showpost.php?p=1596
由于对OPENGL的了解太少,自己改完后没有正常显示替换图片,只是一个同等大小的黑色图块而已。
以上信息参考自 高通vuforiaAR论坛: https://ar.qualcomm.at/arforums/showpost.php?p=70
很幸运,找到了解决显示3D的方法: https://ar.qualcomm.at/arforums/showthread.php?t=231
还得感谢HEIKO BEHRENS老兄的解决方案: http://heikobehrens.net/2009/08/27/obj2opengl/
当然,虽然进步了,但还不是很完美 ,需要更多的突破, 翻滚吧,骚年、、