I am working with Matlab and the Java driver. After writing some big data to the DB I want to read them out later. The files are about 100MB big and because of that I am writing them into GridFS.
I read the data with the Java driver into Matlab this way:
dbgridfs = GridFS(db, 'data_gridfs');
file1 = dbgridfs.findOne('bigdata');
After that code I will get an object in my Matlab workspace: Name Size Bytes Class file1 1x1 com.mongodb.gridfs.GridFSDBFile
Now I have a problem with converting the (Java?)-object into a nativ Matlab variable.
I searched a lot on different sites, but I don't get it. At the moment I am writing the data on my harddisk and after that I read it into a native Matlab variable - but this is a really dirty way and don't ask me relating to the performance :(
Are there any existing solutions I missed with the Java driver and/or do you know some code which could help me? Thanks for your help.
regards matl
I have never responded to any forum question, but I have benefitted a lot especially from stackoverflow, so I thought I have to give something back at least once. Since the above issue was bugging me now for nearly two days and I finally stitched together a solution I thought this would be a good topic to give something back to the community.
I had the same issue of retrieving an image (.png) from a MongoDB/GridFS with the Java driver. First, as also described above, you need to retrieve the file Java object:
import com.mongodb.*;
import com.mongodb.gridfs.*;
mongoClient = MongoClient('server_name',27017);
db = mongoClient.getDB('database_name');
imgData = GridFS(db,'image_data');
The above code part is getting the collection with the image data. Afterwards, the image data Java object can be retrieved:
javaIObj = imgData.findOne('image_name.png');
Now ByteArrayOutputStream is needed where the data stream can be piped into instead of a file:
import java.io.ByteArrayOutputStream;
baos = ByteArrayOutputStream();
javaIObj.writeTo(baos);
The output stream needs again to be piped into a ByteArrayInputStream which can be used to construct an ImageIO object:
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
bais = ByteArrayInputStream(baos.toByteArray());
jbi = ImageIO.read(bais);
Now the actual Matlab image as matrix can be retrieved and displayed:
nrows = jbi.getHeight; ncols = jbi.getWidth;
data = jbi.getData.getPixels(0,0,ncols,nrows,[]);
matImg = reshape(data,ncols,nrows)';
imagesc(matImg);
I do not know if this is the perfect solution, but it worked for me.
Cheers!
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.