B4J Question Rotate bitmap by 12.3 degrees without distortion of edges ?

peacemaker

Expert
Licensed User
Longtime User
Hi, All

How to rotate bitmap precisely and without loosing edges in B4J?
https://www.b4x.com/android/forum/threads/b4x-rotate-bitmap.125957/ is OK only for 1st task...

1737209056415.png
 
Solution
rotated bitmap
Thanks, indeed !
Now Erels's sub version can be:
B4X:
Sub RotateBitmap (bmp As B4XBitmap, Degrees As Float) As B4XBitmap
    Dim cvs As B4XCanvas, rectTest As B4XRect
    Dim panel As B4XView = xui.CreatePanel("")
    rectTest.Initialize(0, 0, bmp.Width, bmp.Height)
    panel.Width = bmp.Width * Abs(CosD(Degrees)) + bmp.Height * Abs(SinD(Degrees))
    panel.Height = bmp.Width * Abs(SinD(Degrees)) + bmp.Height * Abs(CosD(Degrees))
    rectTest.Initialize(bmp.Height * Abs(SinD(Degrees)) / 2, bmp.Width * Abs(SinD(Degrees)) / 2, bmp.Width + bmp.Height * Abs(SinD(Degrees)) / 2, bmp.Height + bmp.Width * Abs(SinD(Degrees)) / 2)
    cvs.Initialize(panel)
    cvs.DrawBitmapRotated(bmp, rectTest, Degrees)...

agraham

Expert
Licensed User
Longtime User
is OK only for 1st task...
Not sure what you mean by this but when rotating images you should always start with the original image and rotate it by how much you want. If you rotate already rotated images then the quality will inevitably suffer and there is no way to avoid it.
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Sure, the source is not distorted used.
Question is to prepare bigger white container for the rotated picture for not to lost the corners

B4X:
    Dim NewHeight As Int = bmp.Width * SinD(Degrees)
    Dim NewWidth As Int = bmp.Width * CosD(Degrees)
 
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
Task is to rotate precisely without losing corners, like here: whole window on the picture is fully rotated without padding:
1737210164356.png
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
In the attached project you find the two solutions:
- Rotate an ImageView with the bitmap.
- Rotate a bitmap on a resized Panel.

1737214124968.png


On top the rotated ImageView and below the rotated bitmap on a resized Panel.

And the code:
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    
    Private pnlTest, imvTest As B4XView
    Private cvsTest As B4XCanvas
    Private bmpTest As B4XBitmap
    Private rectTest As B4XRect
    
    Private Angle As Double
End Sub

Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
    
    Angle = 12.3
    
    'rotated ImageView
    bmpTest = xui.LoadBitmap(File.DirAssets, "Circuit.png")
    imvTest.Width = bmpTest.Width
    imvTest.Height = bmpTest.Height
    imvTest.SetBitmap(bmpTest)
    imvTest.Rotation = 12.3
    
    Sleep(0)
    'rotated bitmap
    rectTest.Initialize(0, 0, bmpTest.Width, bmpTest.Height)
    pnlTest.Width = bmpTest.Width * CosD(Angle) + bmpTest.Height * SinD(Angle)
    pnlTest.Height = bmpTest.Width * SinD(Angle) + bmpTest.Height * CosD(Angle)
    rectTest.Initialize(bmpTest.Height * SinD(Angle) / 2, bmpTest.Width * SinD(Angle) / 2, bmpTest.Width + bmpTest.Height * SinD(Angle) / 2, bmpTest.Height + bmpTest.Width * SinD(Angle) / 2)
    cvsTest.Initialize(pnlTest)
    cvsTest.DrawBitmapRotated(bmpTest, rectTest, Angle)
End Sub

Only tested With B4J.
 

Attachments

  • RotateBitmap.zip
    180.6 KB · Views: 36
Upvote 0

peacemaker

Expert
Licensed User
Longtime User
rotated bitmap
Thanks, indeed !
Now Erels's sub version can be:
B4X:
Sub RotateBitmap (bmp As B4XBitmap, Degrees As Float) As B4XBitmap
    Dim cvs As B4XCanvas, rectTest As B4XRect
    Dim panel As B4XView = xui.CreatePanel("")
    rectTest.Initialize(0, 0, bmp.Width, bmp.Height)
    panel.Width = bmp.Width * Abs(CosD(Degrees)) + bmp.Height * Abs(SinD(Degrees))
    panel.Height = bmp.Width * Abs(SinD(Degrees)) + bmp.Height * Abs(CosD(Degrees))
    rectTest.Initialize(bmp.Height * Abs(SinD(Degrees)) / 2, bmp.Width * Abs(SinD(Degrees)) / 2, bmp.Width + bmp.Height * Abs(SinD(Degrees)) / 2, bmp.Height + bmp.Width * Abs(SinD(Degrees)) / 2)
    cvs.Initialize(panel)
    cvs.DrawBitmapRotated(bmp, rectTest, Degrees)
    cvs.Invalidate
    Dim b As B4XBitmap = cvs.CreateBitmap
    cvs.Release
    Return b
End Sub
1737226626395.png
 
Last edited:
Upvote 0
Solution
Top