Sending a Picture from Camera over TCP/IP

Kbogle24

Member
Licensed User
Longtime User
I am looking at the following code trying to figure away to us it and send the image over to another app across tcp.

B4X:
Sub Camera1_PictureTaken (Data() As Byte)
    Dim out As OutputStream
    out = File.OpenOutput(File.DirRootExternal, "1.jpg", False)
   out.WriteBytes(Data, 0, Data.Length)
    out.Close
    ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "1.jpg"), True)
End Sub

I have a open socket and I have no problem sending simple text back and forth between apps.

B4X:
Sub Server_NewConnection (Successful As Boolean, NewSocket As Socket)
    
   If Successful Then
      Socket1 = NewSocket
      tw.Initialize(Socket1.OutputStream) 
      tw.Writeline ("OK,Connected!,")
      tw.Flush 
      AStreams.Initialize (Socket1.InputStream, Socket1.OutputStream, "AStreams")
    Else
        ToastMessageShow(LastException.Message, True)
    End If
   
    Server.Listen
End Sub

would i just use

B4X:
tw.write (data)
tw.flush

 

Kbogle24

Member
Licensed User
Longtime User
Ok I have a app sending a picture over to another app on another tablet. Seems my send Code is working fine but when i get the image on the other device it just saves a black image.

My Code for sending the image using Astreams.
B4X:
Sub Camera1_PictureTaken (Data() As Byte)
        AStreams.Write2 (Data,0,Data.Length)
End Sub

The code for receiving the image and saving it to a file.

B4X:
Sub AStreams_NewData (Buffer() As Byte)
 Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, "1.jpg", False)
out.WriteBytes(Buffer, 0, Buffer.Length)
out.Close

End Sub

Any Ideas?:sign0104:
 
Upvote 0

Kbogle24

Member
Licensed User
Longtime User
Ok fixed my own problem. I should have used

B4X:
AStreams.InitializePrefix(Socket1.InputStream, False, Socket1.OutputStream, "AStreams")

I was using
B4X:
AStreams.Initialize (Socket1.InputStream, Socket1.OutputStream, "AStreams")

Sending and receiving the image works great now. Thanks for you help!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…