title: How to calculate FOV?
body: <h2 id="initial-context-1ekp">Initial Context</h2>
  <p>I am developping an augmented reality application location based and I need to get the field of view [FOV] (I just update the value when the orientation change, so I am looking for a method which can get this value when I call it)</p>
  <p>The goal is to make a &quot;degree ruler&quot; relevant to reality like the following:
  <img src="https://i.stack.imgur.com/D1GNo.png" alt="Degree Ruler - AR App" /></p>
  <p>I am already using <code>AVCaptureSession</code> to display camera stream ; and a path coupled with a <code>CAShapeLayer</code> to draw the ruler. This is working pretty good, but now I have to use <em>Field of view</em> value to place my element in the right place (choose the right space between 160° and 170° for example!).</p>
  <p>Actually, I am hardcoding these values with these sources : <a href="https://stackoverflow.com/a/3594424/3198096">https://stackoverflow.com/a/3594424/3198096</a> (Special thanks to @hotpaw2!) But I am not sure they are fully precise and this is not handling iPhone 5, etc. I was unable to obtain values from official sources (Apple!), but there is a link showing values for all iDevice I think I need (4, 4S, 5, 5S) : <a href="http://www.anandtech.com/show/7329/some-thoughts-about-the-iphone-5s-camera-improvements" rel="nofollow noreferrer">AnandTech | Some thoughts about the iphone 5s camera improvements</a>.</p>
  <p><strong>Note</strong>: After personal test and some other research online, I am pretty sure these values are inaccurate! Also this forces me to use an external library to check which model of iPhone am I using to manually initialize my FOV... And I have to check my values for all supported device.</p>
  <p>###I would prefer a <em>&quot;code solution&quot;</em> !###</p>
  <p>After reading this post: <a href="https://stackoverflow.com/questions/3772426/iphone-real-time-video-color-info-focal-length-aperture">iPhone: Real-time video color info, focal length, aperture?</a>, I am trying to get <code>exif data</code> from AVCaptureStillImageOutput like suggested. After what I could be able to read the <em>focal length</em> from the exif data, and then calculate the horizontal and vertical field of view via formula! (Or maybe directly obtain the FOV like showed here : <a href="http://www.brianklug.org/2011/11/a-quick-analysis-of-exif-data-from-apples-iphone-4s-camera-samples/" rel="nofollow noreferrer">http://www.brianklug.org/2011/11/a-quick-analysis-of-exif-data-from-apples-iphone-4s-camera-samples/</a> --
  <strong>note</strong>: after a certain number of update, It seems that we can't get directly field of view from exif!)</p>
  <hr />
  <h2 id="actual-point-l8wk">Actual Point</h2>
  <p><strong>Sources from</strong> : <a href="http://iphonedevsdk.com/forum/iphone-sdk-development/112225-camera-app-working-well-on-3gs-but-not-on-4s.html" rel="nofollow noreferrer">http://iphonedevsdk.com/forum/iphone-sdk-development/112225-camera-app-working-well-on-3gs-but-not-on-4s.html</a> and <a href="https://stackoverflow.com/questions/12869706/modified-exif-data-doesnt-save-properly">Modified EXIF data doesn&#39;t save properly</a></p>
  <p>Here is the code I am using:</p>
  <pre><code>AVCaptureDevice* camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  if (camera != nil)
  {
      captureSession = [[AVCaptureSession alloc] init];
      
      AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:camera error:nil];
      
      [captureSession addInput:newVideoInput];
      
      captureLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:captureSession];
      captureLayer.frame = overlayCamera.bounds;
      [captureLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
      previewLayerConnection=captureLayer.connection;
      [self setCameraOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
      [overlayCamera.layer addSublayer:captureLayer];
      [captureSession startRunning];
      
      AVCaptureStillImageOutput *stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
      NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
      [stillImageOutput setOutputSettings:outputSettings];
      [captureSession addOutput:stillImageOutput];
      
      AVCaptureConnection *videoConnection = nil;
      for (AVCaptureConnection *connection in stillImageOutput.connections)
      {
          for (AVCaptureInputPort *port in [connection inputPorts])
          {
              if ([[port mediaType] isEqual:AVMediaTypeVideo] )
              {
                  videoConnection = connection;
                  break;
              }
          }
          if (videoConnection) { break; }
      }
      
      [stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                                           completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error)
       {
           NSData *imageNSData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
           
           CGImageSourceRef imgSource = CGImageSourceCreateWithData((__bridge_retained CFDataRef)imageNSData, NULL);
  
           NSDictionary *metadata = (__bridge NSDictionary *)CGImageSourceCopyPropertiesAtIndex(imgSource, 0, NULL);
  
           NSMutableDictionary *metadataAsMutable = [metadata mutableCopy];
           
           NSMutableDictionary *EXIFDictionary = [[metadataAsMutable objectForKey:(NSString *)kCGImagePropertyExifDictionary]mutableCopy];
          
           if(!EXIFDictionary)
               EXIFDictionary = [[NSMutableDictionary dictionary] init];
  
           [metadataAsMutable setObject:EXIFDictionary forKey:(NSString *)kCGImagePropertyExifDictionary];
           
           NSLog(@&quot;%@&quot;,EXIFDictionary);
       }];
  }
  </code></pre>
  <p>Here is the output:</p>
  <pre><code>{
      ApertureValue = &quot;2.52606882168926&quot;;
      BrightnessValue = &quot;0.5019629837352776&quot;;
      ColorSpace = 1;
      ComponentsConfiguration =     (
          1,
          2,
          3,
          0
      );
      ExifVersion =     (
          2,
          2,
          1
      );
      ExposureMode = 0;
      ExposureProgram = 2;
      ExposureTime = &quot;0.008333333333333333&quot;;
      FNumber = &quot;2.4&quot;;
      Flash = 16;
      FlashPixVersion =     (
          1,
          0
      );
      FocalLenIn35mmFilm = 40;
      FocalLength = &quot;4.28&quot;;
      ISOSpeedRatings =     (
          50
      );
      LensMake = Apple;
      LensModel = &quot;iPhone 4S back camera 4.28mm f/2.4&quot;;
      LensSpecification =     (
          &quot;4.28&quot;,
          &quot;4.28&quot;,
          &quot;2.4&quot;,
          &quot;2.4&quot;
      );
      MeteringMode = 5;
      PixelXDimension = 1920;
      PixelYDimension = 1080;
      SceneCaptureType = 0;
      SceneType = 1;
      SensingMethod = 2;
      ShutterSpeedValue = &quot;6.906947890818858&quot;;
      SubjectDistance = &quot;69.999&quot;;
      UserComment = &quot;[S.D.] kCGImagePropertyExifUserComment&quot;;
      WhiteBalance = 0;
  }
  </code></pre>
  <p>I think I have everything I need to calculate FOV. But are they the right values? Because after reading a lot of different website giving different <em>focal length</em> values, I am a bit confused! Also my PixelDimensions seems to be wrong!</p>
  <p>Via <a href="http://en.wikipedia.org/wiki/Angle_of_view" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Angle_of_view</a> this is the formula I planned to use:</p>
  <pre><code>FOV = (IN_DEGREES(   2*atan( (d) / (2  * f) )   ));
  // d = sensor dimensions (mm)
  // f = focal length (mm)
  </code></pre>
  <h3 id="my-question-ak0i">My Question</h3>
  <p>Do my method and my formula look right, and if yes, which values do I pass to the function?</p>
  <hr />
  <h3 id="precisions-ebj4">Precisions</h3>
  <ul>
  <li>FOV is what I think I need to use, if you have any suggestion of how the ruler can match reality; I would accept the answer !</li>
  <li>Zoom is disabled in the augmented reality view controller, so my field of view is fixed when camera is initialized, and can't change until the user rotate the phone!</li>
  </ul>
  <hr />