简体   繁体   English

设备相机方向,不包括设备横向/纵向

[英]Device camera direction excluding device landscape/portrait orientation

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.我尝试使用 Core Motion 和访问设备姿态来表示这一点。 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).我需要以可序列化的方式获取此信息,因为我稍后需要确定其他设备的摄像头是否指向同一方向(该设备可能是 iOS 或 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?重申:如果用户将他的手机摄像头(主摄像头不是自拍摄像头)指向自由女神像,那么无论用户是纵向还是横向拿着手机,编码此信息的最佳方式是什么,这样,如果另一个用户在同一位置他会知道指向他的相机的方向吗?

I believe that in order to achieve this, you can use CLLocation .我相信为了实现这一点,您可以使用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
}

As stated in this tutorial :本教程所述

The trueHeading is the angle between that vector and the vector starting from the same point but pointing towards the true north. trueHeading 是该矢量与从同一点开始但指向真北的矢量之间的角度。 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.如果您的手机正好指向真北,您将获得 0(零)度的航向,如果它指向东方,您将获得 90 度等。

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;
}

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:对不同的语言(Swift - Objective C)感到抱歉,但为了完全理解问题并找到完整的解决方案,我建议阅读源代码:

Hope this helps!希望这可以帮助! Let me know.让我知道。

There is 4 type of them.它们有 4 种类型。 one is deprecated:一个被弃用:

  1. public static final int ORIENTATION_UNDEFINED = 0; public static final int ORIENTATION_UNDEFINED = 0;
  2. public static final int ORIENTATION_PORTRAIT = 1; public static final int ORIENTATION_PORTRAIT = 1;
  3. public static final int ORIENTATION_LANDSCAPE = 2:公共静态最终 int ORIENTATION_LANDSCAPE = 2:
  4. @Deprecated public static final int ORIENTATION_SQUARE = 3; @Deprecated public static final int ORIENTATION_SQUARE = 3;

You can learn with你可以学习

val orientation: Int = resources.configuration.orientation

If you want to learn specific angle and position you should use Sensor service with this.sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager如果你想学习特定的角度和位置,你应该使用带有this.sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager传感器服务

and for exact position和准确的位置

sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR)?.let { this.rotationVector = it}

this class also has different features like.这个类也有不同的功能,比如。

TYPE_ACCELEROMETER

Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity.测量在所有三个物理轴(x、y 和 z)上施加到设备的加速力(以 m/s2 为单位),包括重力。

Motion detection (shake, tilt, etc.).运动检测(摇晃、倾斜等)。

TYPE_GRAVITY

Measures the force of gravity in m/s2 that is applied to a device on all three physical axes (x, y, z).测量在所有三个物理轴(x、y、z)上施加到设备的重力(以 m/s2 为单位)。

Motion detection (shake, tilt, etc.).运动检测(摇晃、倾斜等)。

TYPE_GYROSCOPE

Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z).测量设备绕三个物理轴(x、y 和 z)中的每一个轴的旋转速率(以 rad/s 为单位)。

Rotation detection (spin, turn, etc.).旋转检测(旋转、转弯等)。

TYPE_LINEAR_ACCELERATION

Measures the acceleration force in m/s2 that is applied to a device on all three physical axes (x, y, and z), without the force of gravity.以 m/s2 为单位测量在所有三个物理轴(x、y 和 z)上施加到设备的加速力,没有重力。

Monitoring acceleration along a single axis.监测沿单轴的加速度。

TYPE_ROTATION_VECTOR

Measures the orientation of a device by providing the three elements of the device's rotation vector.通过提供设备旋转矢量的三个元素来测量设备的方向。

Motion detection and rotation detection.运动检测和旋转检测。

In android, I use setRotation() method to rotted the camera.在 android 中,我使用 setRotation() 方法来腐烂相机。

setRotation(camera, parameters, requestedCameraId);

private void setRotation(Camera camera, Camera.Parameters parameters, int cameraId) {
    WindowManager windowManager =
            (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    int degrees = 0;

    //get current display rotation
    int rotation = windowManager.getDefaultDisplay().getRotation();

    switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
        default:
            Log.e(TAG, "Bad rotation value: " + rotation);
    }

    CameraInfo cameraInfo = new CameraInfo();
    Camera.getCameraInfo(cameraId, cameraInfo);

    int angle;
    int displayAngle;
    if (cameraInfo.facing == CameraInfo.CAMERA_FACING_FRONT) {
        angle = (cameraInfo.orientation + degrees) % 360;
        displayAngle = (360 - angle) % 360; // compensate for it being mirrored
    } else {  // back-facing
        angle = (cameraInfo.orientation - degrees + 360) % 360;
        displayAngle = angle;
    }

    // This corresponds to the rotation constants.
    // mRotation = angle / 90;

    camera.setDisplayOrientation(displayAngle);
    parameters.setRotation(angle);
}

I hope this is work for you.我希望这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 相机预览方向为横向,但设备为纵向 - Camera preview orientation is in landscape but device is in portrait 纵向模式下的相机预览方向(三星设备) - Camera preview orientation in portrait mode (samsung device) Android设备定位是风景,但传感器的行为就像定位是肖像 - Android Device Orientation is Landscape but Sensors Behaves Like the Orientation is Portrait 当设备方向从纵向更改为横向时,活动进入循环 - Activity going in loop when device orientation is changed from portrait to landscape 如何检测Android设备是纵向还是横向锁定 - How to detect whether Android device is locked in portrait or landscape orientation 获取Android / iOS上的默认设备方向(横向或纵向) - Get default device orientation (Landscape or Portrait) on Android/iOS 强制将横向资源用于具有纵向设备的自定义 VIEW - Force the use of landscape resources for a custom VIEW with device in portrait orientation 铸造设备朝向LANDSCAPE,PORTRAIT,LANDSCAPE_REVERTED和PORTRAIT_REVERTED - Casting device orientation to LANDSCAPE, PORTRAIT, LANDSCAPE_REVERTED and PORTRAIT_REVERTED 锁定设备的横向方向 - Lock Device Landscape Orientation 无论设备方向是纵向还是横向,我都想以横向模式录制视频 - I want to record video in Landscape mode no matter device orientation is portrait or landscape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM