Ask Your Question

How could I transfer Mat data from Windows Application to Android application?

asked 2013-04-10 12:47:32 -0500

adrian gravatar image

1 down vote favorite 1

I want to transfer a Mat image through tcp ip sockets from an Android device to a PC running Windows. Android application is made in Java and Windows application is made in C++; both are using OpenCV.

More exactly, I would like to transfer the data from Mat image, but I couldn't find the equivalent of 'data' class member from Windows OpenCV for Android OpenCV.

Any ideas?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-04-10 13:26:46 -0500

berak gravatar image

i'd suggest, you imencode your image to png/jpg, something compressed, and send that buffer via tcp to your pc-client.

there, you can easily retrieve it via VideoCapture::open(url) (url should end in .jpg or .png, so they can retrieve the filetype from it) maybe it even needs a proper http header in front, haven't tested that

edit flag offensive delete link more


it's much simpler to use imdecode on the client side

Andrey Pavlenko gravatar imageAndrey Pavlenko ( 2013-04-11 01:40:00 -0500 )edit

I have implemented with 'imencode' and 'imdecode' functions, but the result is an image which I can't open it...

This is my code for the client application (Android - OpenCV): MatOfByte buf = new MatOfByte(); Highgui.imencode(".jpg", matCameraBitmap, buf);

byte[] bufferByte; bufferByte = buf.toArray(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); out.write(bufferByte.toString()); out.flush();

And this is my code for the server application (Windows - OpenCV): if((bytecount = recv(new_sock, buffer, dim_imagine, 0)) == SOCKET_ERROR) { fprintf(stderr, "Error receiving data %d\n", WSAGetLastError()); return 0; }

std::vector<char> data(buffer, buffer + bytecount); Mat matrixJprg = imdecode(Mat(data), 1); cv::imwrite("aaa.jpg", matrixJprg);

adrian gravatar imageadrian ( 2013-04-12 11:24:09 -0500 )edit
  1. your packets will get fragmentated, so you can't read it all in one go, but have to read like 8k blocks until you reach the total number of bytes

  2. i got no idea about andoid api, but out.write(bufferByte.toString()); looks suspect. strings are null terminated, this might truncate it on the sender size

  3. Wireshark is my weapon of choice to debug it

berak gravatar imageberak ( 2013-04-12 13:19:34 -0500 )edit

Thanks for your help, problem solved!

adrian gravatar imageadrian ( 2013-04-17 00:42:21 -0500 )edit

Question Tools


Asked: 2013-04-10 12:47:32 -0500

Seen: 1,477 times

Last updated: Apr 10 '13