如何在空中控制计算机

电子说

1.3w人已加入

描述

第1步:所需材料

跳跃Motion支持Python,Java,C ++,C#,Javascript。在本教程中,我们将使用Java。

因此,在开始制作程序之前,我们需要查看此构建所需的材料:

1。飞跃运动控制器(在此处购买)

2。面向Java开发人员的Eclipse IDE(在此处下载)

3。 Leap Motion设置文件(在此处下载)

4。 Leap Motion SDK(在此处下载)

5。 Java SE 7(在此处下载)(下载JDK和JRE )

( * Leap Motion库仅适用于Java 6或Java 7 )

如果您不知道如何设置eclipse,那么我推荐此Java系列入门教程:在此处观看

好,现在让我们转到涉及的步骤创建程序。

步骤2:软件设置

控制器

控制器

控制器

控制器

跳跃运动支持多种编程语言,例如C ++,C#,Python,Java和Javascript。

在本教程中,我们将使用Java进行编程。

初始设置:

1。首先转到https://www.leapmotion.com/setup并下载Leap Motion设置文件,然后安装该应用程序。它会安装所有内容,以将the动控制器连接到您的计算机。

2。接下来转到https://developer.leapmotion.com/(开发人员门户)并创建一个帐户。然后下载Leap Motion SDK。

3。然后将zip文件中的所有文件提取到您选择的任何文件夹中。

配置类路径:

在此本教程将使用面向Java开发人员的Eclipse IDE。

1。打开Eclipse,然后单击File》 New》 Java Project。

2。为项目命名,并确保将执行运行时环境JRE设置为Java SE 1.7,如图所示。

3。然后单击“下一步”,打开“库”选项卡,然后选择“添加外部JAR”并导航到先前提取的文件夹。

4。然后打开LeapSDK文件夹并转到libs文件夹并选择LeapJava.jar文件。

5。接下来,单击LeapJava.jar旁边的三角形下拉按钮,然后从“下拉菜单”中单击“本机库位置”,然后单击“编辑”,如图所示。

6。然后单击“外部文件夹”,然后导航到LeapSDK文件夹》 lib》,然后根据您的操作系统选择 x64 或 x86 文件夹,然后单击“确定”,然后单击“完成”。

我们现在在我们的项目中安装了LeapMotion,现在开始进行一些编码!

第3步:跳跃运动监听器

控制器

Now that we are done importing the libraries , let us start working on the project.

初始设置:

1。首先,我们需要在源文件夹中创建一个新类,单击项目文件夹旁边的三角形下拉箭头。

2。在您的项目文件夹中,右键单击src文件夹,转到new》,然后单击class创建一个新的Java类。

3。给班级起个名字,然后单击完成。

现在,让我们从代码开始:

1。首先,我们创建一个Leap Motion侦听器:(注意:导入必要的库),侦听的侦听器是何时将Jump Motion控制器连接到计算机的。

package starkmouse;

import java.io.IOException;

import java.awt.AWTException;

import java.awt.GraphicsDevice;

import java.awt.GraphicsEnvironment;

import java.awt.MouseInfo;

import java.awt.Point;

import java.awt.Robot;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

import com.leapmotion.leap.*;

import com.leapmotion.leap.Controller.PolicyFlag;

public class leapmoues {

public static void main(String[] args) throws AWTException {

Controller controller = new Controller();

controller.setPolicyFlags(PolicyFlag.POLICY_BACKGROUND_FRAMES);

SampleListener listener = new SampleListener();

controller.addListener(listener);

// controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);

// controller.enableGesture(Gesture.Type.TYPE_SWIPE);

controller.enableGesture(Gesture.Type.TYPE_CIRCLE);

System.out.println(“Press Enter to quit.。.”);

try {

System.in.read();

} catch (IOException e) {

e.printStackTrace();

}

controller.removeListener(listener);

}

}

