我正试图在Mac上启动自己的Leap项目,但我遇到了一些问题。
我想在这个项目中使用Objective,而且我已经读过这个语言有一个Leap运动库。但是,我不知道如何使用这个库将Leap控件集成到Mac应用程序中。
类似的问题被问到了这里,只是他们使用的是Python库。
如何将Leap控件添加到Objective应用程序中?
发布于 2013-07-30 02:19:24
我最近就这样做了,所以我可以提供用于将Leap Motion控件添加到Mac应用程序中的步骤。事实上,如果您想要使用一个示例,那么我的支持跃变分子应用程序的源代码是可在GitHub上获得。构建它所需要的只是Leap SDK。
头基本上是它们的底层C++ API的包装器,但是您不需要关心这一点,因为您只能通过Objective访问它。
要将库添加到项目中,首先在系统中的某个地方安装Leap SDK。然后,在项目中添加对Leap.h、LeapMath.h、LeapObjectiveC.h和LeapObjectiveC.mm文件的引用。将libLeap.dylib库添加到链接库中。
为了避免在测试版期间出现编译器和链接器错误(可能已经解决了),我需要转到构建设置,并将C++ Standard Library更改为libstdc++。
您需要确保Leap库与您的应用程序捆绑在一起,因此确保它在构建阶段被复制到捆绑的框架中。我还需要使用以下运行脚本构建阶段来确保其内部路径设置正确(同样,不确定现在是否需要这样做):
echo TARGET_BUILD_DIR=${TARGET_BUILD_DIR}
echo TARGET_NAME=${TARGET_NAME}
cd "${TARGET_BUILD_DIR}/${TARGET_NAME}.app/Contents/MacOS"
ls -la
# then remap the loader path
install_name_tool -change @loader_path/libLeap.dylib @executable_path/../Resources/libLeap.dylib "${TARGET_NAME}"在设置此选项时,最后一个注意事项是,如果您正在对Mac应用程序进行沙箱包装,则需要启用传出网络连接权限,否则应用程序将无法连接到运行在Mac上的Leap Motion服务器应用程序。
一旦所有设置完成,您就可以开始从Leap运动控制器获得输入。一个中央LeapController对象将为LeapController提供委托回调。您可以使用如下代码设置一个:
controller = [[LeapController alloc] init];
[controller addListener:self];您的委托需要满足LeapListener协议,其中有许多可选的回调方法:
// Controller object has initialized
- (void)onInit:(NSNotification *)notification
// Controller has connected
- (void)onConnect:(NSNotification *)notification;
// Controller has disconnected
- (void)onDisconnect:(NSNotification *)notification;
// Exiting your LeapController object
- (void)onExit:(NSNotification *)notification;
// New frame data has arrived from the controller
- (void)onFrame:(NSNotification *)notification;连接和断开连接回调是显而易见的,尽管您会注意到,在用Xcode调试应用程序时从未触发过断开连接回调。您需要在调试器之外运行应用程序,以便在断开Leap运动控制器连接时启动该应用程序。
您将花费大部分时间在-onFrame:回调中,因为这是您获得定位更新的地方。您可以从中获取LeapController作为通知对象,并且可以使用以下方法提取当前帧数据:
LeapController *aController = (LeapController *)[notification object];
LeapFrame *frame = [aController frame:0];LeapFrame有所有的场景数据观察到的飞跃,包括手和手指的位置以及这些的整体缩放。-hands方法在LeapFrame上为您提供了所有检测到的LeapHand对象的数组。反过来,您可以从其LeapFinger方法中获取LeapHand的每个-fingers。您还可以提取手的palmPosition、palmNormal和总体direction。
方向以LeapVectors的形式提供,它是围绕三维矢量的包装对象.您可以从它们中提取X、Y和Z组件,或者执行向量操作或在其他LeapVectors之间进行比较。
在分子中,我通过阅读相对于屏幕的运动来调整分子结构的大小和方向。我通过比较一只张开的手从一个框架到另一个框架的位置来做到这一点。我存储上一个框架,然后比较一下我们最后一次看到这只手使用
LeapVector *handTranslation = [firstHand translation:previousLeapFrame];您还可以将手或帧中所有对象的比例尺、旋转等作为一个组进行比较。从上述代码中获得的帧到帧转换中,我可以提取单个的X、Y和Z组件,以响应于X和Y的平移以及基于Z的缩放而旋转我的模型。
我在我的应用程序中使用了总体定位,但是很明显,通过跟踪单个指尖,您可以进行更好的定位。在应用程序与设备一起运行之后,我建议阅读Objective端的Leap头文件,以了解其他功能是否向您公开。另外,用不同的交互方式进行实验,因为你可能会惊讶于什么在三维空间中起作用和不起作用。
发布于 2013-08-09 17:17:06
@布拉德·拉森的回答几乎涵盖了一切,更多细节请参考。然而,他的回答显示了如何使用LeapListener协议,该协议使用NSNotifications。
如果您不喜欢NSNotificationCenter :p,或者不关心更新发生在哪个线程上,下面是如何使用LeapDelegate的示例。
@interface MyClass () < LeapDelegate >
@property (strong, nonatomic) LeapController *controller;
@end
- (void)startLeapMotion
{
if (!_controller) {
_controller = [[LeapController alloc] initWithDelegate:self];
//
// Could also be...
//
// [_controller addDelegate:self];
// [_controller removeDelegate];
}
}
- (void)onInit:(LeapController *)controller
{
NSLog(@"Init");
}
- (void)onConnect:(LeapController *)controller
{
NSLog(@"Connect");
// Some settings that came bundled with the Leap sample project. Use if needed
//
// [controller setPolicyFlags:LEAP_POLICY_DEFAULT];
// [controller enableGesture:LEAP_GESTURE_TYPE_CIRCLE enable:YES];
// [controller enableGesture:LEAP_GESTURE_TYPE_KEY_TAP enable:YES];
// [controller enableGesture:LEAP_GESTURE_TYPE_SCREEN_TAP enable:YES];
// [controller enableGesture:LEAP_GESTURE_TYPE_SWIPE enable:YES];
}
- (void)onFocusGained:(LeapController *)controller
{
NSLog(@"Focus Gained");
}
- (void)onFrame:(LeapController *)controller
{
// Write awesome code here!!!
NSLog(@"Frame");
}
- (void)onFocusLost:(LeapController *)controller
{
NSLog(@"Focus Lost");
}
- (void)onDisconnect:(LeapController *)controller
{
NSLog(@"Disconnected");
}
- (void)onExit:(LeapController *)controller
{
NSLog(@"Exited");
}https://stackoverflow.com/questions/17910264
复制相似问题