Android Question Create a mutable bitmap and draw over it with Canvas

cenyu

Active Member
Licensed User
Longtime User
Hi, i need some example for loading an image into mutable bitmap and draw text over it with Canvas.

My image 1.jpg is image with - let's say a dog on it.

I try this code:

B4X:
    Dim bmp As Bitmap = LoadBitmap(File.DirAssets, "1.jpg")
    Dim cvs As Canvas
     Dim s as String = "Hello dog!"
    bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
    
    cvs.Initialize2(bmp)
    
    Dim h As Double = cvs.MeasureStringHeight(s, Typeface.DEFAULT, FontSize)
    cvs.DrawText(s, bmp.Width / 2, bmp.Height / 2 + h / 2, Typeface.DEFAULT, FontSize, lFontColour, "CENTER")

but i see only black image with text into it.
when i put cvs.DrawColor(Colors.Green) i see green background with Hello Dog on it.
But my idea is to draw Hello dog on image with dog picture

Where i am wrong?
 
Solution
This should work:
B4X:
    Dim cvs As Canvas
    Dim bmp As Bitmap
    bmp.InitializeMutable(800 * 100dip / 100, 600 * 100dip / 100)
 
    cvs.Initialize2(bmp)
    Dim rct As Rect
    rct.Initialize(0, 0, 800* 100dip / 100, 600* 100dip / 100)
    cvs.DrawBitmap(LoadBitmap(File.DirAssets, "1.jpg", Null, rct)

Attached a test project i had written some time ago.

klaus

Expert
Licensed User
Longtime User
This line initializes a black bitmap.
B4X:
bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
To get the image on the bitmap you need to draw it with the Canvas.
B4X:
    Dim rct As Rect
    rct.Initialize(0, 0, iWidth* 100dip / 10, iHeight* 100dip / 100)
    cvsTest.DrawBitmap(LoadBitmap(File.DirAssets, "1.jpg"), Null, rct
 
Upvote 0

cenyu

Active Member
Licensed User
Longtime User
But without
B4X:
bmp.InitializeMutable(iWidth * 100dip / 100, iHeight * 100dip / 100)
i geting error
Java:
java.lang.RuntimeException: Bitmap is not mutable.

I change my code like this:
B4X:
    Dim rc As Rect
    Dim r As Reflector
    Dim cvs As Canvas
 
    Dim bmp As Bitmap
    bmp.InitializeMutable(800 * 100dip / 100, 600 * 100dip / 100)
    bmp = LoadBitmap(File.DirAssets, "1.jpg")
 
    cvs.Initialize2(bmp)
    Dim rct As Rect
    rct.Initialize(0, 0, 800* 100dip / 10, 600* 100dip / 100)
    cvs.DrawBitmap(cvs.Bitmap, Null, rct)

Do i have to initialize bmp as mutable and then load file - like row 6 and 7?
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
This should work:
B4X:
    Dim cvs As Canvas
    Dim bmp As Bitmap
    bmp.InitializeMutable(800 * 100dip / 100, 600 * 100dip / 100)
 
    cvs.Initialize2(bmp)
    Dim rct As Rect
    rct.Initialize(0, 0, 800* 100dip / 100, 600* 100dip / 100)
    cvs.DrawBitmap(LoadBitmap(File.DirAssets, "1.jpg", Null, rct)

Attached a test project i had written some time ago.
 

Attachments

  • TextOnImage.zip
    53.8 KB · Views: 186
Upvote 0
Solution

cenyu

Active Member
Licensed User
Longtime User
This is great! Thank you very much Klaus!
I appreciate your help very much!
 
Upvote 0
Top