B4J Question [Server] GetImage WriteToStream

enrfortin

Member
Licensed User
Hi, I am using this project as the basis for a server application.

I tested the application and it works very well but it is a desktop application.
and I don't know how I can do this in b4j server.

In desktop app:
Dim out As OutputStream
        out=File.OpenOutput(File.Combine(File.DirApp,"tesseract-ocr"),"image.png",False)
        ImageView1.GetImage.WriteToStream(out)
        out.Close

and tried to do this without success:

bj4[server]:
        Dim out As OutputStream
        out=File.OpenOutput(File.Combine(File.DirApp,"tesseract-ocr"),"image.png",False)
      
    Dim nombreArchivoTemporal As String = Main.NombreArchivo
    Dim imageData() As Byte = File.ReadBytes(File.DirApp & "/up", nombreArchivoTemporal)
    Dim lengnt As String = nombreArchivoTemporal.Length
    out=File.OpenOutput(File.Combine(File.DirApp,"tesseract-ocr"),"image.png",False)
    out.WriteBytes(imageData, 0, lengnt)

    out.Close

the image is uploaded to the directory
File.DirApp & "/up"
and the name of the image I save here when uploading the image
Dim nombreArchivoTemporal As String = Main.NombreArchivo

I would appreciate any help or examples that can be provided.
 

jahswant

Well-Known Member
Licensed User
Longtime User
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
    Try
Dim img() As Byte = FileToBytes(File.DirAssets,"no_image_available.png")
Dim Inp As InputStream
Inp.InitializeFromBytesArray(img, 0, img.Length)
File.Copy2(Inp, resp.OutputStream)
StopMessageLoop
Catch
Log(LastException)
End Try
End Sub

Sub FileToBytes (Dir As String, FileName As String) As Byte()
    Return Bit.InputStreamToBytes(File.OpenInput(Dir, FileName))
End Sub
 
Upvote 0

enrfortin

Member
Licensed User
Thanks I was able to adapt the code:

Process Image:
Sub ProcessImage
    Try
        ' Patch of the upload image.
        Dim uploadedImagePath As String = File.Combine(File.DirApp & "/up", "")

        ' Open OutputStream for output image
        Dim Inp As InputStream = File.OpenInput(uploadedImagePath,Main.NombreArchivo)
        
        ' Open OutputStream for output image
        Dim out As OutputStream = File.OpenOutput(File.DirApp & "/tesseract-ocr", "image.png", False)
        
        ' Copy image from InputStream to OutputStream
        File.Copy2(Inp, out)

        ' Close streams
        out.Close

        ' Here you can call Tesseract or do any additional processing with the image.
        scan
    Catch
        Log(LastException)
    End Try
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…