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,243
Last edited:

TILogistic

Expert
Licensed User
Longtime User
Very good:

I had separate classes for B4A, B4J and B4I ..

Can I take your project and add other routines?
 

LucaMs

Expert
Licensed User
Longtime User
There is an error in the manifest file (how come no one noticed? Is maybe my file corrupted? ?. I don't think so; you (all) should use more often B4J ;))

B4X:
Version=1.10
B4J.DependsOn=jStringUtils, jXUI, jiBitmapCreator, jRandomAccessFile

jiBitmapCreator - it should be jBitmapCreator, of course.
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
There is an error in the manifest file (how come no one noticed? Is maybe my file corrupted? ?. I don't think so; you (all) should use more often B4J ;))

B4X:
Version=1.10
B4J.DependsOn=jStringUtils, jXUI, jiBitmapCreator, jRandomAccessFile

jiBitmapCreator - it should be jBitmapCreator, of course.
BugFix is released #1
 

Johan Hormaza

Well-Known Member
Licensed User
Longtime User
Error

1618674190466.png
 

MarcoRome

Expert
Licensed User
Longtime User
In attachment #1 you have Rel.1.13
Add new methods BaseAnyFileToString + Base64StringToAnyFile. To convert any file to Base64 & any file in Base64 to file
Retried with all three environments (B4A + B4i + B4J)
 

hayk

Member
Hi everyone :) I am facing following problem:
method 1 result: works correct and returns correct image.
2:
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"653.jpg")

123456.jpg


method 2 result: NOT correct and returns incorrect image. I have used B4XBitmap and Bitmap. Result is the same.
1:
        Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bmp1)

1234.jpg
.

Where I made mistake? I am using library v1.13
 

MarcoRome

Expert
Licensed User
Longtime User
Hi everyone :) I am facing following problem:
method 1 result: works correct and returns correct image.
2:
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"653.jpg")

View attachment 156294

method 2 result: NOT correct and returns incorrect image. I have used B4XBitmap and Bitmap. Result is the same.
1:
        Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bmp1)

View attachment 156293.

Where I made mistake? I am using library v1.13
Pls, attach an example
 

hayk

Member
Pls, attach an example
Thanks for response! :)
Here is an example

main:
        Dim bmp1 As Bitmap
        bmp1.Initialize(File.DirInternal, "123456.jpg")
        Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bmp1)
        Sleep(5)
        If Base64EncodeDecodeImage.ValidBase64(base) Then
            ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
        End If
result is the same by using B4XBitmap also
 
Last edited:

MarcoRome

Expert
Licensed User
Longtime User
Thanks for response! :)
Here is an example

main:
        Dim bmp1 As Bitmap
        bmp1.Initialize(File.DirInternal, "123456.jpg")
        Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2(bmp1)
        Sleep(5)
        If Base64EncodeDecodeImage.ValidBase64(base) Then
            ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
        End If
result is the same by using B4XBitmap also

Yes you are right.
As soon as I have time I will make the change.
 

MarcoRome

Expert
Licensed User
Longtime User
In version 1.14 you have a bugfix and added:

Base64ImageToString2Type
Do not use Base64ImageToString2 but use Base64ImageToString2Type as follows:
B4X:
'If file is JPG or JPEG
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
'If file in PNG
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "PNG")

ValidBase64
Modified the ValidBase64 function which was giving an error on very large files 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")
    File.Copy(File.DirAssets, "375.png", File.DirInternal,"375.png")
    File.Copy(File.DirAssets, "26.jpeg", File.DirInternal,"26.jpeg")
    Sleep(300)
   
'    'Example 1
'    'Image to Encode64
'    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"653.jpg")
'  
'    Log(base)
'    'Check if Valid Base64
'    If Base64EncodeDecodeImage.ValidBase64(base) Then
'        ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
'    End If


    'Example 2
    'B4XBitmap
    Dim bmp1 As B4XBitmap
    bmp1 = LoadBitmap(File.DirInternal, "26.jpeg")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    'bmp1 = LoadBitmap(File.DirInternal, "653.jpg")
    'Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    'bmp1 = LoadBitmap(File.DirInternal, "375.png")
    'Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "PNG")
   
    'Bitmap
'    Dim bmp1 As Bitmap
'    bmp1.Initialize(File.DirInternal, "653.jpg")
'    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    Log(base.Length)
    Dim check_base64 As Boolean = Base64EncodeDecodeImage.ValidBase64(base)

    'Check if Valid Base64
    If check_base64 = True Then
        ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
    Else
        Log("Error")
    End If
   
   

   
