B4A Library [B4X] Library Base64 encode/decode Image and File library

Updated Library https://www.b4x.com/android/forum/t...ge-and-file-library.115033/page-2#post-998317

From an idea of the Great Peter ( https://www.b4x.com/android/forum/threads/base64-encode-decode-image-library.31031/ ) "...in attached the library for decoding base64 image stings, so I quickly put together this simple but effective base64 encode/decode images library for others to use..."

In version 1.13 also added the BaseAnyFileToString + Base64StringToAnyFile method. To convert any file to Base64.

The library is for B4X (Tested with B4J, B4A, B4i)

The Example Code:

B4X:
'File 653 kb
    File.Copy(File.DirAssets, "653.jpg", File.DirInternal,"653.jpg")
    Sleep(300)

    'Image to Encode64
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"653.jpg")

    'Check if Valid Base64
    If Base64EncodeDecodeImage.ValidBase64(base) Then
        ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
    End If

    Sleep(2000)

    'Convert without Dir / File as request #8
    Dim bitmap1 As B4XBitmap = ImageView1.Bitmap
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bitmap1)
    ImageView2.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)

    'Any File to Encode64
    Dim pdf_base64 As String = Base64EncodeDecodeImage.Base64AnyFileToString(File.DirApp, "example_5_pages.pdf")
 
    'Check if Valid Base64
    If Base64EncodeDecodeImage.ValidBase64(pdf_base64) Then
       Base64EncodeDecodeImage.Base64StringToAnyFile(pdf_base64, File.DirApp, "convert.pdf")
    End If

Example Code B4A --> https://www.dropbox.com/s/mohtg21cnfvaj1p/CONVERTBMPTOCODE64B4.zip?dl=0
Example Code B4i --> https://www.dropbox.com/s/97x5hcexy9xgkm6/CONVERTBMPTOCODE64.zip?dl=0
Example Code B4J -->https://www.dropbox.com/s/888ic5l1m18f4d1/B4J-CONVERTBMPTOCODE64.zip?dl=0

In attachment library b4xlib
Have a nice day
Marco

18.04.2021
Rel. 1.13 --> Add Base64AnyFileToString
 

Attachments

  • Base64EncodeDecodeImage.b4xlib
    1.5 KB · Views: 1,228
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Very useful, Marco, thank you.

Since I wanted a second method to "import" a bitmap in your Base64ImageToString, which reads an image file
(I "needed" to pass it a B4XBitmap), I added it but I also took the opportunity to change something.
If you "like" my changes you can add this version to your first post and I will remove this one ;)

Description of changes:
B4X:
' Base64EcondeDecodeImage - Code module
' Author:    MarcoRome

#Region VERSIONS
' 1.1
'    Added ImageToString method, which is identical to Base64ImageToString,
'        but its name is more "correct". Kept Base64ImageToString for backward compatibility.
'    Added ImageToString2 method, which requires a B4XBitmap parameter.
'    Renamed variable name "convert" to mStringUtils
'        and declared it as Private in Process_Globals.
'    Added B4XBitmapToBytes private method - used by ImageToString2.
'    Added some Regions.
' 1.0
#End Region
 

Attachments

  • Base64EncodeDecodeImage.b4xlib
    1.3 KB · Views: 685
Last edited:

By-Cod3rs

Member
Licensed User
Hello, I want to flip a picture taken from the camera without saving
can you improve it?
thanks.
 

MarcoRome

Expert
Licensed User
Longtime User
I want to convert an unregistered bitmap object or a picture in imageview to base64 format.

Without the picture way.
Look first post

B4X:
'Image to Encode64
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(Directory,File")
 

By-Cod3rs

Member
Licensed User
Dim img As Bitmap=ImageView1.Bitmap

Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(img)
I wanted to explain this.thanks
an image where I don't know the path to the file.
 

MarcoRome

Expert
Licensed User
Longtime User
Dim img As Bitmap=ImageView1.Bitmap

Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(img)
I wanted to explain this.thanks
an image where I don't know the path to the file.

In attachment Rel 1.10
 

MarcoRome

Expert
Licensed User
Longtime User
Return Type Byte array :(
Can't String Return Type?
thanks.

As in example 1# Post
B4X:
 'Convert without Dir / File as request #8
    Dim bitmap1 As B4XBitmap = ImageView1.Bitmap
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bitmap1)
    ImageView2.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
 

By-Cod3rs

Member
Licensed User
As in example 1# Post
B4X:
 'Convert without Dir / File as request #8
    Dim bitmap1 As B4XBitmap = ImageView1.Bitmap
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bitmap1)
    ImageView2.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
