如何知道iPhone或iPad相机的视角

时间:2013-05-15 01:38:33

标签: ios camera

在Android中,API提供了视角范围:

  1. Camera.Parameters.getHorizontalViewAngle()
  2. Camera.Parameters.getVerticalViewAngle()
  3. iOS中的等效内容是什么? 我不想预先编写这些值,因为它不灵活。

2 个答案:

答案 0 :(得分:1)

我不完全确定在这种情况下“水平”和“垂直”是什么意思,但我想到两个计算,围绕“z”轴的旋转(即我们与照片中的地平线的水平)它向前和向后倾斜多少(即绕“x”轴旋转,即向上或向下倾斜)。您可以使用Core Motion执行此操作。只需add it to your project然后就可以执行以下操作:

  1. 确保导入CoreMotion标题:

    #import <CoreMotion/CoreMotion.h>
    
  2. 定义一些类属性:

    @property (nonatomic, strong) CMMotionManager *motionManager;
    @property (nonatomic, strong) NSOperationQueue *deviceQueue;
    
  3. 启动动画管理器:

    - (void)startMotionManager
    {
        self.deviceQueue = [[NSOperationQueue alloc] init];
        self.motionManager = [[CMMotionManager alloc] init];
        self.motionManager.deviceMotionUpdateInterval = 5.0 / 60.0;
    
        [self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical
                                                                toQueue:self.deviceQueue
                                                            withHandler:^(CMDeviceMotion *motion, NSError *error)
        {
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                CGFloat x = motion.gravity.x;
                CGFloat y = motion.gravity.y;
                CGFloat z = motion.gravity.z;
    
                // how much is it rotated around the z axis
    
                CGFloat rotationAngle = atan2(y, x) + M_PI_2;                  // in radians
                CGFloat rotationAngleDegrees = rotationAngle * 180.0f / M_PI;  // in degrees
    
                // how far it it tilted forward and backward
    
                CGFloat r = sqrtf(x*x + y*y + z*z);
                CGFloat tiltAngle = (r == 0.0 ? 0.0 : acosf(z/r);              // in radians
                CGFloat tiltAngleDegrees = tiltAngle * 180.0f / M_PI - 90.0f); // in degrees
            }];
        }];
    }
    
  4. 完成后,停止运动管理器:

    - (void)stopMotionManager
    {
        [self.motionManager stopDeviceMotionUpdates];
        self.motionManager = nil;
        self.deviceQueue = nil;
    }
    
  5. 我没有对这里的值做任何事情,但您可以将它们保存在类属性中,然后您可以在应用程序的其他位置访问它们。或者您可以从此处将UI更新发送回主队列。一堆选项。

    由于这是iOS 5及更高版本,如果应用程序支持早期版本,您可能还想弱连接Core Motion,然后检查一切是否正常,如果没有,只是意识到你不会是捕获设备的方向:

    if ([CMMotionManager class]) 
    {
        // ok, core motion exists
    }
    

    而且,如果你想知道我每秒十二次相当随意的选择,在Event Handling Guide for iOS,如果只是检查设备的方向,他们建议10-20 /秒。

答案 1 :(得分:1)

在iOS 7.0+中,您可以通过读取此属性来获取相机的FOV角度。 https://developer.apple.com/documentation/avfoundation/avcapturedeviceformat/1624569-videofieldofview?language=objc

AVCaptureDevice *camera;
camera = ...
float fov = [[camera activeFormat] videoFieldOfView];
NSLog("FOV=%f(deg)", fov);