Android Question perspectiveTransformation

BeneBarros

Active Member
Licensed User
Longtime User
Could someone help me to transform this sub to B4A.
Thanks in advance.


B4X:
Public static Bitmap perspectiveTransformation(Bitmap bitmap,BoundingQuad boundingQuad)
{
    matrix matrix = new matrix();
    float[] dst = new float[] {
            0,
            0,
            bitmap.getWidth(),
            0,
            bitmap.getWidth(),
            bitmap.getHeight(),
            0,
            bitmap.getHeight()
    };
    float[] src = new float[] {
            boundingQuad.topLeft.x,
            boundingQuad.topLeft.y,
            boundingQuad.topRight.x,
            boundingQuad.topRight.y,
            boundingQuad.bottomRight.x,
            boundingQuad.bottomRight.y,
            boundingQuad.bottomLeft.x,
            boundingQuad.bottomLeft.y
    };
    matrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1);
    Return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, True);
}
 

Johan Schoeman

Expert
Licensed User
Longtime User


You can do it with inline Java code:

B4X:
#Region  Project Attributes
    #ApplicationLabel: PerspectiveTransformation
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim nativeMe As JavaObject
    Dim bm1, bm2 As Bitmap

    Private ImageView1 As ImageView
    Private ImageView2 As ImageView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("main")
   
    nativeMe.InitializeContext
    bm1.Initialize(File.DirAssets,"33RfN.png")

    ImageView1.Bitmap = bm1
    Log(bm1.Width)
    Log(bm1.Height)
   
    Dim source() As Float = Array As Float(100.0, 0.0, 400.0, 170.0, 375.0, 380.0, 0.0, 190.0)
   
    ImageView2.Bitmap = nativeMe.RunMethod("perspectiveTransformation", Array(bm1, source))

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub


#If Java

import android.graphics.Matrix;
import android.graphics.Bitmap;

public static Bitmap perspectiveTransformation(Bitmap bitmap, float[] src)
{
    Matrix matrix = new Matrix();
    float[] dst = new float[] {
            0,
            0,
            bitmap.getWidth(),
            0,
            bitmap.getWidth(),
            bitmap.getHeight(),
            0,
            bitmap.getHeight()
    };
//    float[] src = new float[] {
//            boundingQuad.topLeft.x,
//            boundingQuad.topLeft.y,
//            boundingQuad.topRight.x,
//            boundingQuad.topRight.y,
//            boundingQuad.bottomRight.x,
//            boundingQuad.bottomRight.y,
//            boundingQuad.bottomLeft.x,
//            boundingQuad.bottomLeft.y
//    };
    matrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1);
    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}



#End If

 

Attachments

  • b4aPerspectiveTransformation.zip
    232.4 KB · Views: 285
Upvote 0

BeneBarros

Active Member
Licensed User
Longtime User
Sensational!!! Exactly what I needed.
Thank you very much for your attention. It is good to know that we have great masters sharing their knowledge.
My knowledge in Java is very little.
I will strive to learn more.
Thank you. Thank you. Thank you.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…