DrawBitmapRotated ?

ozgureffe

Member
Licensed User
Longtime User
I am trying to resize and rotate a bitmap with the function DrawBitmapRotated.

But I am wrong at somewhere. Does DrawBitmapRotated has ability to resize/resample while destination rectange is smaller than the source rectangle?
B4X:
Sub btn_save_Click
    Dim Out As OutputStream
    Dim Smaller As Bitmap
    bmp_note.Initialize3(cnvs_note.Bitmap)
    Smaller = ResizeRotateBitmap(bmp_note, 440, 60)
    fileName = DateTime.Now & ".PNG"
    Out = File.OpenOutput(File.DirRootExternal & "/img",fileName, False)
    Smaller.WriteToStream(out, 100, "PNG")
    Out.Close
End Sub

Sub ResizeRotateBitmap(src_bmp As Bitmap, w As Int, h As Int) As Bitmap
    Dim new As Bitmap
    new.InitializeMutable(w, h)
    Dim c As Canvas
    c.Initialize2(new)
    Dim dst_rect As Rect
    dst_rect.Initialize(0, 0, w, h)
    c.DrawBitmapRotated (src_bmp,Null, dst_rect,270)
    Return new
End Sub

Please someone explain me how to use DrawBitmapRotated while source and destination rectangles are differently sized and not square.
Thank you.
 
Last edited:

birnesoft

Active Member
Licensed User
Longtime User
rotate image and save

The best way I found,
first scale it to a square of picturewidth^2, (if not you get crazy :BangHead:)
rotate it and scale it down to the new size.

sub button1_Click
Dim bmp,smaller As Bitmap
Dim fn As String

root=File.DirRootExternal
path1 = root & "/DCIM/Camera"
fn="TEST.JPG"

bmp.Initialize(path1, fn)
smaller.Initialize3(bmp)
smaller = ResizeRotateBitmap(bmp, 440,,440)
smaller=CreateScaledBitmap(smaller, 60, 440, True)
out = File.OpenOutput(path1 ,fn, False)
smaller.WriteToStream(out, 80, "JPEG")
out.Close
End Sub

Sub ResizeRotateBitmap(src_bmp As Bitmap, w As Int, h As Int) As Bitmap
Dim new As Bitmap
new.InitializeMutable(w, h)
Dim c As Canvas
c.Initialize2(new)
Dim dst_rect As Rect
dst_rect.Initialize(0,0, w, h)
c.DrawBitmapRotated (src_bmp,Null, dst_rect,90)
Return new
End Sub

Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int, Filter As Boolean) As Bitmap
Dim r As Reflector
Dim b As Bitmap
b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
Array As Object(Original, Width, Height, Filter), _
Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
Return b
End Sub

>:)-D)Björn
 
Upvote 0

cocobill

New Member
thx Birnesoft for idea.
Rotate any image, squared or not

B4X:
Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.
   
   Dim resu As ImageView 'in designer
   
   Dim masterPic As Bitmap
   Dim bb As Bitmap
   
   Dim angle As Int : angle = 90
   
   'original size
   Dim pwo As Int
   Dim pho As Int
   
   'resized to ...
   Dim sqp As Int
   
   Dim Recto As Rect
   Dim c As Canvas
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
      
   masterPic.Initialize(File.DirAssets,"testpic2.png") '300 x 172 pix
   
'   masterPic.Initialize(File.DirAssets,"testpic2.png") '300 x 300 pix
   
   pwo = masterPic.Width
   pho = masterPic.Height
   
   resu.Width = pwo
   resu.Height = pho
   resu.Gravity=Gravity.FILL
   resu.Bitmap=masterPic

End Sub
Sub resu_Click
   
   pwo = masterPic.Width
   pho = masterPic.Height
   
   sqp = pwo 'for square imaqe
   
   If pwo <> pho Then
      'set longer side
      sqp = pho
      If pwo > pho Then sqp = pwo
      
      'first RESIZE to square
      bb = CreateScaledBitmap (masterPic, sqp, sqp, True)
   End If
   
   bb.InitializeMutable(sqp,sqp)
   Recto.Initialize(0, 0,sqp,sqp)

   'ROTATE masterPic into bb
   c.Initialize2(bb)
   c.DrawBitmapRotated(masterPic, Null, Recto, angle) 'Draw the new image
   masterPic.Initialize3(bb)
   
   'RESIZE back inverse, if pic was not square
   If pwo <> pho Then
      masterPic = CreateScaledBitmap (bb, pho, pwo, True)
   End If
   
   'show
   resu.Width = masterPic.Width
   resu.Height = masterPic.Height
   resu.Gravity=Gravity.FILL
   resu.Bitmap = masterPic
   
End Sub

Sub CreateScaledBitmap(Original As Bitmap, Width As Int, Height As Int, Filter As Boolean) As Bitmap
Dim r As Reflector
Dim b As Bitmap
b = r.RunStaticMethod("android.graphics.Bitmap", "createScaledBitmap", _
Array As Object(Original, Width, Height, Filter), _
Array As String("android.graphics.Bitmap", "java.lang.int", "java.lang.int", "java.lang.boolean"))
Return b
End Sub
 
Last edited:
Upvote 0
Top