class SampleListener extends Listener {

boolean readyForControl = false;

int screenWidth;

int screenHeight;

boolean iBoxGet = false;

InteractionBox iBox = null;

Robot robot;

boolean isMoving = false;

boolean unGrip = false;

boolean wasFacingDown = true;

boolean wasInTabState = false;

boolean wasTabbing = false;

boolean justCircleGestured = false;

boolean isResizing = false;

public void onConnect(Controller controller) {

System.out.println(“Connected”);

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()

.getDefaultScreenDevice();

screenWidth = gd.getDisplayMode().getWidth();

screenHeight = gd.getDisplayMode().getHeight();

System.out.println(“Screen Resolution: X: ” + screenWidth + “, H: ”

+ screenHeight);

readyForControl = true;

try {

robot = new Robot();

robot.setAutoDelay(5);

} catch (AWTException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

步骤4:帧和手势数据

控制器

跳跃运动帧数据,其中包括手势(圆形,捏,掌面朝下和所有其他手势)

跳跃运动有两个红外摄像机和三个红外LED。跳跃运动使用三个IR Led形成IR点图案,从而形成3D数据收集,IR相机拍摄称为帧的图片。帧从跳跃运动设备发送到计算机,我们可以对其进行编程。

跳跃运动使用复杂的数学分析图片和点定位,然后比较红外拍摄的两张图片的数据相机并使用两个2D表示形式获得3D表示形式。

跳跃运动设备以300 fps的速度捕获。然后,我们对发送到计算机的每个帧进行处理。

代码:

public void onFrame(Controller controller) {

Frame frame = controller.frame(); // The latest frame

// Frame previous = controller.frame(1); //The previous frame

// System.out.println(“Frame available”);

if (!iBoxGet) {

iBox = frame.interactionBox();

iBoxGet = true;

System.out.println(“Interaction box set!”);

}

// Pointable furthestFront = frame.pointables().frontmost();

Hand rightHand = frame.hands().rightmost();

Vector palmV = rightHand.palmVelocity();

// System.out.println(“Velocity: X: ” + palmV.getX() + “, Y: ” +

// palmV.getY()

// + “, Z: ” + palmV.getZ());

Vector palmN = rightHand.palmNormal();

// System.out.println(“Normal: X: ” + palmN.getX() + “, Y: ”

// + palmN.getY() + “, Z: ” + palmN.getZ());

Point mouseLoc = MouseInfo.getPointerInfo().getLocation();

int currentMouseX = mouseLoc.x;

int currentMouseY = mouseLoc.y;

if (readyForControl && rightHand.confidence() 》 .15) {

if (!isMoving && !wasInTabState && frame.hands().count() 》 1) {

Hand leftHand = frame.hands().leftmost();

if (leftHand.pinchStrength() 》 .8

&& rightHand.pinchStrength() 》 .8) {

if (!isResizing) {

System.out.println(“Resizing.。.”);

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_S);

robot.keyRelease(KeyEvent.VK_S);

robot.keyPress(KeyEvent.VK_DOWN);

robot.keyPress(KeyEvent.VK_RIGHT);

robot.keyRelease(KeyEvent.VK_DOWN);

robot.keyRelease(KeyEvent.VK_RIGHT);

isResizing = true;

}

}else{

if(isResizing){

System.out.println(“Resizing complete!”);

robot.mousePress(InputEvent.BUTTON1_MASK);

robot.mouseRelease(InputEvent.BUTTON1_MASK);

isResizing = false;

}

}

}

// System.out.println(“Confidence: ” + rightHand.confidence());

if (rightHand.grabStrength() 》 .99 && !wasInTabState && !isResizing) {

if (!isMoving && palmN.getY() 《 .8) {

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_R);

robot.keyRelease(KeyEvent.VK_R);

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_M);

robot.keyRelease(KeyEvent.VK_M);

robot.keyPress(KeyEvent.VK_DOWN);

robot.keyRelease(KeyEvent.VK_DOWN);

isMoving = true;

}

// System.out.println(rightHand.grabStrength());

}

else {

// System.out.println(“Not grabbing”);

if (isMoving) {

robot.mousePress(InputEvent.BUTTON1_MASK);

robot.mouseRelease(InputEvent.BUTTON1_MASK);

isMoving = false;

if (palmN.getX() != 0 && palmN.getY() != 0 && palmN.getZ() != 0) {

if (palmN.getY() 《 -.1 && palmN.getZ() 》 -.8) {

if (currentMouseY 《= 8) {

robot.keyPress(KeyEvent.VK_WINDOWS);

robot.keyPress(KeyEvent.VK_UP);

robot.keyRelease(KeyEvent.VK_WINDOWS);

robot.keyRelease(KeyEvent.VK_UP);

}

else {

if (screenWidth - currentMouseX 《= 12) {

robot.keyPress(KeyEvent.VK_WINDOWS);

robot.keyPress(KeyEvent.VK_RIGHT);

robot.keyRelease(KeyEvent.VK_WINDOWS);

robot.keyRelease(KeyEvent.VK_RIGHT);

} else if (currentMouseX 《= 12) {

robot.keyPress(KeyEvent.VK_WINDOWS);

robot.keyPress(KeyEvent.VK_LEFT);

robot.keyRelease(KeyEvent.VK_WINDOWS);

robot.keyRelease(KeyEvent.VK_LEFT);

}

}

} else {

System.out.println(“Normal: X: ” + palmN.getX()

+ “, Y: ” + palmN.getY() + “, Z: ”

+ palmN.getZ());

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_N);

robot.keyRelease(KeyEvent.VK_N);

}

}

}

}

if (!isMoving && !isResizing) {

if (palmN.getY() 《 -.8 && palmN.getZ() 》 -.5) {

wasFacingDown = true;

wasTabbing = false;

if (wasInTabState) {

robot.keyPress(KeyEvent.VK_ENTER);

robot.keyRelease(KeyEvent.VK_ENTER);

wasInTabState = false;

}

} else if (palmN.getY() 》= .8 && wasFacingDown

&& !wasInTabState) {

System.out.println(“Alt tabbing”);

wasFacingDown = false;

wasInTabState = true;

wasTabbing = false;

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_CONTROL);

robot.keyPress(KeyEvent.VK_TAB);

robot.delay(100);

robot.keyRelease(KeyEvent.VK_TAB);

robot.keyRelease(KeyEvent.VK_CONTROL);

robot.keyRelease(KeyEvent.VK_ALT);

try {

Runtime.getRuntime().exec(

“cmd /c start ” + “C:WindowSwitcher.lnk”);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

robot.delay(300);

} else if (wasInTabState && !wasFacingDown && !wasTabbing

&& palmN.getY() 《 .45) {

wasTabbing = true;

} else if (wasInTabState && !wasFacingDown && wasTabbing

&& palmN.getY() 》 .75) {

robot.keyPress(KeyEvent.VK_TAB);

robot.keyRelease(KeyEvent.VK_TAB);

wasTabbing = false;

}

}

/*

* if (!isMoving && !wasInTabState) { /* if(palmN.getZ() 《= -.7 &&

* rightHand.grabStrength() 《 .1){

* System.out.println(“Palm vertical velocity: ” +

* rightHand.palmVelocity().getY()); //float resultVerticalV =

* Math.round(Math.abs(rightHand.palmVelocity().getY()) - 1);

* //if(resultVerticalV 》 0){ robot.mouseWheel((int)

* Math.round(((rightHand.palmVelocity().getY()) / 500))); //}

* }else{

*/

if (!isMoving && !wasInTabState && frame.gestures().count() 》 0

&& frame.hands().count() == 1 && !isResizing) {

CircleGesture circleGesture = new CircleGesture(frame

.gestures().get(0));

// System.out.println(“Pinch strength: ” +

// rightHand.pinchStrength());

if (circleGesture.durationSeconds() 》 .5 && !justCircleGestured

&& rightHand.pinchStrength() 》 .8) {

System.out.println(“Closed a window!”);

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_F4);

robot.keyRelease(KeyEvent.VK_F4);

robot.keyRelease(KeyEvent.VK_ALT);

justCircleGestured = true;

}

} else {

justCircleGestured = false;

}

