Sending an Image over a serial port

I have developed an Android app that connects with a laptop running Matlab over Bluetooth SPP. I am able to send strings back and forth easily and I am now interested in sending an image from Matlab to display on tablet (48x64 grayscale would be sufficient). I am unsure how to package an image and send it over a Matlab serial port. I am guessing you cannot just use fprintf or fwrite. Do I need to import java.imageio or some other external java classes?

1 Comment

Hello, I'm very interested in your MatLab code, is it possible that you send it to me?

Sign in to comment.

Answers (1)

What you should do is first fwrite() over a bit of header information -- total number of bytes in the image, number of bytes per pixel, number of rows, number of columns, number of pixel planes (i.e, 1 for grayscale, 3 for RGB.) Perhaps even file name if you want.
After that, fwrite() the image matrix.
When you receive, fread() to extract the total number of bytes in the image, then extract the other header items, and then fread() for a length equivalent to the total number of bytes you read before. Then use the number of bytes per pixel that you extracted to determine whether to interpret the entries as uint8 or uint16 (or whatever). Having determined the kind of data pointer, do a logical reshape according to the rows, columns, and number of pixel planes. Once you have that all done, store the array or display it (as appropriate.)
Do be careful about the fact that in MATLAB adjacent entries in memory are from entries going down columns. IM(J,K) is immediately adjacent to IM(J+1,K). Especially if your Android app is in C or C++, the language you are using for it might expect arrays to have adjacent entries going across rows, with IM(J,K) being immediately adjacent to IM(J,K+1)

Asked:

on 4 Mar 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!