设备摄像头方向(不包括设备横向/纵向) [英] Device camera direction excluding device landscape/portrait orientation

查看:148
本文介绍了设备摄像头方向(不包括设备横向/纵向)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取前置摄像头的方向,但不包括设备方向(横向/纵向).我尝试使用Core Motion和访问设备的姿态来表示这一点.在这里,我尝试访问Euler角度并排除偏航,但是,这似乎不起作用,因为在旋转设备时,一个Euler角度值发生了变化.我也正在考虑使用方向四元数,但是我没有使用它们的经验.我需要以可序列化的方式获取此信息,因为稍后需要确定其他设备的相机是否指向同一方向(此设备可以是iOS或Android).

I need to get the direction of the front facing camera excluding the devices orientation (landscape/portrait). I tried to represent this using Core Motion and accessing device attitude. Here I tried to access the Euler angles and exclude the yaw, however this doesn't seem to work as when rotating the device more that one Euler angle value changes. I am also considering using the orientation quaternion but I don't have experience using them. I need this information in a serialisable manner as I would later need to determine if a different device has it's camera pointed in the same direction ( this device may be either iOS or Android).

要重申:如果用户将手机摄像头(主摄像头不是自拍相机)指向自由女神像,那么无论用户是纵向还是横向手持手机,对这种信息进行编码的最佳方法是什么,这样,如果另一个用户在同一位置,他会知道指向他的相机的方向吗?

To reiterate: if a user is pointing his phone camera (main camera not selfie one) towards the statue of liberty, what would be the best way to encode this information regardless if the user is holding the phone in portrait or landscape, such that if another user is in the same location he would know the direction in which to point his camera?

推荐答案

我相信,要实现此目的,可以使用CLLocation. 您的主要目标是找到电话指向的基点,因此您可以执行以下操作:

I believe that in order to achieve this, you can use CLLocation. As you main objective is to find the cardinal point towards which the phone points, you can do things like:

fun locationManager(_ manager: CLLocationManager, didUpdateHeading heading: CLHeading) {
   let angle = newHeading.trueHeading.toRadians // convert from degrees to radians
   // do what you please with this information
}

此教程中所述:

trueHeading是该向量与从相同点开始但指向真北的向量之间的角度. 如果您的手机正好指向True North,则您将获得0(零)度的航向;如果您的手机指向East,则将获得90度等.

The trueHeading is the angle between that vector and the vector starting from the same point but pointing towards the true north. If your phone points exactly to the True North you’ll get a heading of 0 (zero) degrees, if it points towards East you’ll get 90 degrees etc.

现在,如此处所述,设备方向可能会引起一些麻烦:

Now, as stated here, the device orientation can cause some trouble:

不幸的是,如果用户将设备定向到横向模式,则报告的标题将不正确,或者至少对用户而言看起来不正确.当您在本书后面查看增强现实界面时,这将变得尤为重要.通常在横向左"模式下查看此类界面.

Unfortunately, if the user orientates the device into Landscape Mode, the reported headings will be incorrect, or at least look incorrect to the user. This will become especially important when you look at augmented reality interfaces later in the book; such interfaces are generally viewed in Landscape Left mode.

但是!有一个解决此问题的方法:

BUT! There is a solution for this problem:

您可以使用类似这样的内容来检索您要查找的标题:

And you can use something like this to retrieve the heading you are looking for:

-(float)magneticHeading:(float)heading 
        fromOrientation:(UIDeviceOrientation) orientation {

    float realHeading = heading;
    switch (orientation) {1
          case UIDeviceOrientationPortrait:
               break;
          case UIDeviceOrientationPortraitUpsideDown:
               realHeading = realHeading + 180.0f;
               break;
          case UIDeviceOrientationLandscapeLeft:
               realHeading = realHeading + 90.0f;
               break;
          case UIDeviceOrientationLandscapeRight:
               realHeading = realHeading - 90.0f;
               break;
          default:
               break;
    }
    while ( realHeading > 360.0f ) {
          realHeading = realHeading - 360;
    }
    return realHeading;
}

很抱歉,使用了不同的语言(Swift-目标C),但是为了完全理解问题并找到完整的解决方案,我建议您阅读以下来源:

Sorry about the different languages (Swift - Objective C), but in order to fully understand the problem and find a complete solution I would recommend to read into the sources:

  • Medium Compass Tutorial
  • O'Reilly's Compass Tutorial

希望这会有所帮助!让我知道.

Hope this helps! Let me know.

这篇关于设备摄像头方向(不包括设备横向/纵向)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