Sending a Mat object over socket from Java to Java

878 views Asked by At

I understand Sockets over Java and sending Int,String,bytes etc over it.

What i just want to know is that is there a way to decode a Mat object to byte array and then send it over java socket and then retrieve back the Mat object from the byte received?

Till Now this is What I have done

For Sending Mat over socket

//Sending Mat over Socket

Mat socketmat;      
long  nbytes = socketmat.total() * socketmat.elemSize();
byte[] bytes = new byte[ (int) nbytes ];
socketmat.get(0, 0,bytes);
mybytearray = bytes;

dos = new DataOutputStream(os);
dos.writeLong(nbytes);
dos.write(mybytearray, 0, mybytearray.length);  

dos.flush(); 

For Receiving Mat over Socket

//Receiving Mat over Socket

long len = clientData.readLong();  
byte[] data = new byte[ (int) len];
if (len > 0) {
    clientData.readFully(data);
}            
byte[] bytes_ = data;
result_mat.get(0, 0, bytes_ );
1

There are 1 answers

0
Spit Comp Me On BEST ANSWER

What I think is to Save Mat using FileStorage class using JNI.

The following code can be used to save Mat as File Storage

FileStorage storage("image.xml", FileStorage::WRITE);
storage << "img" << mat;
storage.release();

Then send the file using Socket and then retrive Mat back from File.

FileStorage fs("image.xml", FileStorage::READ);
Mat img;
fs >> img;