Android Tutorial Create dynamic jpg using b4a and php

DonManfred

Expert
Licensed User
With this samplecode i will demonstrate to create a dynamic jpg from other image, put text´s on it, copy another image to it...

ATTENTION: This b4a-example uses CreateMap so you need B4A V3.8 for it to work...

The logic "in app" is simple.
- A list with commands is created and send to php-script.
- The php-script do all given commands in order to create respectively change an image.
- The image is then written to disc and the url to this image is returned to the app.
- The app see this url and download this image
- After finished downloading the image it is showed up in a pictureviewer on the device.

This is the Original
original.jpg

This is the image after running the code
after.jpg

B4X:
Dim cmds As List
cmds.Initialize
' "init" should be the first action
' The image from imageurl is loaded as basis for the future actions
cmds.Add(CreateMap("action": "init", "imageurl": "http://pdf.basic4android.de/20140126_100310.jpg","type":"jpg"))
'
' SetText writes a text into the pdf.
' border can be one OR more chars out of L, B, T, R (resp. Left, Bottom, Top, Right)
' x, y, w and h are the position and width/height of textbox
' r, g, b are the color for this text in RGB-format
' align can be L,C,R (leftjustify, center, rightjustify
cmds.Add(CreateMap("action": "SetText", "text": "Zora", "angle": 0,"x": 650, "y": 700, "r": 255,"g": 0,"b": 0,"size": 50,"font":"arial.ttf"))
cmds.Add(CreateMap("action": "SetText", "text": "Lucky", "angle": 260,"x": 700, "y": 210, "r": 255,"g": 255,"b": 255,"size": 50,"font":"arial.ttf"))
cmds.Add(CreateMap("action": "SetText", "text": "Charly", "angle": 30,"x": 250, "y": 500, "r": 255,"g": 255,"b": 255,"size": 50,"font":"arial.ttf"))
'
' SetImage copy a image into the basis-image
' imgurl = url of the image
' dst_x and dst_y is the position in basis-image where the part of the image in imageurl
' at position src_x and src_y with the dimension src_w, src_h would be copied
' type can be jpg or png
cmds.Add(CreateMap("action": "SetImage", "imageurl": "http://www.basic4ppc.com/new_images/b4a_logo.png", "dst_x": 450, "dst_y": 550, "src_x": 0, "src_y": 0,"src_w": 155,"src_h": 84,"type":"png"))

Dim json As JSONGenerator
json.Initialize2(cmds)
Log(json.ToString)
Dim job As HttpJob
Dim p As PhoneId
job.Initialize("createjpg", Me)
job.download2("http://pdf.basic4android.de/createimage.php", Array As String( _
    "json", json.ToString, _
    "DeviceID", p.GetDeviceId _
))
B4X:
Sub JobDone(job As HttpJob)
  ProgressDialogHide
  If job.Success = True Then
      If job.JobName = "getimg" Then
        Dim OutStream As OutputStream
      Log("DownloadReady: "&job.Tag)
      OutStream = File.OpenOutput(File.DirRootExternal, job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
            File.Copy2(job.GetInputStream,OutStream) ' save the file
        OutStream.Close
        Log(job.Tag&" written to "&File.DirRootExternal) ' Write the Originalname to the log to see what happens ;-)
     
            'send the intent that asks the media scanner to scan the file
            Dim i As Intent
            i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
                "file://" & File.Combine(File.DirRootExternal, job.Tag))
            Dim p As Phone
            p.SendBroadcastIntent(i)
 
            Dim i As Intent
          i.Initialize(i.ACTION_VIEW,"file://" & File.Combine(File.DirRootExternal, job.Tag))
          i.SetType("image/*")
          StartActivity(i)
            'Dim i As Intent
            'i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirRootExternal & "/", job.Tag))
            'i.SetComponent("android/com.android.internal.app.ResolverActivity")
            'i.SetType("application/pdf")
            'StartActivity(i)
        End If
      If job.JobName = "createjpg" Then
            Log(job.GetString)
            Dim parser As JSONParser
            parser.Initialize(job.GetString)
            Dim m As Map = parser.NextObject
            Dim img As String = m.Get("image")
            Dim resultcode As Int = m.Get("resultcode")
            If resultcode = 0 Then
                If img <> "" Then
                    Dim job As HttpJob
                    job.Initialize("getimg", Me)
                    job.Tag = "result.jpg"
                    job.download(img)
                End If     
            End If
        End If
    Else
        Log("Error: " & job.ErrorMessage)
  End If
End Sub
 

Attachments

Last edited:

DonManfred

Expert
Licensed User
Here another example

