Android Question [SOLVED] text to picture (watermark)

madru

Active Member
Licensed User
Longtime User
hi guys,

how can stamp some text into a picture taken from camEx without changing the original picture DPI, resolution, zoom etc., actually leave the picture as it is


i tried the two following approaches without real success


B4X:
Sub Camera1_PictureTaken (Data() As Byte)


    ' save the original picture'
    ' picture 1.jpg'
    camEx.SavePictureToFile(Data, rp.GetSafeDirDefaultExternal(""), "1.jpg")
    
    
    '############################################################################################################################################

    'does not deal with aspect ratio correctly (canvas.Initialize), or i am using it not correct'
    'picture out.jpg
    ImageView1.Bitmap = BytesToBitmap(Data)
    canvas.Initialize(ImageView1) ' canvas has the wrong x,y resolution now how, to resize for picture size
    canvas.DrawText("some text", ImageView1.Width/2 , ImageView1.Height - 20dip ,Typeface.DEFAULT , 15, Colors.Blue, "CENTER") 
    'ImageView1.Invalidate

    b = ImageView1.Bitmap
    Dim out As OutputStream
    out = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), "out.jpg", False)
    b.WriteToStream(out, 100, "JPEG")
    out.Close
    '############################################################################################################################################

    'RSImageEffects 'zooms' somehow into the picture
    'picture out1.jpg
    Dim BitmapTemp As Bitmap
    BitmapTemp.Initialize3(BytesToBitmap(Data))
    BitmapTemp = rs.Watermark(BitmapTemp,"some text",BitmapTemp.Width/2 , BitmapTemp.Height - 20dip,Colors.red,255,80,False)
    
    Dim out As OutputStream
    out = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), "out1.jpg", False)
    BitmapTemp.WriteToStream(out, 100, "JPEG")
    out.Close
    '############################################################################################################################################

    camEx.StartPreview 'restart preview


End Sub

pictures are reduced to 50% JPG quality due to upload limit
 

Attachments

  • 1.jpeg
    1.jpeg
    158.1 KB · Views: 188
  • out.jpeg
    out.jpeg
    28.9 KB · Views: 190
  • out1.jpeg
    out1.jpeg
    138.3 KB · Views: 193

roumei

Active Member
Licensed User
You need to make sure that the aspect ratio of the ImageView and the Image are the same.

Or you could use a mutable bitmap to preserve the original resolution and aspect ratio:
B4X:
   ' Load bitmap
    Dim bmpInput As Bitmap = LoadBitmap(File.DirAssets, "Test.jpg")
    
    ' Create a mutable bitmap and draw the input bitmap
    Dim bmpMutable As Bitmap
    bmpMutable.InitializeMutable(bmpInput.Width, bmpInput.Height)
    Dim cvs As Canvas
    cvs.Initialize2(bmpMutable)
    Dim DestRect As Rect
    DestRect.Initialize(0, 0, bmpInput.Width, bmpInput.Height)
    cvs.DrawBitmap(bmpInput, Null, DestRect)

    ' Watermark
    cvs.DrawText("some text", bmpInput.Width/2 , bmpInput.Height - 20dip ,Typeface.DEFAULT , 15, Colors.Blue, "CENTER")
    
    ' Write output bitmap
    Dim Out As OutputStream
    Out = File.OpenOutput(rp.GetSafeDirDefaultExternal(""), "TestOut.jpg", False)
    cvs.Bitmap.WriteToStream(Out, 100, "JPEG")
    Out.Close
 
Upvote 0
Top