手边还有一台快生锈的leap motion,闲来无事捣鼓下,先来写几篇入门的教程,之后会写点简单好玩的demo~~~
准备工作:
LeapmotionSDK下载:
https://developer.leapmotion.com/get-started?id=v3-developer-beta&platform=windows&version=3.1.3.41910
解压之后双击EXE安装SDK
安装完SDK之后打开App Home,注册登录之后就可以试下最简单的demo感受下效果,如果提示leap motion未连接,可以拔掉重试或者点开桌面右下角leap motion的图标检查故障,如果能正常运行程序那么说明我们的硬件部分没有问题。
下面可以打开Leap_Motion_SDK_Win_3.1.3\LeapSDK\samples目录下的sln文件测试C++能否正常运行,本文用的VS2015,提示版本不兼容的时候直接升版本就好,如果用的VS2010或者VS2012选择对应版本的Sln即可。
下面介绍如何新建一个Leap motion项目配置环境并成功运行示例程序。
类似于opencv的配置环境,我们需要做三件事儿:
1. 把dll添加进环境变量(如果不想添加进环境变量可以把dll拷贝进调试目录)
路径为:C:\Users\zmdsj\Desktop\Leap_Motion_SDK_Win_3.1.3\LeapSDK\lib\x86
2. 添加附加依赖项
项目-属性-连接器-输入-附加依赖项添加Leap.lib
3. 添加包含目录和库目录
包含目录C:\Users\zmdsj\Desktop\Leap_Motion_SDK_Win_3.1.3\LeapSDK\include
库目录:C:\Users\zmdsj\Desktop\Leap_Motion_SDK_Win_3.1.3\LeapSDK\lib
下面新建一个cpp文件将以下代码复制粘贴上即可(就是sample.cpp)
/******************************************************************************\ * Copyright (C) 2012-2016 Leap Motion, Inc. All rights reserved. * * Leap Motion proprietary and confidential. Not for distribution. * * Use subject to the terms of the Leap Motion SDK Agreement available at * * https://developer.leapmotion.com/sdk_agreement, or another agreement * * between Leap Motion and you, your company or other organization. * \******************************************************************************/ #include <iostream> #include <cstring> #include "Leap.h" using namespace Leap; class SampleListener : public Listener { public: virtual void onInit(const Controller&); virtual void onConnect(const Controller&); virtual void onDisconnect(const Controller&); virtual void onExit(const Controller&); virtual void onFrame(const Controller&); virtual void onFocusGained(const Controller&); virtual void onFocusLost(const Controller&); virtual void onDeviceChange(const Controller&); virtual void onServiceConnect(const Controller&); virtual void onServiceDisconnect(const Controller&); virtual void onServiceChange(const Controller&); virtual void onDeviceFailure(const Controller&); virtual void onLogMessage(const Controller&, MessageSeverity severity, int64_t timestamp, const char* msg); }; const std::string fingerNames[] = { "Thumb", "Index", "Middle", "Ring", "Pinky" }; const std::string boneNames[] = { "Metacarpal", "Proximal", "Middle", "Distal" }; void SampleListener::onInit(const Controller& controller) { std::cout << "Initialized" << std::endl; } void SampleListener::onConnect(const Controller& controller) { std::cout << "Connected" << std::endl; } void SampleListener::onDisconnect(const Controller& controller) { // Note: not dispatched when running in a debugger. std::cout << "Disconnected" << std::endl; } void SampleListener::onExit(const Controller& controller) { std::cout << "Exited" << std::endl; } void SampleListener::onFrame(const Controller& controller) { // Get the most recent frame and report some basic information const Frame frame = controller.frame(); std::cout << "Frame id: " << frame.id() << ", timestamp: " << frame.timestamp() << ", hands: " << frame.hands().count() << ", extended fingers: " << frame.fingers().extended().count() << std::endl; HandList hands = frame.hands(); for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) { // Get the first hand const Hand hand = *hl; std::string handType = hand.isLeft() ? "Left hand" : "Right hand"; std::cout << std::string(2, ' ') << handType << ", id: " << hand.id() << ", palm position: " << hand.palmPosition() << std::endl; // Get the hand's normal vector and direction const Vector normal = hand.palmNormal(); const Vector direction = hand.direction(); // Calculate the hand's pitch, roll, and yaw angles std::cout << std::string(2, ' ') << "pitch: " << direction.pitch() * RAD_TO_DEG << " degrees, " << "roll: " << normal.roll() * RAD_TO_DEG << " degrees, " << "yaw: " << direction.yaw() * RAD_TO_DEG << " degrees" << std::endl; // Get the Arm bone Arm arm = hand.arm(); /* std::cout << std::string(2, ' ') << "Arm direction: " << arm.direction() << " wrist position: " << arm.wristPosition() << " elbow position: " << arm.elbowPosition() << std::endl; */ // Get fingers const FingerList fingers = hand.fingers(); for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) { const Finger finger = *fl; /* std::cout << std::string(4, ' ') << fingerNames[finger.type()] << " finger, id: " << finger.id() << ", length: " << finger.length() << "mm, width: " << finger.width() << std::endl; */ // Get finger bones for (int b = 0; b < 4; ++b) { Bone::Type boneType = static_cast<Bone::Type>(b); Bone bone = finger.bone(boneType); /* std::cout << std::string(6, ' ') << boneNames[boneType] << " bone, start: " << bone.prevJoint() << ", end: " << bone.nextJoint() << ", direction: " << bone.direction() << std::endl; */ } } } if (!frame.hands().isEmpty()) { std::cout << std::endl; } } void SampleListener::onFocusGained(const Controller& controller) { std::cout << "Focus Gained" << std::endl; } void SampleListener::onFocusLost(const Controller& controller) { std::cout << "Focus Lost" << std::endl; } void SampleListener::onDeviceChange(const Controller& controller) { std::cout << "Device Changed" << std::endl; const DeviceList devices = controller.devices(); for (int i = 0; i < devices.count(); ++i) { std::cout << "id: " << devices[i].toString() << std::endl; std::cout << " isStreaming: " << (devices[i].isStreaming() ? "true" : "false") << std::endl; std::cout << " isSmudged:" << (devices[i].isSmudged() ? "true" : "false") << std::endl; std::cout << " isLightingBad:" << (devices[i].isLightingBad() ? "true" : "false") << std::endl; } } void SampleListener::onServiceConnect(const Controller& controller) { std::cout << "Service Connected" << std::endl; } void SampleListener::onServiceDisconnect(const Controller& controller) { std::cout << "Service Disconnected" << std::endl; } void SampleListener::onServiceChange(const Controller& controller) { std::cout << "Service Changed" << std::endl; } void SampleListener::onDeviceFailure(const Controller& controller) { std::cout << "Device Error" << std::endl; const Leap::FailedDeviceList devices = controller.failedDevices(); for (FailedDeviceList::const_iterator dl = devices.begin(); dl != devices.end(); ++dl) { const FailedDevice device = *dl; std::cout << " PNP ID:" << device.pnpId(); std::cout << " Failure type:" << device.failure(); } } void SampleListener::onLogMessage(const Controller&, MessageSeverity s, int64_t t, const char* msg) { switch (s) { case Leap::MESSAGE_CRITICAL: std::cout << "[Critical]"; break; case Leap::MESSAGE_WARNING: std::cout << "[Warning]"; break; case Leap::MESSAGE_INFORMATION: std::cout << "[Info]"; break; case Leap::MESSAGE_UNKNOWN: std::cout << "[Unknown]"; } std::cout << "[" << t << "] "; std::cout << msg << std::endl; } int main(int argc, char** argv) { // Create a sample listener and controller SampleListener listener; Controller controller; // Have the sample listener receive events from the controller controller.addListener(listener); if (argc > 1 && strcmp(argv[1], "--bg") == 0) controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES); controller.setPolicy(Leap::Controller::POLICY_ALLOW_PAUSE_RESUME); // Keep this process running until Enter is pressed std::cout << "Press Enter to quit, or enter 'p' to pause or unpause the service..." << std::endl; bool paused = false; while (true) { char c = std::cin.get(); if (c == 'p') { paused = !paused; controller.setPaused(paused); std::cin.get(); //skip the newline } else break; } // Remove the sample listener when done controller.removeListener(listener); return 0; }
结果如图:
手指部分太多我就先注释掉了,这样看上去清爽一点,233
最后祝大家编程愉快~