-2

I have to make an app in which user can take photo only when iPhone is still. Can you please tell me how to proceed with that. Any help will be appreciated.

Below is the code that I have tried, please Suggest improvement on it, this code is giving jerky output

            _previousMotionValue = 0.0f;
            memset(xQueue, 0, sizeof(xQueue));
            memset(yQueue, 0, sizeof(yQueue));
            queueIndex = 0;

            [_motionManager startAccelerometerUpdatesToQueue:_motionManagerUpdatesQueue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {

                if ([_motionManagerUpdatesQueue operationCount] > 1) {
                    return;
                }

                xQueue[queueIndex] = -accelerometerData.acceleration.x;
                yQueue[queueIndex] = accelerometerData.acceleration.y;

                queueIndex++;
                if (queueIndex >= QueueCapacity) {
                    queueIndex = 0;
                }

                float xSum = 0;
                float ySum = 0;

                int i = 0;

                while (i < QueueCapacity)
                {
                    xSum += xQueue[i];
                    ySum += yQueue[i];
                    i++;
                }

                ySum /= QueueCapacity;
                xSum /= QueueCapacity;

                    double motionValue = sqrt(xSum * xSum + ySum * ySum);
                    CGFloat difference = 50000.0 * ABS(motionValue - _previousMotionValue);
                    if (difference < 100)
                    {
                        //fire event for capture
                    }
                    [view setVibrationLevel:difference];
                    _previousMotionValue = motionValue;

            }];

Based on vibration level, I am setting the different images like green, yellow, red. I have chosen threshold 100.

Mohit Nigam
  • 454
  • 5
  • 10

1 Answers1

0

To answer “…user can take photo only when iPhone is stabilized…?”:

You can use CoreMotion.framework and its CMMotionManager to obtain info about device movement. (I guess you are interested in accelerometer data.) These data will come at high rate (you can choose frequency, default if 1/60 s). Then you store (let's say) 10 latest values and make some statistics about the average and differences. By choosing optimal treshold you can tell when the device is in stabilized position.


But you mentioned image stabilization, which is not the same as taking photos in stabilized position. To stabilize image, I guess you will have to adjust the captured image by some small offset calculated from device motion.

Tricertops
  • 8,492
  • 1
  • 39
  • 41
  • I have tried similar logic, you can find that in updated question, but it is giving jerky output. – Mohit Nigam Feb 11 '13 at 19:05
  • 1
    Try to use low-pass filter. One is included in example app “AccelerometerGraph” in docs. When we used it for accelerometer data it was much better and smoother. – Tricertops Feb 11 '13 at 19:48
  • There is always noise that affects the motion sensors. You will always get a small amount of jerky motion. You've got to work out what threshold you can deal with ok. – Fogmeister Feb 11 '13 at 20:23