ios App检查iPhone摇晃然后拍照

时间:2012-12-27 18:46:11

标签: iphone objective-c ios camera

我正在写一部iPhone相机应用程序。当用户即将拍照时,我想检查一下iPhone是否在摇晃并等待没有晃动的那一刻,然后抓住手机。

我该怎么做?

2 个答案:

答案 0 :(得分:5)

Anit-shake功能是一个非常复杂的功能。我认为它是一些强大的模糊检测/移除算法和iPhone上的陀螺仪的组合。

您可以从使用iPhone查看how to detect motion开始,看看您可以获得哪种结果。如果还不够,请开始研究shift/blur direction detection algorithms。这不是一个微不足道的问题,但如果有足够的时间,你可能会完成这件事。希望有帮助!

答案 1 :(得分: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?