【问题标题】:ios App check iPhone shaking and then take pictureios App检查iPhone抖动然后拍照
【发布时间】:2012-12-27 18:46:11
【问题描述】:

我正在编写一个 iPhone 相机应用程序。当用户要拍照时,我想检查一下iPhone是否在晃动,等待没有晃动的那一刻,然后再抓拍手机。

我该怎么做?

【问题讨论】:

  • 防抖比您想象的要复杂得多。那么,当用户摇晃 iPhone 时,您会在 2 秒延迟后拍照。

标签: iphone objective-c ios camera


【解决方案1】:

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

您可以先使用 iPhone 查看how to detect motion,然后看看可以得到什么样的结果。如果还不够,请开始查看shift/blur direction detection algorithms。这不是一个微不足道的问题,但如果有足够的时间,您可能可以完成。希望有帮助!

【讨论】:

  • +1 为一个非常模糊的“为我做”类型的问题提供真正有用的答案。
【解决方案2】:
// 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?

【讨论】:

  • 我认为它不足以满足 OP 的需求,但很好发现 :)
  • 谢谢,但是检测 Shaking 的结束很复杂而且浪费时间。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多