Search code examples
androidandroid-ndkandroid-camera2arcore

How get camera orientation in NDK


I'm getting the NDK_Image from ARCore on the NDK side of the project. My AImage is coming rotated because of the camera orientation.

How could I get the value of the camera orientation inside the NDK?

In the Camera NDK docs I found this ACAMERA_JPEG_ORIENTATION

It even give a sample of code:

private int getJpegOrientation(CameraCharacteristics c, int deviceOrientation) {
    if (deviceOrientation == android.view.OrientationEventListener.ORIENTATION_UNKNOWN) return 0;
    int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION);
    // Round device orientation to a multiple of 90
deviceOrientation = (deviceOrientation + 45) / 90 * 90;
    // Reverse device orientation for front-facing cameras
    boolean facingFront = c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT;
    if (facingFront) deviceOrientation = -deviceOrientation;
    // Calculate desired JPEG orientation relative to camera orientation to make
    // the image upright relative to the device orientation
    int jpegOrientation = (sensorOrientation + deviceOrientation + 360) % 360;
    return jpegOrientation;
}

But it don't explain how to use, and I cannot find the library for CameraCharascteristics.

Would be a nice a sample about how to get the camera orientation in the NDK. Thanks!


Solution

  • This sample code in Java is there by mistake.

    From C++, you can call ACameraManager_getCameraCharacteristics() with tag ACAMERA_SENSOR_ORIENTATION and tag ACAMERA_LENS_FACING.

    OrientationEventListener.ORIENTATION_UNKNOWN is a constant -1. CameraCharacteristics.LENS_FACING_FRONT or ACAMERA_LENS_FACING_FRONT is 0.