Leap提供了SDK。但是整合有很多的问题,写博客记录一下;
写一个类:SampleListener.cpp以及头文件SampleListener.h。
这里主要碰到的问题是找不到以及冲突问题;
这里最关键的是用的不能写using namespace Lead。必须写Lead::这种形式。
代码如下:
#include <iostream> #include <cstring> #include "Leap.h" //using namespace Leap; class SampleListener : public Leap::Listener { public: int civiv;//手机识别的标志 void onInit(const Leap:: Controller&); void onConnect(const Leap::Controller&); void onDisconnect(const Leap:: Controller&); void onExit(const Leap:: Controller&); void onFrame(const Leap:: Controller&); void onFocusGained(const Leap:: Controller&); void onFocusLost(const Leap::Controller&); void onDeviceChange(const Leap:: Controller&); void onServiceConnect(const Leap:: Controller&); void onServiceDisconnect(const Leap:: Controller&); void onStart(); private: };
SampleListener.cpp:
#include "stdafx.h" #include "SampleListener.h" using namespace Leap; const std::string fingerNames[] = {"Thumb", "Index", "Middle", "Ring", "Pinky"}; const std::string boneNames[] = {"Metacarpal", "Proximal", "Middle", "Distal"}; const std::string stateNames[] = {"STATE_INVALID", "STATE_START", "STATE_UPDATE", "STATE_END"}; void SampleListener::onInit(const Leap::Controller& controller) { std::cout << "Initialized" << std::endl; } void SampleListener::onConnect(const Leap:: Controller& controller) { std::cout << "Connected" << std::endl; controller.enableGesture(Gesture::TYPE_CIRCLE); controller.enableGesture(Gesture::TYPE_KEY_TAP); controller.enableGesture(Gesture::TYPE_SCREEN_TAP); controller.enableGesture(Gesture::TYPE_SWIPE); } void SampleListener::onDisconnect(const Leap:: Controller& controller) { // Note: not dispatched when running in a debugger. std::cout << "Disconnected" << std::endl; } void SampleListener::onExit(const Leap::Controller& controller) { std::cout << "Exited" << std::endl; } void SampleListener::onFrame(const Leap:: 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() << ", tools: " << frame.tools().count() << ", gestures: " << frame.gestures().count() << std::endl; Leap::HandList hands = frame.hands(); for ( Leap::HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) { // Get the first hand const Leap:: 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 Leap::Vector normal = hand.palmNormal(); const Leap:: Vector direction = hand.direction(); // Calculate the hand's pitch, roll, and yaw angles std::cout << std::string(2, ' ') << "pitch: " << direction.pitch() * Leap::RAD_TO_DEG << " degrees, " << "roll: " << normal.roll() * Leap::RAD_TO_DEG << " degrees, " << "yaw: " << direction.yaw() * Leap:: RAD_TO_DEG << " degrees" << std::endl; // Get the Arm bone Leap::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 Leap::FingerList fingers = hand.fingers(); for ( Leap::FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) { civiv=2; const Leap::Finger finger = *fl; std::cout << std::string(4, ' ') << fingerNames[finger.type()] << " finger, id: " << finger.id() << ", length: " << finger.length() << "mm, " << finger.width() << std::endl; // Get finger bones for (int b = 0; b < 4; ++b) { Leap::Bone::Type boneType = static_cast< Leap::Bone::Type>(b); Leap::Bone bone = finger.bone(boneType); std::cout << std::string(6, ' ') << boneNames[boneType] << " bone, start: " << bone.prevJoint() << ", end: " << bone.nextJoint() << ", direction: " << bone.direction() << std::endl; } } } // Get tools const ToolList tools = frame.tools(); for (ToolList::const_iterator tl = tools.begin(); tl != tools.end(); ++tl) { const Tool tool = *tl; std::cout << std::string(2, ' ') << "Tool, id: " << tool.id() << ", position: " << tool.tipPosition() << ", direction: " << tool.direction() << std::endl; } // Get gestures const GestureList gestures = frame.gestures(); for (int g = 0; g < gestures.count(); ++g) { Gesture gesture = gestures[g]; switch (gesture.type()) { case Gesture::TYPE_CIRCLE: { CircleGesture circle = gesture; std::string clockwiseness; if (circle.pointable().direction().angleTo(circle.normal()) <= PI/2) { clockwiseness = "clockwise"; } else { clockwiseness = "counterclockwise"; } // Calculate angle swept since last frame float sweptAngle = 0; if (circle.state() != Gesture::STATE_START) { CircleGesture previousUpdate = CircleGesture(controller.frame(1).gesture(circle.id())); sweptAngle = (circle.progress() - previousUpdate.progress()) * 2 * PI; } /* std::cout << std::string(2, ' ') << "Circle id: " << gesture.id() << ", state: " << stateNames[gesture.state()] << ", progress: " << circle.progress() << ", radius: " << circle.radius() << ", angle " << sweptAngle * RAD_TO_DEG << ", " << clockwiseness << std::endl;*/ break; } case Gesture::TYPE_SWIPE: { SwipeGesture swipe = gesture; std::cout << std::string(2, ' ') << "Swipe id: " << gesture.id() << ", state: " << stateNames[gesture.state()] << ", direction: " << swipe.direction() << ", speed: " << swipe.speed() << std::endl; break; } case Gesture::TYPE_KEY_TAP: { KeyTapGesture tap = gesture; std::cout << std::string(2, ' ') << "Key Tap id: " << gesture.id() << ", state: " << stateNames[gesture.state()] << ", position: " << tap.position() << ", direction: " << tap.direction()<< std::endl; break; } case Gesture::TYPE_SCREEN_TAP: { ScreenTapGesture screentap = gesture; std::cout << std::string(2, ' ') << "Screen Tap id: " << gesture.id() << ", state: " << stateNames[gesture.state()] << ", position: " << screentap.position() << ", direction: " << screentap.direction()<< std::endl; break; } default: std::cout << std::string(2, ' ') << "Unknown gesture type." << std::endl; break; } } if (!frame.hands().isEmpty() || !gestures.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 Leap::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; } } 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::onStart() { // civiv=1; // Create a sample listener and controller SampleListener listener; listener.civiv=1; Controller controller; //controller.Config.SetFloat ("Gesture.Circle.MinRadius", 10.0f); ............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................. //................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................// controller.Config.SetFloat ("Gesture.Circle.MinArc", .5f); // controller.config().setFloat("Gesture.Circle.MinRadius", 10.0f); // controller.config().setFloat("Gesture.Circle.MinRadius", .5f); // 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); // Keep this process running until Enter is pressed // Remove the sample listener when done controller.removeListener(listener); } /*int main(int argc, char** argv) { // Create a sample listener and controller SampleListener listener; Controller controller; //controller.Config.SetFloat ("Gesture.Circle.MinRadius", 10.0f); // controller.Config.SetFloat ("Gesture.Circle.MinArc", .5f); controller.config().setFloat("Gesture.Circle.MinRadius", 10.0f); controller.config().setFloat("Gesture.Circle.MinRadius", .5f); // 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); // Keep this process running until Enter is pressed std::cout << "Press Enter to quit..." << std::endl; std::cin.get(); // Remove the sample listener when done controller.removeListener(listener); return 0; } */
嵌入师兄的MFC写的程序中:
注意因为与师兄写的变量冲突,所以这里不能写using namespace Lead。必须写Lead::这种形式。
EyeTrackView.h:
#include "TshirtCode.h" #include "TshirtDraw.h" #include "GeneticAlgorithm.cpp" #include "CvvImage.h" #include <deque> #include "shockwaveflash1.h" #include "pupilDetector.h" #include "Device.h" #include "communication.h" #include "DeviceEyeDataAnalyze.h" #include "HardWord.h" #include "SampleListener.h"
EyeTrackView.cpp照常引入EyeTrackView.h。