我試圖在Mac上啓動我自己的Leap Motion項目,但我遇到了一些問題。如何將Leap Motion控制器集成到Mac應用程序中?
我想爲這個項目使用Objective-C,並且我已經讀過這個語言有一個Leap Motion庫。但是,我不確定如何將使用此庫的Leap Motion控件集成到Mac應用程序中。
類似的東西被問到here,只有他們問使用Python Leap Motion庫。
如何將Leap Motion控件添加到Objective-C Mac應用程序?
我試圖在Mac上啓動我自己的Leap Motion項目,但我遇到了一些問題。如何將Leap Motion控制器集成到Mac應用程序中?
我想爲這個項目使用Objective-C,並且我已經讀過這個語言有一個Leap Motion庫。但是,我不確定如何將使用此庫的Leap Motion控件集成到Mac應用程序中。
類似的東西被問到here,只有他們問使用Python Leap Motion庫。
如何將Leap Motion控件添加到Objective-C Mac應用程序?
我最近做了這個,所以我可以提供用於將Leap Motion控件添加到Mac應用程序的步驟。實際上,如果您想要使用示例,則啓用了Leap的Molecules應用程序的源代碼是available on GitHub。您需要構建的是Leap SDK。
Leap Motion Objective-C頭文件基本上都是圍繞它們的底層C++ API封裝的,但您不需要關心它,因爲您只能通過Objective-C訪問它。
要將庫添加到您的項目中,請首先在您的系統中的某處安裝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 Motion控制器獲取輸入。中央的LeapController對象是爲Leap Motion事件提供委託回調的東西。您設置一個使用如下代碼:
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 Motion控制器時觸發它。
您會花大部分時間在-onFrame:
回調中,因爲這是您獲取定位更新的位置。你得到你的LeapController回自該通知的對象,你可以使用提取當前幀數據如下:
LeapController *aController = (LeapController *)[notification object];
LeapFrame *frame = [aController frame:0];
的LeapFrame具有內所有由大躍進現場觀察數據,包括手和手指位置以及這些的整體縮放比例。 LeapFrame上的-hands
方法爲您提供了所有檢測到的LeapHand對象的數組。反過來,你從它的-fingers
方法中獲得LeapHand的每個LeapFinger。您還可以提取palmPosition
,palmNormal
和整體direction
的手。
方向提供爲LeapVectors,它是圍繞3D矢量的包裝對象。您可以從中提取X,Y和Z分量,或執行矢量操作或其他LeapVectors之間的比較。
在分子中,我通過讀入相對於屏幕的進出或從左到右的運動來調整分子結構的尺度和方向。我通過比較從一幀到下一幀的開放手的位置來做到這一點。我店前一幀,然後做現在,我們看到使用這一手最後一次之間的比較
LeapVector *handTranslation = [firstHand translation:previousLeapFrame];
您也可以在幀之間比較縮放,旋轉等,爲手或在幀中的所有對象一組。從我在上面代碼中獲得的幀到幀的轉換中,我可以提取單個的X,Y和Z分量來旋轉我的模型,以響應基於Z的X和Y平移和縮放。
I對我的應用程序使用總體定位,但通過追蹤個別指尖,您顯然可以做得更好。在設備上啓動並運行應用程序之後,我建議閱讀Objective-C的Leap SDK頭文件,以瞭解還有哪些其他功能會暴露給您。另外,還要嘗試不同的交互模式,因爲您可能會對三維空間中的哪些方法和哪些方面無法正常工作感到驚訝。
@布拉德拉森的回答幾乎涵蓋了一切,所以請參閱它以獲取更多詳細信息。但是,他的答案顯示瞭如何使用使用NSNotifications工作的LeapListener協議。
如果你不喜歡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");
}
我一直沒有料到我的問題是爲積極的迴應而制定的。 – CTSchmidt
很好的答案!請注意,如果您的項目使用libC++,則可以在Leap Motion SDK的lib/libC++文件夾中使用libLeap.dylib的版本。 –