从java中的跳跃动作错过了轻击手势

时间:2015-07-29 07:32:38

标签: java leap-motion

我写了一个测试代码,看看是否从跳跃动作中接受了我的轻击手势。 在运行代码之后,我注意到我必须非常积极地点击,即便如此,我也只能在每次点击时收到一次点击。 如何更改我的代码以防止这种情况?

(请注意,我对编码很新,所以请详细说明,谢谢)

文件#1

    package com.leaptoarduino;
    import com.leapmotion.leap.Controller;
    import com.leapmotion.leap.Gesture;
    public class Leapd
    {
        //Main
        public static final void main(String args[])
        {
            //Initialize serial communications
            RS232Protocol serial = new RS232Protocol();
            serial.connect("COM3");
            //Initialize the Leapd listener
            LeapdListener leap = new LeapdListener(serial);
            Controller controller = new Controller();
            controller.addListener(leap);
            controller.enableGesture(Gesture.Type.TYPE_KEY_TAP);
            //Set up controller for gestures
            controller.config().setFloat("Gesture.KeyTap.MinDownVelocity", 5.0f);
            controller.config().setFloat("Gesture.KeyTap.HistorySeconds", .8f);
            controller.config().setFloat("Gesture.KeyTap.MinDistance", 20.0f);
            controller.config().save();     
        }

     }

文件#2

    package com.leaptoarduino;
    import com.leapmotion.leap.*;
    import com.leapmotion.leap.Gesture.Type;
    import com.leapmotion.leap.KeyTapGesture;
    public class LeapdListener extends Listener
    {
    //Serial port that will be used to communicate with the Arduino
    private RS232Protocol serial;
    //Constructor
    public LeapdListener(RS232Protocol serial)
    {
        this.serial = serial;
    } 
    //Member function: onInit
    public void onInit(Controller controller)
    {
        System.out.println("Initialized");
    }
    //Member function: onConncect
    public void onConnect(Controller controller)
    {
       System.out.println("Connected");
    } 
    //Member Function: onDisconnect
    public void onDisconnect(Controller controller)
    {
       System.out.println("Disconnected");
    } 
    //Member Function: onExit
    public void onExit(Controller controller)
    {
       System.out.println("Exited");
    } 
    //Member Function: onFrame
    public void onFrame(Controller controller)
    {

        //Get the most recent frame
        Frame frame = controller.frame();
        //Verify a hand is in view
        if (frame.hands().count() > 0)
        {
            GestureList gestures = frame.gestures();
            for (Gesture gesture: gestures)
            {
                if (gesture.type() == Type.TYPE_KEY_TAP)
                {
                    KeyTapGesture keytap = new KeyTapGesture (gesture);
                    System.out.println("KEYTAPPED");
                }
            }

            //Send the tap data to the Arduino
            // TBD
            //Give the Arduino some time to process our data
            try { Thread.sleep(30); }
            catch (InterruptedException e) { e.printStackTrace(); }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

当您在OnFrame处理程序中休眠线程时,您将错过Leap Motion服务中的帧。部分问题可能是在那些错过的帧中识别出轻击手势。 Frame.gestures()采用" sinceFrame"用于避免此问题的参数:frame.gestures(sinceFrame)为您提供在sinceFrame和当前帧之间发生的所有手势,因此您不会在丢弃或跳过帧时丢失任何手势。您所做的是每次运行onFrame处理程序时将当前帧保存到lastFrameProcessed变量。您将此lastFrameProcessed变量传递给gestures()以获取完整的手势列表。类似的东西:

Frame lastFrameProcessed = Frame.invalid();
public void onFrame(Controller controller)
{

    //Get the most recent frame
    Frame frame = controller.frame();
    //Verify a hand is in view
    if (frame.hands().count() > 0)
    {
        GestureList gestures = frame.gestures(lastFrameProcessed);
        for (Gesture gesture: gestures)
        {
            if (gesture.type() == Type.TYPE_KEY_TAP)
            {
                KeyTapGesture keytap = new KeyTapGesture (gesture);
                System.out.println("KEYTAPPED");
            }
        }

        //Send the tap data to the Arduino
        // TBD
        //Give the Arduino some time to process our data
        try { Thread.sleep(30); }
        catch (InterruptedException e) { e.printStackTrace(); }
    }
    lastFrameProcessed = frame;
}