Continuing saving images Lansa mobile

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
atostaine
Posts: 696
Joined: Wed Jan 20, 2016 7:38 am

Continuing saving images Lansa mobile

Post by atostaine »

This thread shows how to get base64 from the camera into a CLOB.

viewtopic.php?f=3&t=1386&p=2861&hilit=image#p2908

I'm trying to get a BLOB though. How can I do that? Thanks
Art Tostaine
atostaine
Posts: 696
Joined: Wed Jan 20, 2016 7:38 am

Re: Continuing saving images Lansa mobile

Post by atostaine »

While I wait for SP2, I researched other options to convert the BASE64 to a BLOB. Python seemed simple enough, is available on V7Rx on i and most other platforms. Research IBM 5733OPS to install IBM's open source options.

The script below works great for me. I saved it in as /pyscripts/decodebase64.py. I'll wrap it in a function and call from my server module, then use the outputfile for my BLOB. I've never written python before and did all this tonight by googling.

You can run it from IBM i command line like this:
QSH CMD('python3 /pyscripts/decodebase64.py -i /tmp/image642.txt -o /tmp/image642_new.jpg')

[code
import base64
import sys, getopt

def main(argv):
inputfile = ''
outputfile = ''
try:
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except getopt.GetoptError:
print ('decodebase64.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('decodebase64.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg

input_file = open(inputfile, 'r')

coded_string = input_file.read()
decoded = base64.b64decode(coded_string)
output_file = open(outputfile, 'wb')
output_file.write(decoded)
output_file.close()

if __name__ == "__main__":
main(sys.argv[1:])

[/code]
Art Tostaine
tsupartono

Re: Continuing saving images Lansa mobile

Post by tsupartono »

If you install EPC141070 (for SP1), you can use XPRIM_Binary to convert your Base64 string to bytes and write it to a file.
Assuming your Base64 data is in a variable called #MyBase64String, see example below.

Code: Select all

Define_Com Class(#XPRIM_Binary) Name(#BinaryData)

#BinaryData.FromBase64String String(#MyBase64String)

#STD_BLOB := #BinaryData.AsFile
You can find the documentation for XPRIM_Binary here:
http://docs.lansa.com/14/en/lansa015/in ... 0_0010.htm
Last edited by tsupartono on Tue Nov 14, 2017 8:04 am, edited 1 time in total.
atostaine
Posts: 696
Joined: Wed Jan 20, 2016 7:38 am

Re: Continuing saving images Lansa mobile

Post by atostaine »

Thanks. USA tech support didn't know about this. I'll install it and check it out.

Art
Art Tostaine
User avatar
lawingo
Posts: 78
Joined: Fri Dec 04, 2015 6:41 am

Re: Continuing saving images Lansa mobile

Post by lawingo »

I originally posted the topic you referenced. Forget that original Post - Use the "File Picker". It is much simpler and will allow you save your images as BLOBs. The "CameraControl" Widget is a bit misleading. FilePicker does the exact same thing but allows you to use Blobs.

Code: Select all

Evtroutine Handling(#SelectFile.FileSelected) File(#File)

Signal Event(uSelectedImage) Selectedimage(#File.blob)
Endroutine

Evtroutine Handling(#webFilePicker.uSelectedImage) Selectedimage(#mImage)

#Image1.FileName := #mImage
#EMEIIMG := #mImage

Endroutine



atostaine
Posts: 696
Joined: Wed Jan 20, 2016 7:38 am

Re: Continuing saving images Lansa mobile

Post by atostaine »

I need to scan barcodes also so I'm using LANSA Mobile. Is there a way to scan barcodes without it?

Thanks, Art
Art Tostaine
atostaine
Posts: 696
Joined: Wed Jan 20, 2016 7:38 am

Re: Continuing saving images Lansa mobile

Post by atostaine »

tsupartono wrote: Tue Oct 24, 2017 6:20 pm If you install EPC141070 (for SP1), you can use XPRIM_Binary to convert your Base64 string to bytes and write it to a file.
Assuming your Base64 data is in a variable called #MyBase64String, see example below.

Code: Select all

Define_Com Class(#XPRIM_Binary) Name(#BinaryData)

#BinaryData.FromBase64String(#MyBase64String)

#STD_BLOB := #BinaryData.AsFile
You can find the documentation for XPRIM_Binary here:
http://docs.lansa.com/14/en/lansa015/in ... 0_0010.htm
How do I define the base64string? Previously I was using a PRIM_ALPH field to hold the base64 string.

Thank you.
Art Tostaine
tsupartono

Re: Continuing saving images Lansa mobile

Post by tsupartono »

You can still use PRIM_ALPH.

I noticed in the example I provided, the FromBase64String invocation is missing the String parameter name.
The String parameter name does need to be specified.
Please see the corrected code below.

Code: Select all

Define_Com Class(#PRIM_ALPH) Name(#MyBase64String)

#BinaryData.FromBase64String String(#MyBase64String)
atostaine
Posts: 696
Joined: Wed Jan 20, 2016 7:38 am

Re: Continuing saving images Lansa mobile

Post by atostaine »

Thank you. I was using alt-space and searching the docs to try and figure out what was wrong. Another few hours maybe. :D
Art Tostaine
VLNinja70

Re: Continuing saving images Lansa mobile

Post by VLNinja70 »

How are you guys getting your base64 variable up to the server from the client after you take the picture?
I can only talk to the server module from the web page using fields (limited to 65k)

Is there a way you can pass up the Prim object in one go to the server module or do you have to break it up into 65k bits in a list and rebuild the string on the server side to stuff it all into a blob?


AAAAAAnd Ok I totally see what you guys mean by the file picker being easier.... way cool 8-)
atostaine
Posts: 696
Joined: Wed Jan 20, 2016 7:38 am

Re: Continuing saving images Lansa mobile

Post by atostaine »

Sorry missed this post.

It's held in the RP as a PRIM_ALPH, then it to send it up, substring into a list that has one entry per 64K.

The link that I posted earlier in this thread has Casey's example. Here is how I substring'd it out. Do the opposite in the server function.
#Current comes from an image collection because I'm sending multiple images up.

Code: Select all

#W_LENGTH := #current.CurSize
Clr_List Named(#imageList)
#W_START := 23
#W_END := 65535

Dowhile Cond(#W_END <= #W_LENGTH)

#fBase64  := #Current.Substring( #W_START #W_END )
Add_Entry To_List(#imageList)

#W_START := #W_START + 65535
#W_END := #W_END + 65535

If Cond(#W_END > #W_LENGTH)
#W_END := #W_LENGTH
#fBase64 := #current.Substring( #W_START #W_END )
Add_Entry To_List(#imageList)
Leave
Endif

Endwhile
Art
Art Tostaine
atostaine
Posts: 696
Joined: Wed Jan 20, 2016 7:38 am

Re: Continuing saving images Lansa mobile

Post by atostaine »

Does the #binaryData.FromBase64String return a BLOB? I can use it as a JPG?

I can't seem to get it to save anything correctly either as a BLOB or CLOB.

Thanks, Art
tsupartono wrote: Tue Nov 14, 2017 7:55 am You can still use PRIM_ALPH.

I noticed in the example I provided, the FromBase64String invocation is missing the String parameter name.
The String parameter name does need to be specified.
Please see the corrected code below.

Code: Select all

Define_Com Class(#PRIM_ALPH) Name(#MyBase64String)

#BinaryData.FromBase64String String(#MyBase64String)
Art Tostaine
Speedlime
Posts: 44
Joined: Wed Feb 03, 2021 2:52 am

Decode from Base64

Post by Speedlime »

Thanks I was looking for a example of this, I can confirm this works well.
Pulling data from Json Response, and decoding it.

DEFINE_COM Class(#PRIM_ALPH) Name(#MyBase64String)

#Reader.SetSourceHttpResponse Httpresponse(#Request.Response)

#Reader.BeginObjectWithPath Path('paperwork')
* Get manifest block of data which is written as Base64
#MyBase64String := #Reader.ReadStringWithName( 'manifest' ).AsNativeString
* Decode Base64 string
#BinaryData.FromBase64String String(#MyBase64String)
#STD_BLOB := #BinaryData.AsFile

#Reader.EndObject
Post Reply