通过套接字从 Java 发送一个 Mat 对象到 Java

Sending a Mat object over socket from Java to Java

我了解 Java 上的套接字并通过它发送 Int、String、bytes 等。

我只想知道有没有办法将 Mat 对象解码为字节数组,然后通过 java 套接字发送它,然后从接收到的字节中取回 Mat 对象?

Till Now this is What I have done

用于通过套接字发送 Mat

//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(); 

用于通过插座接收垫

//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_ );

我认为是使用 FileStorage class 使用 JNI 来保存 Mat。

下面的代码可以用来将Mat保存为File Storage

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

然后使用 Socket 发送文件,然后从 File 中取回 Mat。

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