android - 设备摄像头方向,不包括设备横向/纵向方向

标签 android ios orientation augmented-reality

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

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

最佳答案

我相信为了实现这一点,你可以使用CLLocation .
因为你的主要目标是找到 基点手机指向哪个方向,您可以执行以下操作:

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

this tutorial 中所述:

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.



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

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.



但!这个问题有一个解决方案:


你可以使用这样的东西来检索你正在寻找的标题:

-(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 - Objective C)感到抱歉,但为了完全理解问题并找到完整的解决方案,我建议阅读源代码:
  • Medium Compass Tutorial
  • O'Reilly's Compass Tutorial

  • 希望这可以帮助!让我知道。

    关于android - 设备摄像头方向,不包括设备横向/纵向方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60507826/

    相关文章:

    android - 设计 Android 可绘制形状

    java - 未经检查的对 List<T> 的调用

    ios - 如何使用 QuickBlox 在 Swift 中创建返回当前时间和日期之后发生的事件的查询

    iOS 11 更喜欢 LargeTitles 在方向改变后不扩展

    java - 将 parse api 连接到 android 滑动布局

    ios - NSURLSessionDownloadTask 在后台停止,在前台恢复缓慢

    ios - 我可以直接从具有特定大小类或特征集合的 xib 加载 UITableViewCell 吗?

    iphone - 程序化 View 正确旋转

    javascript - 重新分配 window.orientation?

    Android 错误 - close() 从未在数据库上显式调用