I just saw it Thank you. Thank you very much
 

By-Cod3rs

Member
Licensed User
I have a problem. I have a .net web Service that meets this picture.
and it doesn't work compatible with its library.
I want to translate the picture into text on mobile and decode the code on the website. What Do You Recommend?
 

MarcoRome

Expert
Licensed User
Longtime User
Its posible to use this lib. in order to encode in base64 any binary file?

thanks

No. But you can use this ( not tested ):

B4X:
Sub Base64AnyFileToString(Dir As String, Filename As String) As String
    Dim convert As StringUtils
    Return convert.EncodeBase64(Bit.InputStreamToBytes(File.OpenInput(Dir, Filename)))
End Sub

Sub Base64StringToAnyFile(s As String, Dir As String , FileName As String)
    Dim su As StringUtils
    Dim bytes() As Byte = su.DecodeBase64(s)
    Dim out As OutputStream = File.OpenOutput(Dir, FileName, False)
    out.WriteBytes(bytes, 0, bytes.Length)
    out.Close
End Sub
 

By-Cod3rs

Member
Licensed User
It is even simpler as you can use File.ReadBytes / WriteBytes
B4X:
Dim su As StringUtils
Dim base64 As String = su.EncodeBase64(File.ReadBytes(...))

Thanks Erel.
I Solved That Way.
MarcoRome ibrary not supported in .net base64 function.

Snapshot:
Sub Camera1_PictureTaken (Data() As Byte)
'
    Dim su As StringUtils
    Dim base64 As String = su.EncodeBase64(Data)
    Log(base64) 'limit 4000 char..

End Sub


Thus, the picture was displayed on both .net and web.
.Net Base64 Code(:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim Base64Code As String = "......................................"

            Dim imageBytes As Byte() = Convert.FromBase64String(Base64Code)
            Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length)
            Dim Btmp As New Bitmap(Image.FromStream(ms))
            PictureBox1.Image = (OrientateImage(Btmp))


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub

    Public Function OrientateImage(img As Image) As Image
        Const EXIF_ORIENTATION = 274
        Dim orientationTag = img.PropertyItems.FirstOrDefault(Function(x) x.Id = EXIF_ORIENTATION)
        If orientationTag IsNot Nothing Then
            Dim orientation As Short = BitConverter.ToInt16(orientationTag.Value, 0)
            Select Case orientation
                Case 3
                    img.RotateFlip(RotateFlipType.Rotate180FlipNone)
                Case 6
                    img.RotateFlip(RotateFlipType.Rotate90FlipNone)
                Case 8
                    img.RotateFlip(RotateFlipType.Rotate270FlipNone)
                Case Else
            End Select
        End If

        Return img
    End Function
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Thanks Erel.
I Solved That Way.
MarcoRome ibrary not supported in .net base64 function.

Snapshot:
Sub Camera1_PictureTaken (Data() As Byte)
'
    Dim su As StringUtils
    Dim base64 As String = su.EncodeBase64(Data)
    Log(base64) 'limit 4000 char..

End Sub


Thus, the picture was displayed on both .net and web.
.Net Base64 Code(:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim Base64Code As String = "......................................"

            Dim imageBytes As Byte() = Convert.FromBase64String(Base64Code)
            Dim ms As New MemoryStream(imageBytes, 0, imageBytes.Length)
            Dim Btmp As New Bitmap(Image.FromStream(ms))
            PictureBox1.Image = (OrientateImage(Btmp))


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try


    End Sub

    Public Function OrientateImage(img As Image) As Image
        Const EXIF_ORIENTATION = 274
        Dim orientationTag = img.PropertyItems.FirstOrDefault(Function(x) x.Id = EXIF_ORIENTATION)
        If orientationTag IsNot Nothing Then
            Dim orientation As Short = BitConverter.ToInt16(orientationTag.Value, 0)
            Select Case orientation
                Case 3
                    img.RotateFlip(RotateFlipType.Rotate180FlipNone)
                Case 6
                    img.RotateFlip(RotateFlipType.Rotate90FlipNone)
                Case 8
                    img.RotateFlip(RotateFlipType.Rotate270FlipNone)
                Case Else
            End Select
        End If

        Return img
    End Function
Strain, because if you resolved with suggestion by Erel. The library use the same function su.EncodeBase64....
 

MarcoRome

Expert
Licensed User
Longtime User
The library is in new format b4xlib. So if you open the libraries you can see ( and modified ) code without problem.
 
Top