How does auto focus in cameras work? I am just wondering how does camera auto focus work. What kind of processing does it do on images in order to adjust the focus? Is there freely available code that detects the focus level of an image or video?
This answer actually provides free code to perform the focus detection. 
https://stackoverflow.com/a/3795151/600736