float xSpeed = (palmV.getX() / 6);

float ySpeed = (palmV.getY() / 6);

// System.out.println(“xSpeed: ” + xSpeed + “, ySpeed: ” + ySpeed);

robot.mouseMove((int) (currentMouseX + xSpeed),

(int) (currentMouseY - ySpeed));

// }

}

}

}

步骤5:完成代码!

控制器

package starkmouse;

import java.io.IOException;

import java.awt.AWTException;

import java.awt.GraphicsDevice;

import java.awt.GraphicsEnvironment;

import java.awt.MouseInfo;

import java.awt.Point;

import java.awt.Robot;

import java.awt.event.InputEvent;

import java.awt.event.KeyEvent;

import com.leapmotion.leap.*;

import com.leapmotion.leap.Controller.PolicyFlag;

public class leapmoues {

public static void main(String[] args) throws AWTException {

Controller controller = new Controller();

controller.setPolicyFlags(PolicyFlag.POLICY_BACKGROUND_FRAMES);

SampleListener listener = new SampleListener();

controller.addListener(listener);

// controller.enableGesture(Gesture.Type.TYPE_SCREEN_TAP);

// controller.enableGesture(Gesture.Type.TYPE_SWIPE);

controller.enableGesture(Gesture.Type.TYPE_CIRCLE);

System.out.println(“Press Enter to quit.。.”);

try {

System.in.read();

} catch (IOException e) {

e.printStackTrace();

}

controller.removeListener(listener);

}

}

