初始环境
我正在开发一款基于位置的增强现实应用程序,需要获取视野范围[FOV](仅在方向更改时更新值,因此我正在寻找一种可以在调用时获取此值的方法)
目标是制作一个与现实相关的“度量尺”,如下所示:
我已经使用AVCaptureSession
来显示相机流;并使用与CAShapeLayer
耦合的路径来绘制标尺。这很有效,但现在我必须使用视野值将我的元素放置在正确的位置(例如选择160°和170°之间的正确空间!)。
实际上,我正在使用以下来源硬编码这些值:https://dev59.com/r3A65IYBdhLWcg3w2yk2#3594424(特别感谢@hotpaw2!)但我不确定它们是否完全准确,而且这也无法处理iPhone 5等。我无法从官方来源(苹果!)获得值,但有一个链接显示了我认为需要的所有iDevice的值(4、4S、5、5S):AnandTech | Some thoughts about the iphone 5s camera improvements。
注意:经过个人测试和在线研究,我相当确定这些值是不准确的!这也迫使我使用外部库来检查我使用的iPhone型号以手动初始化我的FOV...并且我必须检查所有支持的设备的值。
###我更喜欢一个"代码解决方案"!###
阅读了这篇文章:iPhone:实时视频颜色信息,焦距,光圈?之后,我试图像建议的那样从AVCaptureStillImageOutput获取exif数据
。在我读取到exif数据中的焦距后,可以通过公式计算水平和垂直视场!(或者可能直接获取FOV,就像这里展示的一样:http://www.brianklug.org/2011/11/a-quick-analysis-of-exif-data-from-apples-iphone-4s-camera-samples/-- 注意:更新一定数量后,似乎我们无法直接从exif中获取视野!)
实际要点
来源:http://iphonedevsdk.com/forum/iphone-sdk-development/112225-camera-app-working-well-on-3gs-but-not-on-4s.html 和 Modified EXIF data doesn't save properly
这是我正在使用的代码:
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(@"%@",EXIFDictionary);
}];
}
这里是输出:
{
ApertureValue = "2.52606882168926";
BrightnessValue = "0.5019629837352776";
ColorSpace = 1;
ComponentsConfiguration = (
1,
2,
3,
0
);
ExifVersion = (
2,
2,
1
);
ExposureMode = 0;
ExposureProgram = 2;
ExposureTime = "0.008333333333333333";
FNumber = "2.4";
Flash = 16;
FlashPixVersion = (
1,
0
);
FocalLenIn35mmFilm = 40;
FocalLength = "4.28";
ISOSpeedRatings = (
50
);
LensMake = Apple;
LensModel = "iPhone 4S back camera 4.28mm f/2.4";
LensSpecification = (
"4.28",
"4.28",
"2.4",
"2.4"
);
MeteringMode = 5;
PixelXDimension = 1920;
PixelYDimension = 1080;
SceneCaptureType = 0;
SceneType = 1;
SensingMethod = 2;
ShutterSpeedValue = "6.906947890818858";
SubjectDistance = "69.999";
UserComment = "[S.D.] kCGImagePropertyExifUserComment";
WhiteBalance = 0;
}
我认为我拥有计算视场角所需的所有内容。但是它们是否是正确的值?因为在阅读了很多给出不同焦距值的不同网站后,我有点困惑!另外,我的像素尺寸似乎是错误的!
通过http://en.wikipedia.org/wiki/Angle_of_view这个链接,这是我计划使用的公式:
FOV = (IN_DEGREES( 2*atan( (d) / (2 * f) ) ));
// d = sensor dimensions (mm)
// f = focal length (mm)
我的问题
我的方法和公式是否正确,如果是,我需要向函数传递哪些值?
精度
- FOV(视野)是我认为需要使用的,如果您有任何建议,可以让尺子与现实匹配;我会接受答案!
- 在增强现实视图控制器中,缩放被禁用,因此当摄像头初始化时,我的视野是固定的,并且直到用户旋转手机之前都无法更改!