java - Java 中的 Leap Motion 错过了点击手势

标签 java leap-motion

我编写了一个测试代码来查看我的点击手势是否被 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(); }
        }
    }
}

最佳答案

当您在 OnFrame 处理程序中 hibernate 线程时,您将错过来自 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;
} 

关于java - Java 中的 Leap Motion 错过了点击手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31693994/

相关文章:

java - 绕过对用户名/密码的 JConsole 要求 - 当使用带有 JMX 的 Jaas 自定义登录模块来处理授权和身份验证时

java - 以自然顺序存储集合中的独特元素

javascript - 在其他元素上单击元素

javascript - Websockets 不再对我有用

python - 如何在事件回调之间保持 python 生成器的状态

c# - Unity3D 中的 Leap Motion 手势识别总是返回 TYPEINVALID 和 STATEINVALID

java - 为什么以及如何使用 JShell?

java - 在 Java 中跟踪异常的好应用程序?

java - 使用 JSch 连接到 SFTP 时如何选择网络接口(interface)

python - 我如何开始编写或创建 leap motion 应用程序? (Python)