B4J Question Jserver httpjob download image

epneeh

Member
Hello
How to resize image/jpeg, encode to base64, and save it to database
B4X:
Dim j as httpjob
J.initialize("",me)
J.download("someurl.com")
Wait for (j) jobdone (j as httpjob)
If j.success then
'Process image here


End if
J.release
In b4a there is j.getbitmapresize, how to do in b4j?
 
Solution
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    DownloadImage("https://picsum.photos/200/300")
    StartMessageLoop
End Sub

Private Sub DownloadImage (Url As String)
    Try
        Log("Start download...")
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(Url)
        Wait For (j) JobDone (j As HttpJob)
        If j.Success Then
            Dim out As OutputStream = File.OpenOutput(File.DirApp, "Test.jpeg", False)
            Dim tmp As InputStream = j.GetInputStream
            File.Copy2(tmp, out)
            out.Close...

aeric

Expert
Licensed User
Longtime User
B4X:
Private Sub DownloadImage (Url As String)
    Try
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(Url)
        Wait For (j) JobDone (j As HttpJob)
        If j.Success Then
            Dim bmp As B4XBitmap = j.GetBitmap
            bmp = bmp.Resize(150, 150, False)
            
            Dim SU As StringUtils
            Dim encoded As String
            encoded = SU.EncodeBase64(ImageToBytes(bmp))
            Log(encoded)
            
            ' Test read from base64           
            Dim data() As Byte = SU.DecodeBase64(encoded)
            B4XImageView1.Bitmap = BytesToImage(data)
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
End Sub
 

Attachments

  • DownloadAndResizeImage.zip
    9.5 KB · Views: 39
Upvote 0

epneeh

Member
B4X:
Private Sub DownloadImage (Url As String)
    Try
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(Url)
        Wait For (j) JobDone (j As HttpJob)
        If j.Success Then
            Dim bmp As B4XBitmap = j.GetBitmap
            bmp = bmp.Resize(150, 150, False)
           
            Dim SU As StringUtils
            Dim encoded As String
            encoded = SU.EncodeBase64(ImageToBytes(bmp))
            Log(encoded)
           
            ' Test read from base64          
            Dim data() As Byte = SU.DecodeBase64(encoded)
            B4XImageView1.Bitmap = BytesToImage(data)
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
End Sub
Hi, thanks for your reply, please try using NON-UI or SERVER project, there is method which is j.getInputStream but no j.getBitmap, please check my test project.
 

Attachments

  • test.zip
    775 bytes · Views: 44
Upvote 0

aeric

Expert
Licensed User
Longtime User
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
    
End Sub

Sub AppStart (Args() As String)
    DownloadImage("https://picsum.photos/200/300")
    StartMessageLoop
End Sub

Private Sub DownloadImage (Url As String)
    Try
        Log("Start download...")
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(Url)
        Wait For (j) JobDone (j As HttpJob)
        If j.Success Then
            Dim out As OutputStream = File.OpenOutput(File.DirApp, "Test.jpeg", False)
            Dim tmp As InputStream = j.GetInputStream
            File.Copy2(tmp, out)
            out.Close           
            Log("File saved!")
            
            'Dim scaler As ImageScaler
            'scaler.Initialize
            'scaler.FileType = "JPEG"
            'If scaler.ResizeImage(File.DirApp, File.DirApp, "Test.jpeg", 200, 200, "FIT_EXACT") Then
            '    Log("File resized!")
            'Else
            '    Log(scaler.Error)
            'End If
            '
            'Dim SU As StringUtils
            'Dim buffer() As Byte = File.ReadBytes(File.DirApp, scaler.ResizedFileName)
            'Dim encoded As String = SU.EncodeBase64(buffer)
            'Log(encoded)
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
End Sub
 
Upvote 0
Solution

epneeh

Member
B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region

Sub Process_Globals
   
End Sub

Sub AppStart (Args() As String)
    DownloadImage("https://picsum.photos/200/300")
    StartMessageLoop
End Sub

Private Sub DownloadImage (Url As String)
    Try
        Log("Start download...")
        Dim j As HttpJob
        j.Initialize("", Me)
        j.Download(Url)
        Wait For (j) JobDone (j As HttpJob)
        If j.Success Then
            Dim out As OutputStream = File.OpenOutput(File.DirApp, "Test.jpeg", False)
            Dim tmp As InputStream = j.GetInputStream
            File.Copy2(tmp, out)
            out.Close          
            Log("File saved!")
           
            'Dim scaler As ImageScaler
            'scaler.Initialize
            'scaler.FileType = "JPEG"
            'If scaler.ResizeImage(File.DirApp, File.DirApp, "Test.jpeg", 200, 200, "FIT_EXACT") Then
            '    Log("File resized!")
            'Else
            '    Log(scaler.Error)
            'End If
            '
            'Dim SU As StringUtils
            'Dim buffer() As Byte = File.ReadBytes(File.DirApp, scaler.ResizedFileName)
            'Dim encoded As String = SU.EncodeBase64(buffer)
            'Log(encoded)
        Else
            Log(j.ErrorMessage)
        End If
    Catch
        Log(LastException.Message)
    End Try
    j.Release
End Sub
Thanks aeric for your detailed instruction this indeed works. Using your imagescaler wrapper it has to save it first to disk (file) then the resized image is also at disk (file), i found better solution using Thumbnailator from this thread Post in thread 'non-ui - resize image in byte array without javafx' https://www.b4x.com/android/forum/t...-byte-array-without-javafx.120421/post-753233

Process take place in memory no need to save it to disk first, again thanks. I'll mark this thread solved.
 
Upvote 0
Top