zmq进行图片的传输

原始数据

cv::Mat mat

编码

vector data_encode;
cv::imencode(".jpg",mat,data_encode);

传输

zmq::context_t context(1);
zmq::socket_t skt(context,ZMQ_PUB);
skt.bind("tcp://*:20000);
zmq::message_t msg(data_encode.size());
memcpy(msg.data(),data_encode.data(),data_encode.size());
skt.send(msg);

转发(根据实际情况中转了一下)

zmq::context_t context(1);
zmq::socket_t socket_get(context,ZMQ_SUB);
socket_get.setsockopt(ZMQ_SUBSCRIBE,"",0);
socket_get.connect("tcp://127.0.0.1:20000");

zmq::socket_t socket_pos(context,ZMQ_PUB);
socket_pos.setsockeopt(ZMQ_CONFLATE,1);//缓冲消息条数为1,需要实时传输图片时设置。将消息队列改装了消息栈。。哈哈
socket_pos.bind("tcp://*:20001");
while(true){
    zmq::message_t request;
    socket_get.recv(&request);
    if(request.size()==0)
        break;
    socket_pos.send(request);
}

接收

@Override
public void run(){
    ZMQ.Context context = ZMQ.context(1);
    ZMQ.Socket socket = context.socket(ZMQ.SUB);
    socket.subscribe("");
    socket.setConflate(true);
    socket.connect("tcp://192.168.xx.xx:200001");
    while(!Thread.currenThread().isInterrupted()){
        byte[] msg = socket.recv(0);
        UIThreadHandler.sendMessage(....);
    }
    socket.close();
    context.term();
}

显示

    private void clientMessageReceived(byte[] msgBody){
        ByteArrayInputStream inputStream = new ByteArrayInputStream(msgBody);
        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
        imageView.setImageBitmap(bitmap);

}

你可能感兴趣的:(工作日志)