简体   繁体   中英

Send a OpenCV Mat object over socket from Android Java to Java

I have threaded Java Open CV Server on Windows Machine. 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.

How can i send a Mat object over a Socket from Android Java to Desktop Java?

I have seen a lot of Mat from c++ to Java but am not able to find Java to Java!!

Convert the Mat to BufferedImage object, then send it as bytes over the socket. OpenCV already has a Mattobmp function, then you can convert the bitmap to a byte array. Send this byte array through the socket via 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.

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

Then create another Mat object, fill its contents with the image data. This is a slower method I found, I would be pleased if someone could suggest a faster one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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