Skip to content Skip to sidebar Skip to footer

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 atti

Solution 1:

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
}

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. 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) {1case 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:

Hope this helps! Let me know.

Solution 2:

There is 4 type of them. one is deprecated:

  1. public static final int ORIENTATION_UNDEFINED = 0;
  2. public static final int ORIENTATION_PORTRAIT = 1;
  3. public static final int ORIENTATION_LANDSCAPE = 2:
  4. @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

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.

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).

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).

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.

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.

Solution 3:

In android, I use setRotation() method to rotted the camera.

setRotation(camera, parameters, requestedCameraId);

privatevoidsetRotation(Camera camera, Camera.Parameters parameters, int cameraId) {
    WindowManagerwindowManager=
            (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    intdegrees=0;

    //get current display rotationintrotation= 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);
    }

    CameraInfocameraInfo=newCameraInfo();
    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.

Post a Comment for "Device Camera Direction Excluding Device Landscape/portrait Orientation"