2015-06-15 139 views
1

我理解Java上的套接字,並通過它發送Int,String,Byte等。從Java到Java通過套接字發送Mat對象

什麼,我只是想知道是有辦法解碼墊對象字節數組,然後將其發送對Java插座,然後檢索回從接收的字節墊目標?

Till Now this is What I have done

發送墊上插座

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

@Newd ..是的,我現在提到它 –

+0

你試過ObjectOutputStream嗎?這對你來說是一個很好的起點,因爲它會自動處理序列化/反序列化。 – Danstahr

+0

Nah ...我沒有任何關於序列化/反序列化的想法 –

回答

0

我覺得是用墊子使用JNI FileStorage類保存。

下面的代碼可以用來墊保存爲文件存儲

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

然後採用Socket發送文件,然後從文件retrive墊回。

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

至於其他指出,您可以使用Serialization解決您的問題。你應該讓你的Mat類實現Serializable接口。

下,而不是手動將對象轉換爲字節,你可以直接寫你的對象,通過在ObjectOutputStream

ObjectOutputStream oos = new ObjectOutputStream(outputStream); 
oos.writeObject(mat); 

包裹你流在receival你可以以同樣的方式提取對象。

ObjectInputStream ois = new ObjectInputStream(inputStream); 
Mat mat = (Mat)(ois.readObject()); 

而不是使用byte的溝通中,您可以通過更靈活的基於XML的通信替換上面。您可以讓java爲您使用JAXB(Java Architecture for Xml Binding)爲您創建XML,這非常容易,而且與序列化非常相似。

// for sender 
JAXBContext jc = JAXBContext.newInstance(Mat.class); 
Marshaller marshaller = jc.createMarshaller(); 
marshaller.marshal(mat, outputStream); 

// for receiver 
JAXBContext jc = JAXBContext.newInstance(Mat.class); 
Unmarshaller unmarshaller = jc.createUnmarshaller(); 
Mat mat = unmarshaller.unmarshal(inputStream); 

在你的情況你的對象有本地引用。但是對於這個問題有一個簡單的解決方案。只需通過創建一個包裝類

import java.io.Serializable; 

public class MatWrapper implements Serializable { 
    int rows; 
    int cols; 
    int type; 
    byte[] data; 

    public MatWrapper() { 
    } 

    public MatWrapper(Mat mat) 
    { 
     if (mat.isContinuous()) { 

      int elemSize = (int) mat.elemSize(); 
      rows = mat.rows(); 
      cols = mat.cols(); 
      type = mat.type(); 

      data = new byte[cols * rows * elemSize]; 
      mat.get(0, 0, data); 
     } 
    } 

    public Mat toMat() 
    { 
     Mat mat = new Mat(rows, cols, type); 
     mat.put(0, 0, data); 
     return mat; 
    } 
} 

用法打破本土界限:

// convert Mat object to a wrapper object 
MatWrapper wrapper = new MatWrapper(mat); 

// this wrapper object is serializable 

// next on receiver-side 
MatWrapper wrapper = (MatWrapper)(ois.readObject); 
Mat mat = wrapper.toMat(); 
+0

評論不適用於擴展討論;此對話已[移至聊天](http ://chat.stackoverflow.com/rooms/80697/discussion-on-answer-by-bvdb-sending-a-mat-object-over-socket-from-java-to-java)。 – meagar

+0

我收到以下mat.put(0,0,data)錯誤;'Caught Error:java.lang.UnsupportedOperationException:提供的數據元素編號(0)應該是Mat通道數的倍數(4) –

+0

抱歉,修正了愚蠢的錯誤(在構造函數中刪除'byte []')。如果這能解決它,你能告訴我嗎? – bvdb