我正在编写一个 iPhone 相机应用程序。当用户要拍照时,我想检查一下iPhone是否在晃动,等待没有晃动的那一刻,然后再抓拍手机。
我该怎么做?
我正在编写一个 iPhone 相机应用程序。当用户要拍照时,我想检查一下iPhone是否在晃动,等待没有晃动的那一刻,然后再抓拍手机。
我该怎么做?
Anit-shake 功能是一个相当复杂的功能。我认为它是一些强大的模糊检测/去除算法和 iPhone 上的陀螺仪的组合。
你可以从研究如何用 iPhone 检测运动开始,看看你能得到什么样的结果。如果还不够,请开始研究移位/模糊方向检测算法。这不是一个微不足道的问题,但如果有足够的时间,您可能会完成。希望有帮助!
// 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 上搜索并在如何检测有人摇晃 iPhone 时发现?