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
This is the image after running the code
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
This is the image after running the code
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.b4x.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: