B4A Library Base64 encode/decode image library

Peter Simpson

Expert
Licensed User
I noticed the other day that there were quite a few questions regarding decoding base64 image stings, so I quickly put together this simple but effective base64 encode/decode images library for others to use.

I will be turning more of my simple but useful routines to libraries, but not too many :)

Instructions:
In Globals add the following line
Dim Base64Con As Base64Image
Dim Base64Con As Base64Convert

To get an encoded base64 data stream, and return it as an bitmap ready to load into an image view for example, use the following line.
Base64Con.DecodeToImage(ImgStr As String) As Bitmap

Or

To get an image file, and encode it into an base64 data stream, ready to save into a database or file for example, use the following line.
Base64Con.EncodeFromImage(FolderPath As String, Filename As String) As String

Quite simple to use really.

Anyway, I hope that it work great for you all, well that's if anybody uses it :)
Just download the library and let me know what you think about it.

UPDATED: 09/04/2018: 1.10
Removed logs

This library is extremely simple to use.

SS_Base64Convert

Author: Peter Simpson
Version: 1.1
  • Base64Convert
    • Functions:
      • DecodeToImage (ImageStr As String) As Bitmap
        Use this call to decode the base64 datastream into an image
      • EncodeFromImage (FolderPath As String, Filename As String) As String
        Use this call to encode the image into a base64 datastream
      • Initialize As String
        Initializes the object. You can add parameters to this method if needed.
      • IsInitialized As Boolean
        Tests whether the object has been initialized.

Cheers :cool:
 

Attachments

Last edited:

Big JR

Member
Licensed User
I noticed the other day that there were quite a few questions regarding decoding base64 image stings, so I quickly put together this simple but effective base64 encode/decode images library for others to use.

I will be turning more of my simple but useful routines to libraries, but not too many :)

Instructions:
DecodeToImage(ImgStr As String) As Bitmap
Get an encoded base64 data stream, and return it as an bitmap ready to load into an image view for example.

And

EncodeFromImage(FolderPath As String, Filename As String) As String
Get an image file, and encode it into an base64 data stream.

Quite simple to use really.

Anyway, I hope that it work great for you all, well that's if anybody uses it :)
Just download the library and let me know what you think about it.

Cheers :cool:
Hi Peter,
This looks like just what I needed but I'm doing something silly. I've added your jar and xml files to the library and checked and loaded the Base64Image library within B4A. I've added the image to assets. Then I've got...

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim EditText1 As EditText
Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
Dim b64Str As String
b64Str = EncodeFromImage(File.DirAssets,"CompleteLandscapes.jpg")
End Sub

... but when I try to compile and run it B4A says "Undeclared variable 'encodefromimage' is used before it was assigned any value."
 

Peter Simpson

Expert
Licensed User
Hi Peter,
This looks like just what I needed but I'm doing something silly. I've added your jar and xml files to the library and checked and loaded the Base64Image library within B4A. I've added the image to assets. Then I've got...

Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim EditText1 As EditText
Dim Button1 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Layout1")

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
Dim b64Str As String
b64Str = EncodeFromImage(File.DirAssets,"CompleteLandscapes.jpg")
End Sub

... but when I try to compile and run it B4A says "Undeclared variable 'encodefromimage' is used before it was assigned any value."

Hello @Big JR

In Globals add the following line
Dim Base64Con As Base64Image

Then your line should read as follows.
Dim B64Str As String = Base64Con.EncodeFromImage(File.DirAssets, "CompleteLandscapes.jpg")

Enjoy :)
 

Robert Valentino

Well-Known Member
Licensed User
I noticed the other day that there were quite a few questions regarding decoding base64 image stings, so I quickly put together this simple but effective base64 encode/decode images library for others to use.

I will be turning more of my simple but useful routines to libraries, but not too many :)

Instructions:
In Globals add the following line
Dim Base64Con As Base64Image

To get an encoded base64 data stream, and return it as an bitmap ready to load into an image view for example, use the following line.
Base64Con.DecodeToImage(ImgStr As String) As Bitmap

Or

To get an image file, and encode it into an base64 data stream, ready to save into a database or file for example, use the following line.
Base64Con.EncodeFromImage(FolderPath As String, Filename As String) As String

Quite simple to use really.

Anyway, I hope that it work great for you all, well that's if anybody uses it :)
Just download the library and let me know what you think about it.

Cheers :cool:
I tried your library - VERY cool the only thing I would suggest is a routine to return a BitmapDrawable do we could take what you returned right into a statelist setting

Thanks

BobVal
 

Robert Valentino

Well-Known Member
Licensed User
I wrote a TestCode app that will take bitmaps and make into strings and save to a file as a B4A formatted declare string

If run the APP after compiling with a conditional symbol of MakeStrings it will generate the DownArrows.txt file onto your Android which you can then Bluetooth to your PC.

NOTE: This file is now included in the TestCode program so compiling it with a conditional symbol of TestStrings will actually take the text string from the program and use them to make button bitmaps for you to try.

if we ever get something like a #include for B4A we will be able to just include the file into our source with have to copy paste it.

Hope someone fines this useful

NOTE: The DownArrows.txt file does not look to good when opened in IE but when brought into B4A is nicely formatted (done by the routine
B4ACodeFormatter)


BobVal
 

Attachments

Johnson Samuel

Member
Licensed User
I have encoded the image using the base64image library, and have saved into mysql database. How can I decode the saved image string from the database using php. I could not succeed. Can any one help me in this?
 

Johnson Samuel

Member
Licensed User
Dear Simpson,
Is the encoded format of your library is same that of php encoding? I am trying to point out is that ,is the encoded string is in the format recognised by php decoder.? if the formats are different than it may not be possible to decode.
I have tried enough of php methods but have failed. If you could solve the problem that could be nice. I am using your base64Image library to encode.
 

Kwame Twum

Active Member
Licensed User
Thanks for the Lib @Peter Simpson,
Please, I encoded an image and got the resulting string, however I tried decoding the string and saving back to image and all I got was a piece of the image I saved.
Here's the encoded string:

B4X:
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH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2AFY06POpzi5fDa/m1KSurvRfjqtmldTjGytq+WPN83JS+9b/9u9dTxP8AZm+Ig0DxZa+H79YobXxLDHYRqCEEV3FcFtOllJZTG26IWmxQBsuWchhHI1fonf2bRFo0c7QIlaTayhii9WaQffcAKoLbQ2NxBBFfjdql8+meKdG1Swt47G40u8t72Pyw7zSSrcM7LNOZUhlaNG8lNkY+VwOrGv2T8PatH4t8J+HvEUYRTqml213cEE7vNcJ9qjPzMu3zxLlAcZcggkGjERio80NPhT83eWv/AJLfXq3p7zYo6QcZJN3XK+yV7r70rfLZxRxN5ZttcgFCu9U5DiQ5nV3jIBLLKFGCcfN/CBknkpreRZHCRMwGM9MhgzKVI3Lhht+YDcAf4iDivVrq18ppDtwWVt/8RI2yoUJx12lwq8kE49a5O40pJ5GYsF2pGmGJxwoOV4Ix1yB0ffkltxqaFb3JKT1TSTv0vpo36+XvWVlGTZ/X5/5X+a3aZlWFjK4BjRcDgklSy4JDMC4+YFivAUkHHJJOd7Xbyz8LeCvEGv38nkWmj6Pf3N1KyKyoLexlY5VSu5gSAqhVVyyqCMsx0rO3CLBtjyE2lCRwy/NwQFGDlQeT1A
and this is the output after saving...:
8.jpg

Meanwhile, this is the actual image:
my_ch.jpg

What could be the problem?
 
Last edited:

Kwame Twum

Active Member
Licensed User
Thanks Erel, I thought as much..., so that explains it then.

1. When posting a string (using PostString) containing a query with a parameter that long (over 4000 characters), does it get truncated during the process or the entire string is sent?
2. Again, when the JobDone event fires, with the resulting query response after parsing the JSON (supposing I'm retrieving a string with over 4000 characters from a database), does it get truncated as well?
 
Top