问题描述
我需要获取前置摄像头的方向,但不包括设备方向(横向/纵向).我尝试使用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
}
如此教程中所述:
现在,如此处所述,设备方向可能会引起一些麻烦:
Now, as stated here, the device orientation can cause some trouble:
但是!有一个解决此问题的方法:
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.
这篇关于设备摄像头方向(不包括设备横向/纵向)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!