I think I found a pretty clean solution for my problem, so I'll publish it as an answer myself. There is a simple open source java framework for .mat file exporting called JMatIO (also available at SourceForge, but here be sure to download the newest version 0.2). It allows to easily export data in .mat format which is then available to Matlab in a straightforward manner. If you have any problems using it, due to little documentation, download the source from here:
svn co https://jmatio.svn.sourceforge.net/svnroot/jmatio jmatio
and take a look at unit tests, they show how it can be used.
Back to my problem: I needed to upload a burst of frames to Matlab. An image in Matlab is a 2D matrix, so a burst of them would be a 3D matrix. I didn't figure out how to export Java arrays as 3D martices in Matlab though, so I exported each java.awt.image.BufferedImage
as a row in Matlab, which is easily done. It takes some trivial data manipulation in Matlab to represent the data as one would want to afterwards (matrix transpose and reshape function). Take a look at the code snippet below. The function export will buffer 100 frames and if called again after that it'll export them to a file with 8-bit color depth.
public class MatFileExporter {
private BufferedImage frame;
private int[][] frames;
private int frameSize;
private int numFrames = 100;
private int frameNumber = 0;
protected void export() {
//This will only work with 8-bit coded SampleModels, change if needed
if (frames == null) {
frameSize = frame.getData().getWidth() * frame.getHeight();
frames = new int[numFrames][frameSize];
}
if (frameNumber < numFrames) {
frame.getData().getPixels(0, 0, frame.getWidth(),
frame.getHeight(), frames[frameNumber++]);
} else {
byte[][] framesByte = new byte[numFrames][frameSize];
for (int i=0; i<numFrames; i++) {
for (int j=0; j<frameSize; j++) {
framesByte[i][j] = (byte) frames[i][j];
}
}
MLUInt8 array = new MLUInt8("frames", framesByte);
ArrayList<MLArray> list = new ArrayList<MLArray>();
list.add(array);
new MatFileWriter( "frames.mat", list );
}
}
}
Feel free to use and change it, hope it helps someone. Incremental .mat-file writing is also supported, consult the source code for that.