class SampleListener extends Listener {

boolean readyForControl = false;

int screenWidth;

int screenHeight;

boolean iBoxGet = false;

InteractionBox iBox = null;

Robot robot;

boolean isMoving = false;

boolean unGrip = false;

boolean wasFacingDown = true;

boolean wasInTabState = false;

boolean wasTabbing = false;

boolean justCircleGestured = false;

boolean isResizing = false;

public void onConnect(Controller controller) {

System.out.println(“Connected”);

GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()

.getDefaultScreenDevice();

screenWidth = gd.getDisplayMode().getWidth();

screenHeight = gd.getDisplayMode().getHeight();

System.out.println(“Screen Resolution: X: ” + screenWidth + “, H: ”

+ screenHeight);

readyForControl = true;

try {

robot = new Robot();

robot.setAutoDelay(5);

} catch (AWTException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void onFrame(Controller controller) {

Frame frame = controller.frame(); // The latest frame

// Frame previous = controller.frame(1); //The previous frame

// System.out.println(“Frame available”);

if (!iBoxGet) {

iBox = frame.interactionBox();

iBoxGet = true;

System.out.println(“Interaction box set!”);

}

// Pointable furthestFront = frame.pointables().frontmost();

Hand rightHand = frame.hands().rightmost();

Vector palmV = rightHand.palmVelocity();

// System.out.println(“Velocity: X: ” + palmV.getX() + “, Y: ” +

// palmV.getY()

// + “, Z: ” + palmV.getZ());

Vector palmN = rightHand.palmNormal();

// System.out.println(“Normal: X: ” + palmN.getX() + “, Y: ”

// + palmN.getY() + “, Z: ” + palmN.getZ());

Point mouseLoc = MouseInfo.getPointerInfo().getLocation();

int currentMouseX = mouseLoc.x;

int currentMouseY = mouseLoc.y;

if (readyForControl && rightHand.confidence() 》 .15) {

if (!isMoving && !wasInTabState && frame.hands().count() 》 1) {

Hand leftHand = frame.hands().leftmost();

if (leftHand.pinchStrength() 》 .8

&& rightHand.pinchStrength() 》 .8) {

if (!isResizing) {

System.out.println(“Resizing.。.”);

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_S);

robot.keyRelease(KeyEvent.VK_S);

robot.keyPress(KeyEvent.VK_DOWN);

robot.keyPress(KeyEvent.VK_RIGHT);

robot.keyRelease(KeyEvent.VK_DOWN);

robot.keyRelease(KeyEvent.VK_RIGHT);

isResizing = true;

}

}else{

if(isResizing){

System.out.println(“Resizing complete!”);

robot.mousePress(InputEvent.BUTTON1_MASK);

robot.mouseRelease(InputEvent.BUTTON1_MASK);

isResizing = false;

}

}

}

// System.out.println(“Confidence: ” + rightHand.confidence());

if (rightHand.grabStrength() 》 .99 && !wasInTabState && !isResizing) {

if (!isMoving && palmN.getY() 《 .8) {

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_R);

robot.keyRelease(KeyEvent.VK_R);

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_M);

robot.keyRelease(KeyEvent.VK_M);

robot.keyPress(KeyEvent.VK_DOWN);

robot.keyRelease(KeyEvent.VK_DOWN);

isMoving = true;

}

// System.out.println(rightHand.grabStrength());

} else {

// System.out.println(“Not grabbing”);

if (isMoving) {

robot.mousePress(InputEvent.BUTTON1_MASK);

robot.mouseRelease(InputEvent.BUTTON1_MASK);

isMoving = false;

if (palmN.getX() != 0 && palmN.getY() != 0

&& palmN.getZ() != 0) {

if (palmN.getY() 《 -.1 && palmN.getZ() 》 -.8) {

if (currentMouseY 《= 8) {

robot.keyPress(KeyEvent.VK_WINDOWS);

robot.keyPress(KeyEvent.VK_UP);

robot.keyRelease(KeyEvent.VK_WINDOWS);

robot.keyRelease(KeyEvent.VK_UP);

} else {

if (screenWidth - currentMouseX 《= 12) {

robot.keyPress(KeyEvent.VK_WINDOWS);

robot.keyPress(KeyEvent.VK_RIGHT);

robot.keyRelease(KeyEvent.VK_WINDOWS);

robot.keyRelease(KeyEvent.VK_RIGHT);

} else if (currentMouseX 《= 12) {

robot.keyPress(KeyEvent.VK_WINDOWS);

robot.keyPress(KeyEvent.VK_LEFT);

robot.keyRelease(KeyEvent.VK_WINDOWS);

robot.keyRelease(KeyEvent.VK_LEFT);

}

}

} else {

System.out.println(“Normal: X: ” + palmN.getX()

+ “, Y: ” + palmN.getY() + “, Z: ”

+ palmN.getZ());

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_SPACE);

robot.keyRelease(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_N);

robot.keyRelease(KeyEvent.VK_N);

}

}

}

}

if (!isMoving && !isResizing) {

if (palmN.getY() 《 -.8 && palmN.getZ() 》 -.5) {

wasFacingDown = true;

wasTabbing = false;

if (wasInTabState) {

robot.keyPress(KeyEvent.VK_ENTER);

robot.keyRelease(KeyEvent.VK_ENTER);

wasInTabState = false;

}

} else if (palmN.getY() 》= .8 && wasFacingDown

&& !wasInTabState) {

System.out.println(“Alt tabbing”);

wasFacingDown = false;

wasInTabState = true;

wasTabbing = false;

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_CONTROL);

robot.keyPress(KeyEvent.VK_TAB);

robot.delay(100);

robot.keyRelease(KeyEvent.VK_TAB);

robot.keyRelease(KeyEvent.VK_CONTROL);

robot.keyRelease(KeyEvent.VK_ALT);

try {

Runtime.getRuntime().exec(

“cmd /c start ” + “C:WindowSwitcher.lnk”);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

robot.delay(300);

} else if (wasInTabState && !wasFacingDown && !wasTabbing

&& palmN.getY() 《 .45) {

wasTabbing = true;

} else if (wasInTabState && !wasFacingDown && wasTabbing

&& palmN.getY() 》 .75) {

robot.keyPress(KeyEvent.VK_TAB);

robot.keyRelease(KeyEvent.VK_TAB);

wasTabbing = false;

}

}

/*

* if (!isMoving && !wasInTabState) { /* if(palmN.getZ() 《= -.7 &&

* rightHand.grabStrength() 《 .1){

* System.out.println(“Palm vertical velocity: ” +

* rightHand.palmVelocity().getY()); //float resultVerticalV =

* Math.round(Math.abs(rightHand.palmVelocity().getY()) - 1);

* //if(resultVerticalV 》 0){ robot.mouseWheel((int)

* Math.round(((rightHand.palmVelocity().getY()) / 500))); //}

* }else{

*/

if (!isMoving && !wasInTabState && frame.gestures().count() 》 0

&& frame.hands().count() == 1 && !isResizing) {

CircleGesture circleGesture = new CircleGesture(frame

.gestures().get(0));

// System.out.println(“Pinch strength: ” +

// rightHand.pinchStrength());

if (circleGesture.durationSeconds() 》 .5 && !justCircleGestured

&& rightHand.pinchStrength() 》 .8) {

System.out.println(“Closed a window!”);

robot.keyPress(KeyEvent.VK_ALT);

robot.keyPress(KeyEvent.VK_F4);

robot.keyRelease(KeyEvent.VK_F4);

robot.keyRelease(KeyEvent.VK_ALT);

justCircleGestured = true;

}

} else {

justCircleGestured = false;

}

float xSpeed = (palmV.getX() / 6);

float ySpeed = (palmV.getY() / 6);

// System.out.println(“xSpeed: ” + xSpeed + “, ySpeed: ” + ySpeed);

robot.mouseMove((int) (currentMouseX + xSpeed),

(int) (currentMouseY - ySpeed));

// }

}

}

}

步骤6:运行程序和结果:

现在我们已经完成了对Leap Motion控制器的编程,我们可以在Eclipse中运行该程序了。

责任编辑:wv

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分