【发布时间】:2012-12-27 18:46:11
【问题描述】:
我正在编写一个 iPhone 相机应用程序。当用户要拍照时,我想检查一下iPhone是否在晃动,等待没有晃动的那一刻,然后再抓拍手机。
我该怎么做?
【问题讨论】:
-
防抖比您想象的要复杂得多。那么,当用户摇晃 iPhone 时,您会在 2 秒延迟后拍照。
标签: iphone objective-c ios camera
我正在编写一个 iPhone 相机应用程序。当用户要拍照时,我想检查一下iPhone是否在晃动,等待没有晃动的那一刻,然后再抓拍手机。
我该怎么做?
【问题讨论】:
标签: iphone objective-c ios camera
Anit-shake 功能是一项相当复杂的功能。我认为它是一些强大的模糊检测/去除算法和 iPhone 上的陀螺仪的组合。
您可以先使用 iPhone 查看how to detect motion,然后看看可以得到什么样的结果。如果还不够,请开始查看shift/blur direction detection algorithms。这不是一个微不足道的问题,但如果有足够的时间,您可能可以完成。希望有帮助!
【讨论】:
// 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?
【讨论】: