Log in or register to post comments

How to get active trackable name in JAVA??

June 19, 2011 - 6:24am #1

Hello

part of my code:

/** Native function to receive touch events. */
    public native void nativeTouchEvent(int actionType, int pointerId, float x, float y);
    
    /** Send touch events to native. */
    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction();
        int actionType = -1;
        int pointerIndex = -1;
        this.display_toast("onTouchEvent: MotionEvent.ACTION_Touch" );
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: // on click
            	this.display_toast("onTouchEvent: MotionEvent.ACTION_DOWN" );
                actionType = 0;
	            for (int i = 0; i < event.getPointerCount(); i++) {
	                int pointerId = event.getPointerId(i);
	                float x = event.getX(i);
	                float y = event.getY(i);
	                this.display_toast("onTouchEvent: pointerIndex: (" + Float.toString(x)+","+ Float.toString(y) + ")" );
	                
	                String active_trackable_name = "???";
	                //String tracker_name = HOW TO GET CURENT TRACKER NAME ?
	                if( active_trackable_name == "track1" ){
	                	//TODO
	                }
	                else if( active_trackable_name == "track2" ){
	                	//TODO 2
	                }
	                else{
	                	this.display_toast("There is no tracakble active or trackable is unknown");
	                }
	            }
            break;
        }
        return true;
    }

Now the problem is: how to get CURENT TRACKER NAME
beacouse I want to do different actions on each trackables.

//String tracker_name = HOW TO GET CURENT TRACKER NAME ?

Any help :) ?

Thx
Niedved

Re: How to get active trackable name in JAVA??

June 19, 2011 - 2:18pm #3

Thanks. It works fine.

But finally I make new App :)

CPP:

jstring name = env->NewStringUTF(trackable->getName());
jclass clazz = env->GetObjectClass(obj);
jmethodID getObjectTrackedMethodID = env->GetMethodID(clazz,"ObjectTrackedBuy", "(Ljava/lang/String;)I");
env->CallVoidMethod(obj, getObjectTrackedMethodID, name );

and In Java ImageTargetsRenderer.java:

    public Handler _alertDialogHandlerBuy;
   
    public int ObjectTrackedBuy( String tname )
    {
    	track_name=tname;
        if(!invokedbuy)
        {
            invokedbuy=true;
            _alertDialogHandlerBuy.sendEmptyMessage(0);
        }

        return 1; 
    }

And in ImageTargets.java:

mRenderer._alertDialogHandlerBuy = new Handler() {
            @Override
            public void handleMessage(Message msg) {
               
                create_buttons_for( mRenderer.track_name);
            }
        };

Thanks again.

Re: How to get active trackable name in JAVA??

June 19, 2011 - 11:09am #2

Here's the general idea and then a code snippet:

When you touch the screen the OS passes the event to your java activity if you subscribe to it w/ onTouch method. From there it is your app's job to pass it to the native layer. Get the trackable name using QCAR APIs and then return it back to java layer and print to log.

Add this method to your cpp files:

(pseudo code)

JNIEXPORT jstring JNICALL 
Java_com_your_name_space_nativeTouchEvent( JNIEnv* env, jobject obj, jint actionType, jint pointerId, jfloat x, jfloat y)
{
  LOG("***************** NATIVE TOUCH EVENT &*********");
  QCAR::State state = QCAR::Renderer::getInstance().begin();
  const char * name="";
  for(int tIdx = 0; tIdx < state.getNumActiveTrackables(); tIdx++)
  {
    //get target object 
     const QCAR::Trackable* trackable = state.getActiveTrackable(tIdx);
     name = trackable->getName();
  }

  return env->NewStringUTF(name);
    
}

You must add that signature to your java class too (like all JNI stuff).

Then in your java on the onTouch: (pseudo code)

 @Override
       public boolean onTouchEvent(MotionEvent event)
       {
.
.
.
     
                   int pointerId = event.getPointerId(i);
                   float x = event.getX(i);
                   float y = event.getY(i);
                   String tname=nativeTouchEvent(actionType, pointerId, x, y);
                   DebugLog.LOGI("tracker name from java: "+tname);
    
     .
.
.
          
           return true;
       }
Log in or register to post comments