'    'FILE PDF
'  
'    'File 653 kb
'    'File.Copy(File.DirAssets, "example_5_pages.pdf", File.DirInternal,"example_5_pages.pdf")
'    File.Copy(File.DirAssets, "example_5_pages.pdf", Starter.Provider.SharedFolder, "example_5_pages.pdf")
'    Sleep(300)
'  
'  
'    Dim converti_base64 As String = Base64EncodeDecodeImage.Base64AnyFileToString(Starter.Provider.SharedFolder, "example_5_pages.pdf")
'  
'    Log(converti_base64.Length)
'    Dim check_base64 As Boolean = Base64EncodeDecodeImage.ValidBase64(converti_base64)
'
'    'Check if Valid Base64
'    If check_base64 = True Then
'        Base64EncodeDecodeImage.Base64StringToAnyFile(converti_base64, Starter.Provider.SharedFolder, "convert.pdf")
'    End If
'  
'    Sleep(200)
'  
'    Dim Intent1 As Intent
'
'    Intent1.Initialize(Intent1.ACTION_VIEW, "")
'    'Vedi Manifest per ShareFolder
'    Starter.Provider.SetFileUriAsIntentData(Intent1, "convert.pdf")
'    Intent1.SetComponent("android/com.android.internal.app.ResolverActivity")
'    Intent1.SetType("application/pdf")
'
'    StartActivity(Intent1)

In attachment library b4xlib 1.14
 

Attachments

  • Base64EncodeDecodeImage.b4xlib
    1.9 KB · Views: 106

b4x-de

Active Member
Licensed User
Longtime User
Thank you for this solution! I have one question: Is the second argument of Base64EncodeDecodeImage() the type of the target or the the of the source image in B4Xbitmap?
 

hayk

Member
In version 1.14 you have a bugfix and added:

Base64ImageToString2Type
Do not use Base64ImageToString2 but use Base64ImageToString2Type as follows:
B4X:
'If file is JPG or JPEG
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
'If file in PNG
Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "PNG")

ValidBase64
Modified the ValidBase64 function which was giving an error on very large files 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")
    File.Copy(File.DirAssets, "375.png", File.DirInternal,"375.png")
    File.Copy(File.DirAssets, "26.jpeg", File.DirInternal,"26.jpeg")
    Sleep(300)
  
'    'Example 1
'    'Image to Encode64
'    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString(File.DirInternal,"653.jpg")
' 
'    Log(base)
'    'Check if Valid Base64
'    If Base64EncodeDecodeImage.ValidBase64(base) Then
'        ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
'    End If


    'Example 2
    'B4XBitmap
    Dim bmp1 As B4XBitmap
    bmp1 = LoadBitmap(File.DirInternal, "26.jpeg")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
  
    'bmp1 = LoadBitmap(File.DirInternal, "653.jpg")
    'Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
  
    'bmp1 = LoadBitmap(File.DirInternal, "375.png")
    'Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "PNG")
  
    'Bitmap
'    Dim bmp1 As Bitmap
'    bmp1.Initialize(File.DirInternal, "653.jpg")
'    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
  
    Log(base.Length)
    Dim check_base64 As Boolean = Base64EncodeDecodeImage.ValidBase64(base)

    'Check if Valid Base64
    If check_base64 = True Then
        ImageView1.Bitmap = Base64EncodeDecodeImage.Base64StringToImage(base)
    Else
        Log("Error")
    End If
  
  

  
'    'FILE PDF
' 
'    'File 653 kb
'    'File.Copy(File.DirAssets, "example_5_pages.pdf", File.DirInternal,"example_5_pages.pdf")
'    File.Copy(File.DirAssets, "example_5_pages.pdf", Starter.Provider.SharedFolder, "example_5_pages.pdf")
'    Sleep(300)
' 
' 
'    Dim converti_base64 As String = Base64EncodeDecodeImage.Base64AnyFileToString(Starter.Provider.SharedFolder, "example_5_pages.pdf")
' 
'    Log(converti_base64.Length)
'    Dim check_base64 As Boolean = Base64EncodeDecodeImage.ValidBase64(converti_base64)
'
'    'Check if Valid Base64
'    If check_base64 = True Then
'        Base64EncodeDecodeImage.Base64StringToAnyFile(converti_base64, Starter.Provider.SharedFolder, "convert.pdf")
'    End If
' 
'    Sleep(200)
' 
'    Dim Intent1 As Intent
'
'    Intent1.Initialize(Intent1.ACTION_VIEW, "")
'    'Vedi Manifest per ShareFolder
'    Starter.Provider.SetFileUriAsIntentData(Intent1, "convert.pdf")
'    Intent1.SetComponent("android/com.android.internal.app.ResolverActivity")
'    Intent1.SetType("application/pdf")
'
'    StartActivity(Intent1)

In attachment library b4xlib 1.14

Thank you very much! ?
 

MarcoRome

Expert
Licensed User
Longtime User
Thank you for this solution! I have one question: Is the second argument of Base64EncodeDecodeImage() the type of the target or the the of the source image in B4Xbitmap?
Source.
if it's a png it will remain a png if it's a jpg it will remain a jpg

B4X:
    'File source JPG / JPEG
    Dim bmp1 As B4XBitmap
    bmp1 = LoadBitmap(File.DirInternal, "26.jpeg")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    Dim bmp1 As B4XBitmap
    bmp1 = LoadBitmap(File.DirInternal, "267.jpg")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "JPG")
   
    'File source PNG
    Dim bmp1 As B4XBitmap
    bmp1 = LoadBitmap(File.DirInternal, "2678.png")
    Dim base As String = Base64EncodeDecodeImage.Base64ImageToString2Type(bmp1, "PNG")
 
Last edited:
Top