Android Question Help converting this code to B4A

walterf25

Expert
Licensed User
Longtime User
Hi all, i am currently working on a project at work that requires writing an application to test the cameras on this product, i am currently using the Camera2 and CameraEx2 class written by Erel, i need to use the following java code to rotate the preview screen, i've did some research and found this code online, i am trying to use it but i need to convert it to B4A, my understanding of Reflection and JavaObject library is very limitted, can anyone help me with this?

B4X:
    private void setSurfaceOrientation(){
        if (textureView == null) {

            return;
        } else try {
            {
                Matrix matrix = new Matrix();
                int rotation = getWindowManager().getDefaultDisplay().getRotation();
                RectF textureRectF = new RectF(0, 0, 640, 480);
                RectF previewRectF = new RectF(0, 0, textureView.getHeight(), textureView.getWidth());
                float centerX = textureRectF.centerX();
                float centerY = textureRectF.centerY();
                if (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) {
                    previewRectF.offset(centerX - previewRectF.centerX(), centerY - previewRectF.centerY());
                    matrix.setRectToRect(textureRectF, previewRectF, Matrix.ScaleToFit.FILL);
                    float scale = Math.max((float) 640 / 640, (float) 480 / 640);
                    matrix.postScale(scale, scale, centerX, centerY);
                    matrix.postRotate(90 * (rotation - 2), centerX, centerY);
                }
                textureView.setTransform(matrix);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Thanks All.
Walter
 

techknight

Well-Known Member
Licensed User
Longtime User
That subroutine is proprietary to an object called "Matrix" and its internal functions, that subroutine wont help you. hopefully someone can chime in with something that does.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
That subroutine is proprietary to an object called "Matrix" and its internal functions, that subroutine wont help you. hopefully someone can chime in with something that does.
No, Matrix is a an Android.Graphics class that used to perform complex mathematical functions, I know how instantiate the Matrix class using Java object library, but i'm a bit confused as to the rest of the code, i've tried several things but i keep getting errors, I know we have a class called Rect in B4A but i don't see the offset property in that class, the code posted above requires that method.

Walter
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Ah, Didnt know that since I didnt see an import that would declare this. I think Rect would be considered a panel.
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
I'm no java expert, but looking up what the function does I think you could just replace that code with the manual equivalents? It looks to be the same as the .NET .Inflate method.
B4X:
offset(float dx, float dy)
Offset the rectangle by adding dx to its left and right coordinates, and adding dy to its top and bottom coordinates.
https://developer.android.com/reference/android/graphics/RectF
Thanks, i missed that when looking at the documentation regarding the RectF class.

I'll give that a try
Walter
 
Upvote 0
Top