Create scaled bitmap produces jagged edges

tamadon

Active Member
Licensed User
Longtime User
I am using the following code to scale images

B4X:
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

The original image size is 512x512 px. The scaled size randomly set between 50x50px to 120x120px

I noticed the resulting scaled images always have jagged edges. I tried setting the Filter to both True or False but the result is the same.
 

tamadon

Active Member
Licensed User
Longtime User
After scaling, I rotate and place the image into imageviews

B4X:
   For i = 0 To 2
      Dim btnSize As  Int
      Dim bmpTemp As Bitmap
      
      Dim cvs As Canvas
      Dim rectDest As Rect
         
      btnSize = 10 * Rnd(4, 12)
      
      imvTop(i).Initialize("imvTop")
      pnlTop.AddView(imvTop(i), Rnd(0, pnlTop.Width - btnSize - 20), Rnd(0, pnlTop.Height - btnSize - 20), btnSize, btnSize) 
               
'      First resize To temporarily bitmap
      bmpTemp = matchcode.CreateScaledBitmap(bmpIcon(i), btnSize, btnSize,  True)
      

         
      bmpTemp.InitializeMutable(btnSize, btnSize)
      rectDest.Initialize(0, 0, btnSize, btnSize)
      
'      'Rotate bmpIcon into bmpTemp
      cvs.Initialize2(bmpTemp)
      cvs.DrawBitmapRotated(bmpIcon(i), Null, rectDest, Rnd(0, 360)) 'Draw the new image

      'matchcode.SetAntiAlias(cvs, 1)
   
      bmpIcon(i).Initialize3(bmpTemp)
      
      imvTop(i).Gravity = Gravity.FILL   
      imvTop(i).Bitmap = bmpIcon(i)
      

   Next
 
Upvote 0

tamadon

Active Member
Licensed User
Longtime User
I tried to enable antialiasing using this example

http://www.b4x.com/forum/basic4android-updates-questions/14579-how-set-anti_alias_flag.html

After searching the Internet I found the following but not sure how to convert the code into B4A. It appears that I have to ensure to use 32-bit ARGB_8888 image?

android - Quality problems when resizing an image at runtime - Stack Overflow

Tried setting setDither but with the following code but problem is still there
B4X:
      Dim r As Reflector
      r.Target = cvs
      r.Target = r.GetField("paint")
      r.RunMethod2("setDither", True, "java.lang.boolean")
 
Upvote 0

tamadon

Active Member
Licensed User
Longtime User
Thanks for the suggestion Informatix.

Do you have any suggestion how can I modified the RotateBmp sub to rotate at a specified angle?

EDIT: Never mind I think I managed to do it
 
Last edited:
Upvote 0
Top