简体   繁体   English

通过套接字将OpenCV Mat对象从Android Java发送到Java

[英]Send a OpenCV Mat object over socket from Android Java to Java

I have threaded Java Open CV Server on Windows Machine. 我在Windows机器上建立了Java Open CV Server的线程。 Multiple Android Clients Connect to Server using socket programming and want to send a Mat object of the current camera frame over socket to the server and receive back a Mat object back from the Server after being processed. 多个Android客户端使用套接字编程连接到服务器,并希望通过套接字将当前摄像机帧的Mat对象发送到服务器,并在处理后从Server接收回Mat对象。

How can i send a Mat object over a Socket from Android Java to Desktop Java? 如何通过套接字将Mat对象从Android Java发送到Desktop Java?

I have seen a lot of Mat from c++ to Java but am not able to find Java to Java!! 我已经看到很多从C ++到Java的Mat,但是找不到Java到Java!

Convert the Mat to BufferedImage object, then send it as bytes over the socket. 将Mat转换为BufferedImage对象,然后以字节为单位通过套接字发送。 OpenCV already has a Mattobmp function, then you can convert the bitmap to a byte array. OpenCV已经具有Mattobmp功能,然后可以将位图转换为字节数组。 Send this byte array through the socket via BufferedOutputStream. 通过BufferedOutputStream通过套接字发送此字节数组。 On the receiver side it would be easy to recover the Mat object from the bytes you get (first get a BufferedImage from the raw data in the byte array), then convert it back to Mat. 在接收方,从获得的字节中恢复Mat对象很容易(首先从字节数组中的原始数据中获得BufferedImage),然后将其转换回Mat。

That's what worked out fine for me. 那对我来说很好。

Some code snippet of mine: 我的一些代码片段:

Sender: 发件人:

Bitmap bmp = ...

Utils.MattoBmp(mat, bmp)

byte[] bytes = new ...;
bmp.compress(...,..., bytes)

//then send the bytes by DataOutputStream.write

Receiver: 接收方:

//Read the DataInputStream data to an OutputStream

byte[] bytes = outputstream.toByteArray();

You need to do some trick in order to recover the bytes to an Image object 您需要做一些技巧才能将字节恢复到Image对象

Then create another Mat object, fill its contents with the image data. 然后创建另一个Mat对象,用图像数据填充其内容。 This is a slower method I found, I would be pleased if someone could suggest a faster one. 我发现这是一种较慢的方法,如果有人建议使用较快的方法,我会感到很高兴。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM