Send Matlab Image to Android Tablet

I developed an Android application that connects to a laptop running Matlab via Bluetooth SPP. I can easily send lines back and forth, and now I'm interested in sending an image from Matlab for display on a tablet (48x64 gray enough). I am not sure how to pack the image and send it to the Matlab serial port. I assume that you cannot just use fprintf or fwrite.

Here is what I think the Android side might look like

public void drawImage(byte[] buffer){ ImageView camView = (ImageView)findViewById(R.id.camView); Bitmap myBitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length); Log.d(TAG,"decoded image"); if(myBitmap != null){ camView.setImageBitmap(myBitmap); Log.d(TAG,"Trying to display..."); } else{ Log.d(TAG, "Bitmap = null"); } }// end drawImage 

Any advice on the side of Andrei or Matlab is welcome. Thanks!

+4
source share
2 answers

So, I started to work. The main problem was that I did not send the image with proper compression (either .jpg or .png). I found that if you have a Matlab image that Matlab represents simply as a matrix of pixel values โ€‹โ€‹regardless of compression, you need to create java BufferedImage in order to properly construct the byte array so that you can decode it on the Android side.

Matlab Side

 import java.awt.*; import java.io.*; import java.util.*; import javax.imageio.*; serCam = InitUSBcamera; % initialize USB camera as a serial port type = java.lang.String('jpg'); % translating matlab to java outputStream = ByteArrayOutputStream; % create java output stream im = getsnapshot(serCam); % get an image from the camera im2 = imresize(im, [96,128],'nearest'); % reduce the size im3 = im2java2d(im2); % create java Buffered Image ImageIO.write(im3, type, outputStream); bytes = outputStream.toByteArray(); fwrite(serTablet, bytes, 'int8') % send the image // changed to async 
0
source

Are you just trying to get serial data from Matlab? Why not treat it like any other sequential data.

I first debugged the application for the Android console to make sure it was transferred at all, and also to make sure that the data is transmitting what you want to transfer.

0
source

All Articles