B4J Question Writing array of ints to image file...

rbghongade

Active Member
Licensed User
Longtime User
Dear friends,
I want to write the processed data (array of ints) as an output image file,but am not quite getting it. Tried to use writetostream method but am not being successful.
 

sorex

Expert
Licensed User
Longtime User
B4X:
Dim out As OutputStream = File.OpenOutput(file.dirapp,"image.png", False)
mycanvas.Snapshot.WriteToStream(out)
out.Close
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear sorex,
That is wonderful. Thanks. But is there no way to avoid drawing on the canvas but directly saving to the output image?
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
My image data is 2D array of ints and the example requires 1D array of bytes, how to do that?
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
I flattened the 2D array to 1D array, converted it using Byteconverter but if I try to use the sub BytesToImage(bytes() As Byte) As Image to get an image and then use the WriteToStream method it flags an error as shown below:
B4X:
main._appstart (java line: 117)
java.lang.IllegalArgumentException: image == null!
    at javax.imageio.IIOImage.<init>(IIOImage.java:109)
    at anywheresoftware.b4j.objects.ImageViewWrapper$ImageWrapper.WriteToStream(ImageViewWrapper.java:182)
    at b4j.example.main._appstart(main.java:117)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:90)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:77)
    at b4j.example.main.start(main.java:38)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
I thinks it's better to show some code aswell.

B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim imat(512,512) As Int
    Dim inew(512,512) As Int
    Dim new(512*512) As Int
    Dim img,imgnew As Image
    Private btnQuit As Button
    Private ImageView1 As ImageView
    Private Pane1 As Pane
    Dim bc As ByteConverter
    Dim mycanvas As Canvas
    Dim k As Int
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    img.Initialize(File.DirApp, "lena_gray.bmp")
    'bc.LittleEndian = True
    For i=0 To 511
        For  j=0 To 511
            Dim p As Int=ToUnsigned(img.GetPixel(i,j))
            imat(i,j)=p
            If p>=127 Then
            inew(i,j)=1
               
            Else
            inew(i,j)=0
               
            End If
            new(k)=inew(i,j)
            k=k+1
           
        Next
    Next

    imgnew=BytesToImage(bc.IntsToBytes(new))
    Log(new.Length)
    Dim out As OutputStream = File.OpenOutput(File.dirapp,"image.bmp", False)
    imgnew.WriteToStream(out)
    out.Close
    ImageView1.SetImage(img)
   
End Sub

Sub ToUnsigned(b As Byte) As Int
    Return Bit.And(0xFF, b)
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Public Sub ImageToBytes(I As Image)
    Dim Out As OutputStream = File.OpenOutput(File.DirApp, "1.bmp",True)
   
    Out.InitializeToBytesArray(0)
    I.WriteToStream(Out)
    Out.Close
    Return
End Sub

Public Sub BytesToImage(bytes() As Byte) As Image
    Dim In As InputStream
    In.InitializeFromBytesArray(bytes, 0, bytes.Length)
    Dim bmp As Image
    bmp.Initialize2(In)
    Return bmp
End Sub

Sub btnQuit_Click
    MainForm.Close
End Sub
 
Upvote 0

rbghongade

Active Member
Licensed User
Longtime User
Dear Erel,
During debug, the flattened array(1D array) shows only 1000 values , but the actual size is 262144, is this ok? I must be doing something wrong as I am still unable to write data to the file.
Also can you wrap the java image processing library (example;https://github.com/imglib/imglib2) or similar which takes care of image I/O and formats? That would be a great thing ! I hope I am not asking too much!
regards,
 
Upvote 0
Top