2012-12-27 32 views
0

我正在寫一個iPhone相機應用程序。當用戶即將拍照時,我想檢查一下iPhone是否在晃動,等待沒有晃動的時刻,然後拍下手機。iOS應用程序檢查iPhone晃動,然後拍照

我該怎麼辦?

+0

防抖複雜得多比你想象的。那麼如何在用戶搖晃iPhone 2秒鐘後拍攝照片。 – iProgrammed

回答

5

Anit-shake功能是一個非常複雜的功能。我認爲這是一些強大的模糊檢測/去除算法和iPhone上的陀螺儀的組合。

您可以先用iPhone調查how to detect motion開始,然後查看可以得到哪種結果。如果這還不夠,請開始查看shift/blur direction detection algorithms。這不是一個微不足道的問題,但是如果有足夠的時間,你可以完成這個任務。希望有助於!

+0

+1對真正模糊的「爲我做的」類型問題提供真正有用的答案。 – mmc

0
// Ensures the shake is strong enough on at least two axes before declaring it a shake. 
// "Strong enough" means "greater than a client-supplied threshold" in G's. 
static BOOL L0AccelerationIsShaking(UIAcceleration* last, UIAcceleration* current, double threshold) { 
    double 
     deltaX = fabs(last.x - current.x), 
     deltaY = fabs(last.y - current.y), 
     deltaZ = fabs(last.z - current.z); 

    return 
     (deltaX > threshold && deltaY > threshold) || 
     (deltaX > threshold && deltaZ > threshold) || 
     (deltaY > threshold && deltaZ > threshold); 
} 

@interface L0AppDelegate : NSObject <UIApplicationDelegate> { 
    BOOL histeresisExcited; 
    UIAcceleration* lastAcceleration; 
} 

@property(retain) UIAcceleration* lastAcceleration; 

@end 

@implementation L0AppDelegate 

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    [UIAccelerometer sharedAccelerometer].delegate = self; 
} 

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { 

    if (self.lastAcceleration) { 
     if (!histeresisExcited && L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.7)) { 
      histeresisExcited = YES; 

      /* SHAKE DETECTED. DO HERE WHAT YOU WANT. */ 

     } else if (histeresisExcited && !L0AccelerationIsShaking(self.lastAcceleration, acceleration, 0.2)) { 
      histeresisExcited = NO; 
     } 
    } 

    self.lastAcceleration = acceleration; 
} 

// and proper @synthesize and -dealloc boilerplate code 

@end 

我GOOGLE了它,並在How do I detect when someone shakes an iPhone?發現

+0

我不認爲這將是足夠好的OP什麼需要,但很好發現:) –

+1

謝謝,但檢測抖動的結束是複雜的和浪費時間。 – iProgrammed