Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextureView TranslationX & Y not expected behaviour on API 23

Tags:

Currently I am making a camera player with a textureview to render my camera. Because the preview can have any dimension I have created some custom code to alter the textureview when OnSurfaceTextureUpdated is called:

    void updateTextureMatrix(int width, int height) {
        Display display = WindowManager.DefaultDisplay;
        var isPortrait = (display.Rotation == SurfaceOrientation.Rotation0 || display.Rotation == SurfaceOrientation.Rotation180);

        int previewWidth = orgPreviewWidth;
        int previewHeight = orgPreviewHeight;

        if(isPortrait) {
            previewWidth = orgPreviewHeight;
            previewHeight = orgPreviewWidth;
        }

        // determine which part to crop
        float widthRatio = (float)width / previewWidth;
        float heightRatio = (float)height / previewHeight;

        float scaleX;
        float scaleY;

        if(widthRatio > heightRatio) {
            // height must be cropped
            scaleX = 1;
            scaleY = widthRatio * ((float)previewHeight / height);
        } else {
            // width must be cropped
            scaleX = heightRatio * ((float)previewWidth / width);
            scaleY = 1; 
        }

        Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();

        matrix.SetScale(scaleX, scaleY);
        _textureView.SetTransform(matrix);

        float scaledWidth = width * scaleX;
        float scaledHeight = height * scaleY;

        float dx = (width - scaledWidth) * 0.5f;
        float dy = (height - scaledHeight) * 0.5f;
        _textureView.TranslationX = dx;
        _textureView.TranslationY = dy;
    }

The scaling & calculation of dx & dy work perfectly fine on older android devices but the devices I have at my disposal with API level 23 throw unexpected behaviour.

The galaxy S3 displays it correctly: Galaxy S3

But on the S7: S7

The phone cuts off a lot of the image, despite positioning it correctly. This makes me believe the bottom part is not being rendered where on older devices it is. Can anyone confirm this and point me in the correct position to fix this issue?

like image 911
Philippe Creytens Avatar asked Feb 07 '17 16:02

Philippe Creytens


1 Answers

After long testing I figured out the issue was due to the SetTransform method. I was setting my scale using the matrix but this somehow rendered my texture & ignored the TranslationX & TranslationY. Removing the matrix & replacing it by float scaledWidth = width * scaleX; float scaledHeight = height * scaleY;

        float dx = (width - scaledWidth) * 0.5f;
        float dy = (height - scaledHeight) * 0.5f;
        _textureView.ScaleX = scaleX;
        _textureView.ScaleY = scaleY;
        _textureView.TranslationX = dx;
        _textureView.TranslationY = dy;

Fixed my issue of rendering wrongly on certain android devices.

like image 80
Philippe Creytens Avatar answered Sep 22 '22 10:09

Philippe Creytens