B4X:
    Dim cards As List
    cards.Initialize
    cards.Add("ace")
    cards.Add("2")
    cards.Add("3")
    cards.Add("4")
    cards.Add("5")
    cards.Add("6")
    cards.Add("7")
    cards.Add("8")
    cards.Add("9")
    cards.Add("10")
    cards.Add("J")
    cards.Add("Q")
    cards.Add("K")


    Dim cmds As List
    cmds.Initialize
    ' "init" should be the first action
    ' The image from imageurl is loaded as basis for the future actions
    cmds.Add(CreateMap("action": "init", "imageurl": "http://pdf.basic4android.de/decks/classic-playing-cards.png","type":"png","destinationname":"card"))

    Dim rowx,rowy As Int

    ' Row 1 - clubs
    rowy = 0
    For i = 0 To cards.Size-1
        cmds.Add(CreateMap("action": "copy2jpg", "name": "clubs-"&cards.Get(i), "x": (i*60), "y": rowy, "w": 60,"h":80))
    Next
    ' Row 2 - spade
    rowy = 80
    For i = 0 To cards.Size-1
        cmds.Add(CreateMap("action": "copy2jpg", "name": "spade-"&cards.Get(i), "x": (i*60), "y": rowy, "w": 60,"h":80))
    Next
    ' Row 1 - clubs
    rowy = 160
    For i = 0 To cards.Size-1
        cmds.Add(CreateMap("action": "copy2jpg", "name": "heart-"&cards.Get(i), "x": (i*60), "y": rowy, "w": 60,"h":80))
    Next
    ' Row 1 - clubs
    rowy = 240
    For i = 0 To cards.Size-1
        cmds.Add(CreateMap("action": "copy2jpg", "name": "diamond-"&cards.Get(i), "x": (i*60), "y": rowy, "w": 60,"h":80))
    Next

    Dim json As JSONGenerator
    json.Initialize2(cmds)
    Log(json.ToString)
    Dim job As HttpJob
    Dim p As PhoneId
    job.Initialize("createjpg", Me)
    job.download2("http://pdf.basic4android.de/createimage.php", Array As String( _
        "json", json.ToString, _
        "DeviceID", p.GetDeviceId _
    ))
B4X:
Sub JobDone(job As HttpJob)
  ProgressDialogHide
  If job.Success = True Then
      If job.JobName = "getimg" Then
        Dim OutStream As OutputStream
      Log("DownloadReady: "&job.Tag)
      OutStream = File.OpenOutput(File.DirRootExternal, job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
            File.Copy2(job.GetInputStream,OutStream) ' save the file
        OutStream.Close
        Log(job.Tag&" written to "&File.DirRootExternal) ' Write the Originalname to the log to see what happens ;-)
        
            'send the intent that asks the media scanner to scan the file
            Dim i As Intent
            i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
                "file://" & File.Combine(File.DirRootExternal, job.Tag))
            Dim p As Phone
            p.SendBroadcastIntent(i)
    
            Dim i As Intent
          i.Initialize(i.ACTION_VIEW,"file://" & File.Combine(File.DirRootExternal, job.Tag))
          i.SetType("image/*")
          StartActivity(i)
            'Dim i As Intent
            'i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirRootExternal & "/", job.Tag))
            'i.SetComponent("android/com.android.internal.app.ResolverActivity")
            'i.SetType("application/pdf")
            'StartActivity(i)
        End If
      If job.JobName = "getimg2" Then
        Dim OutStream As OutputStream
      Log("DownloadReady: "&job.Tag)
      OutStream = File.OpenOutput(File.DirRootExternal, job.Tag, False) ' Job.Tag is read to set the Original Filename we specify earlier in the creation of the Job
            File.Copy2(job.GetInputStream,OutStream) ' save the file
        OutStream.Close
        Log(job.Tag&" written to "&File.DirRootExternal) ' Write the Originalname to the log to see what happens ;-)
        
            'send the intent that asks the media scanner to scan the file
            Dim i As Intent
            i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", _
                "file://" & File.Combine(File.DirRootExternal, job.Tag))
            Dim p As Phone
            p.SendBroadcastIntent(i)
        End If
      If job.JobName = "createjpg" Then
            Log(job.GetString)

            Dim parser As JSONParser
            parser.Initialize(job.GetString)
            Dim m As Map = parser.NextObject
            Dim img As String = m.Get("image")
            Dim resultcode As Int = m.Get("resultcode")
            If resultcode = 0 Then
                Dim newimages As List = m.Get("newimages")
                For Each colnewimages As String In newimages
                    If colnewimages <> "" Then
                        Dim job As HttpJob
                        job.Initialize("getimg2", Me)
                        job.Tag = GetFilename(colnewimages)
                        job.download(colnewimages)
                    End If        
            
                Next

            End If
        End If
    Else
        Log("Error: " & job.ErrorMessage)
  End If  
End Sub
Sub GetFilename(fullpath As String) As String
  Return fullpath.SubString(fullpath.LastIndexOf("/") + 1)
End Sub
This is the "starting-image"


The created single Card-images you find in Cards.zip

Please note that you need an updated php-script. V1.1 of php-script is updated in Post #1
 

Attachments

Top