Search code examples
javac++pythonsocketsjpeg

how to stream jpeg frame from java client to python server


i am developing an android application wherein i have to send a frame in jpeg format allocated to a BufferedArrayOutputStream (baos variable in code). I convert this baos into a byte array to write into the socket. On the server side i would like to reconstruct the image in jpeg format. If i write the data received in a variable to a '.jpg' file on the server, on opening the file, it gives an error like "file starting with ffx0 not jpeg format". I think this is because the string variable in python writes the data in the file as a hex string. The client code is as follows :-

Bitmap memoryImage = Bitmap.createBitmap(rgb, previewSize.width,previewSize.height,Bitmap.Config.ARGB_8888);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
if(memoryImage.compress(CompressFormat.JPEG,100, baos)){
    try {
        if(count==0){
            byte [] Finalbaos = baos.toByteArray();
            int tempLen = Finalbaos.length;
            Log.v("Client","ImageBytes :"+ tempLen);
            String dataMeta = Integer.toString(tempLen);
            Log.v("Client","Integer Size :"+ dataMeta.length());
            PrintWriter tempOut = new PrintWriter(socket.getOutputStream());
            if(tempOut!=null){
                tempOut.write(dataMeta);
                Log.v("Client","data size sent");
                tempOut.flush();
            }

            DataInputStream in = new DataInputStream(socket.getInputStream());
            if(in!=null){
                Log.v("Client","read buffer created");
                String xyz = in.readLine();
                String temp = "recvd";
                Log.v("Client",xyz);
                if(xyz.equals(temp)){
                    OutputStream out = socket.getOutputStream();
                    out.write(Finalbaos,0,tempLen);
                    out.flush();
                    Log.d("Client", "Client sent message");
                }
            }

server code:

import socket,thread
import string
import array
host=""
port=54321
s=socket.socket()
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((host,port))
s.listen(5)
conn,address=s.accept()
data=""
mylen=0
dataRecv=0
file1 = open("myfile.jpg","w")
totalLength=""
length=conn.recv(1024)
conn.send("recvd")
mylen=int(length)

while dataRecv<mylen:
    newData=""
    newData=conn.recv(1)
    if not newData:
        break
    data+=newData
    dataRecv+=len(newData)

result= array.array('B',data.decode("hex"))
file1.write(result)
file1.close()
print len(data)
conn.close()
s.close()

can anyone let me know how to reconstruct the frame on server either in python or C++


Solution

    • mylen=len(length) doesn't give you the length you're trying to send. it gives you how many bytes were read in the previsous recv. So you get the wrong lenght there.
    • on your client side, you use String xyz = in.readLine(); which will block until a newline character is encountered. but you never send a '\n' on the server side, instead you go waiting for a response from the client. so you have a deadlock there.
    • you use data.decode("hex") on your recieved data. unless you do the equivalend of data.encode("hex") in java on the other side, that won't work. it should give you an error if the string is not a valid hex-representation of a binary string.
    • result is an array.array, which you write to file. file1.write expects a string as argument, it gives you an error if you pass your result object.

    so i can't even see why your code works at all, and why there's anything at all in your file.