Android Question Adding a label to an image in Camera2

Cliff McKibbin

Member
Licensed User
Longtime User
I am using Camera2 to take a picture and save it to the download directory using the logic from the Camera2 sample as follows.
Note that I am currently setting the name of the jpg file to the date, time, latitude, and longitude of the picture.

B4X:
Sub TakePicture
  Try
        xErr="Focus and Take Picture"
        Wait For(cam.FocusAndTakePicture(MyTaskIndex)) Complete (Data() As Byte)
        Dim bmp As Bitmap = cam.DataToBitmap(Data)
        ' set name of jpg
        x="LL" & Starter.llnr 
        DateTime.DateFormat="yyyy-MM-dd hh-mm "
        X=x & DateTime.Date (MarkTime)
        X=x & "Lat " & ActLat & " Lg " & ActLg
'        Log ("Actlat " & ActLat)
        X=x & ".jpg"
        xErr="Data to File"
        cam.DataToFile(Data, VideoFileDir, x)

   Catch
        MsgboxAsync (xErr, "Error in Take Picture")
        Return    
    End Try
 End Sub



In
Erel gives a method to add an image of a compass to an image as follows.

B4X:
Dim bmp As B4XBitmap = cam.DataToBitmap(Data)
Dim bmp2 As B4XBitmap = LoadBitmap(File.DirAssets, "android-chrome-192x192.png")
Dim bc As BitmapCreator
bc.Initialize(bmp.Width, bmp.Height)
bc.CopyPixelsFromBitmap(bmp)
Dim r As B4XRect
r.Initialize(20dip, 20dip, 0, 0)
r.Width = bmp2.Width
r.Height = bmp2.Height
bc.DrawBitmap(bmp2, r, False)
Dim mergedbmp As B4XBitmap = bc.Bitmap

I would like to add a label with the latitude and longitude of the location of the picture.
Preferably, the label would be transparent and across the bottom of the picture.
The image with the label would be downloaded.

Any help would be appreciated,
Thanks, Cl;iff McKibbin
 

JohnC

Expert
Licensed User
Longtime User
(the below is some of my own code edited by AI for this post)

You can draw the latitude and longitude directly onto the bitmap with a Canvas, then overwrite the original JPEG or save it under a new filename.

I normally draw the text twice: first in black with a slight offset to create a shadow, then again in a lighter color. This keeps the text readable without adding a solid background across the image.

B4X:
Sub AddLocationToImage(Dir As String, FileName As String, Latitude As Double, Longitude As Double)

    Dim iv As ImageView
    iv.Initialize("")
    iv.Bitmap = LoadBitmap(Dir, FileName)

    'The ImageView must have dimensions before initializing the Canvas.
    iv.Width = iv.Bitmap.Width
    iv.Height = iv.Bitmap.Height

    Dim cvs As Canvas
    cvs.Initialize(iv)

    Dim LocationText As String
    LocationText = NumberFormat2(Latitude, 1, 6, 6, False) & _
        ", " & NumberFormat2(Longitude, 1, 6, 6, False)

    Dim TextSize As Float = 16
    Dim LeftPosition As Float = 10dip
    Dim BottomPosition As Float = iv.Height - 10dip

    'Draw a black shadow slightly down and to the right.
    cvs.DrawText(LocationText, _
        LeftPosition + 1dip, _
        BottomPosition + 1dip, _
        Typeface.DEFAULT_BOLD, _
        TextSize, _
        Colors.Black, _
        "LEFT")

    'Draw the visible text over the shadow.
    cvs.DrawText(LocationText, _
        LeftPosition, _
        BottomPosition, _
        Typeface.DEFAULT_BOLD, _
        TextSize, _
        Colors.Yellow, _
        "LEFT")

    'Save the modified image.
    Dim Out As OutputStream
    Out = File.OpenOutput(Dir, FileName, False)
    cvs.Bitmap.WriteToStream(Out, 90, "JPEG")
    Out.Close

End Sub

Call it after DataToFile has saved the picture:

cam.DataToFile(Data, VideoFileDir, x)
AddLocationToImage(VideoFileDir, x, ActLat, ActLg)

The overlay itself is transparent because only the text and its shadow are drawn. There is no filled rectangle behind it.

One thing to watch is image scaling. The example sets the ImageView dimensions to the bitmap’s actual pixel dimensions so that the text is drawn onto the full-resolution image rather than a scaled display copy.
 
Upvote 0
Top