Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone : camera autofocus observer?

I would like to know if it's possible to receive notification about autofocus inside an iPhone application?

I.E, does it exist a way to be notified when autofocus starts, ends, if it has succeed or failed... ?

If so, what is this notification name ?

like image 830
Sly Avatar asked Feb 01 '12 17:02

Sly


People also ask

Does iPhone camera have autofocus?

Before you take a photo, the iPhone camera automatically sets the focus and exposure, and face detection balances the exposure across many faces. If you want to manually adjust the focus and exposure, do the following: Open Camera. Tap the screen to show the automatic focus area and exposure setting.

How do I change autofocus on my iPhone?

You can schedule a Focus to turn on at certain times, when you're at a particular location, or when you open a specific app. Go to Settings > Focus, then tap the Focus you want to schedule. at the top left. Tap Add Schedule, then set the times, a location, or an app you want to activate this Focus.

How does iPhone camera autofocus work?

Your camera focuses and adjusts exposure automatically based on what you're pointing it towards. You can tap a different area in the viewfinder to change the focus and exposure. If you want to keep the focus and exposure in the same spot, press and hold on the screen until you see AE/AF Lock.


1 Answers

I find the solution for my case to find when autofocus starts / ends. It's simply dealing with KVO (Key-Value Observing).

In my UIViewController:

// callback
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if( [keyPath isEqualToString:@"adjustingFocus"] ){
        BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
        NSLog(@"Is adjusting focus? %@", adjustingFocus ? @"YES" : @"NO" );
        NSLog(@"Change dictionary: %@", change);
    }
}

// register observer
- (void)viewWillAppear:(BOOL)animated{
    AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    int flags = NSKeyValueObservingOptionNew; 
    [camDevice addObserver:self forKeyPath:@"adjustingFocus" options:flags context:nil];

    (...)   
}

// unregister observer
- (void)viewWillDisappear:(BOOL)animated{
    AVCaptureDevice *camDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    [camDevice removeObserver:self forKeyPath:@"adjustingFocus"];

    (...)
}

Documentation:

  • Key-Value Observing programming guide
  • NSKeyValueObserving protocol
like image 56
Sly Avatar answered Oct 27 '22 13:10

Sly