Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix CameraX rotation support

I faced with the issue of CameraX screen rotation support.

Portrait: see picture

Landscape: see picure

Transformation code:

private void updateTransform() {
    Log.d(TAG, "updateTransform: ");
    Matrix matrix = new Matrix();

    float centerX = cameraViewTextureV.getWidth() / 2f;
    float centerY = cameraViewTextureV.getHeight() / 2f;

    switch (cameraViewTextureV.getDisplay().getRotation()) {
        case Surface.ROTATION_0:
            rotation = 0;
            break;

        case Surface.ROTATION_90:
            rotation = 90;
            break;

        case Surface.ROTATION_180:
            rotation = 180;
            break;

        case Surface.ROTATION_270:
            rotation = 270;
            break;

        default:
            break;
    }

    matrix.postRotate((float) -rotation, centerX, centerY);

    cameraViewTextureV.setTransform(matrix);
}

So, as you can see in the pictures, camera support screen rotation not correctly... I calling updateTransform method when screen rotating... Took this code from the official guide for cameraX from Android developers site.

Will be very grateful for any proposes for fixing. Have a nice day!

like image 539
Nikita Lapin Avatar asked Jun 05 '19 14:06

Nikita Lapin


People also ask

How do I fix the camera orientation on my Android?

There is a property in class Camera. CameraInfo named as orientation . It returns the integer. You can get the current orientation and then changed accordingly.

What are possible App orientation in the APK builder?

Display orientation. This refers to which side of the device is in the upward position, and can be one of four values: portrait, landscape, reverse portrait, or reverse landscape.

Why does an image captured using camera intent gets rotated on some devices on Android?

Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the Exif data with the orientation that the photo should be viewed in.

What is camera orientation?

The orientation of a Camera Station defines the position of and the pointing direction of the camera at the time of exposure of a photograph. Orientation can also refer to the process of determining the orientation of one or more Camera Stations.


1 Answers

Solution based on AutoFitPreviewBuilder:

preview.onPreviewOutputUpdateListener = Preview.OnPreviewOutputUpdateListener { output ->
    // Get all dimensions
    val metrics = DisplayMetrics().also { camera_texture_view.display.getRealMetrics(it) }
    val previewWidth = metrics.widthPixels
    val previewHeight = metrics.heightPixels
    val width = output.textureSize.width
    val height = output.textureSize.height
    val centerX = camera_texture_view.width.toFloat() / 2
    val centerY = camera_texture_view.height.toFloat() / 2

    // Get rotation
    val rotation = when (camera_texture_view.display.rotation) {
        Surface.ROTATION_0 -> 0
        Surface.ROTATION_90 -> 90
        Surface.ROTATION_180 -> 180
        Surface.ROTATION_270 -> 270
        else -> throw IllegalStateException()
    }
    val matrix = Matrix()
    // Rotate matrix
    matrix.postRotate(-rotation.toFloat(), centerX, centerY)
    // Scale matrix
    matrix.postScale(
        previewWidth.toFloat() / height,
        previewHeight.toFloat() / width,
        centerX,
        centerY
    )
    // Assign transformation to view
    camera_texture_view.setTransform(matrix)
    camera_texture_view.surfaceTexture = output.surfaceTexture
}
like image 91
Marcin Mrugas Avatar answered Oct 15 '22 05:10

Marcin Mrugas