MATLAB Answers

Mongo and GridFS Java Driver with MATLAB

10 views (last 30 days)
I am trying to upload and download pictures from MATLAB to MongoDB using the Mongo Java Driver.
I am following below tutorial on GridFS java:
In MATLAB I did :
javaaddpath("C:\Users\****\Documents\jar_files\mongo-java-driver-3.11.0-beta3.jar")
import com.mongodb.Block;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.*;
import com.mongodb.client.gridfs.model.*;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.io.*;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import com.mongodb.client.model.Filters.eq;
mongoClient = MongoClients.create();
myDatabase = mongoClient.getDatabase("TestData");
gridFSFilesBucket = GridFSBuckets.create(myDatabase, "PicturesTest");
streamToUploadFrom = FileInputStream(File("C:\Users\****\Pictures\MyPicture.png"));
Until that point it works but then I can't do the following line:
fileId = gridFSBucket.uploadFromStream("mongodb-tutorial", streamToUploadFrom)
I get follwing error message ;
Undefined variable "gridFSBucket" or class "gridFSBucket.uploadFromStream".
I do not get why it is not able to find gridFSBucket function in the java driver.
I tried to play with the jar files it worked once but is not working anymore (do not know what I did exactly)...
Any help is much appreciated.
Thanks.

  0 Comments

Sign in to comment.

Accepted Answer

Debasish Samal
Debasish Samal on 7 Jun 2019
The error occurs because there is no such object "gridFSBucket".
You need to create an object "gridFSBucket" of type GridFSBucket. Just add this line to your code:
gridFSBucket = GridFSBuckets.create(myDatabase);
Then add this:
fileId = gridFSBucket.uploadFromStream("mongodb-tutorial", streamToUploadFrom);

  1 Comment

Marc Youcef
Marc Youcef on 7 Jun 2019
Perfect it solved the issue in fact I badly re coded the java code shown in the tutorial webpage into MATLAB.
I am gonna add the reste of the script for people interested on having the full upload/download code to MongoDB. Thanks again for the help.

Sign in to comment.

More Answers (1)

Marc Youcef
Marc Youcef on 7 Jun 2019
Edited: Marc Youcef on 7 Jun 2019
Please find below an example of how to upload/download a picture from MATLAB to MongoDB:
javaaddpath("C:\Users\***\Documents\jar_files\mongo-java-driver-3.11.0-beta3.jar")
import com.mongodb.Block;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.*;
import com.mongodb.client.gridfs.model.*;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.io.*;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import com.mongodb.client.model.Filters.eq;
%connection
mongoClient = MongoClients.create();
myDatabase = mongoClient.getDatabase("TestDb");
%Upload Picture
gridFSFilesBucket = GridFSBuckets.create(myDatabase, "PicturesTest");
streamToUploadFrom = FileInputStream(File("C:\Users\***\Pictures\Mypicture.png"));
fileId = gridFSFilesBucket.uploadFromStream("mongodb-tutorial", streamToUploadFrom)
%Download Picture
streamToDownloadTo = FileOutputStream("C:\Users\***\Pictures\Mypicture.png");
gridFSFilesBucket.downloadToStream(fileId, streamToDownloadTo);
streamToDownloadTo.close();
downloadStream = gridFSFilesBucket.openDownloadStream(fileId);
% Create the copier class
isc = com.mathworks.mlwidgets.io.InterruptibleStreamCopier.getInterruptibleStreamCopier;
% Create a buffer to copy into
outputStream = java.io.ByteArrayOutputStream;
% Copy from your download stream to this buffer
isc.copyStream(downloadStream,outputStream);
% Close the buffer
outputStream.close
% Get the data from the buffer as array of int8 in MATLAB
binaryData = outputStream.toByteArray;
import java.io.ByteArrayInputStream;
import javax.imageio.ImageIO;
bais = ByteArrayInputStream(outputStream.toByteArray());
jbi = ImageIO.read(bais);
nrows = jbi.getHeight; ncols = jbi.getWidth;
data = reshape(typecast(jbi.getData.getDataStorage, 'uint8'), [], ncols, nrows);
matImg = permute(data,[3 2 1]);
% Re order RGB
matImg2=matImg(:,:,3);
matImg2(:,:,2)=matImg(:,:,2);
matImg2(:,:,3)=matImg(:,:,1);
% Display Image
imagesc(matImg2);

  2 Comments

Martijn
Martijn on 12 Jun 2019
Hello Marc,
While your code is correct, it can be slightly optimized, there is no need to actually copy the streams, ImageIO.read should be able to read from the download stream directly:
%% Setup
% Load the driver
javaaddpath mongo-java-driver-3.9.1.jar
%% Upload
% Connect to the server and database
mongoClient = com.mongodb.client.MongoClients.create();
myDatabase = mongoClient.getDatabase("TestDb");
% Get GridFS Bucket
gridFSFilesBucket = com.mongodb.client.gridfs.GridFSBuckets.create(myDatabase, "PicturesTest");
% Open local file
streamToUploadFrom = java.io.FileInputStream(java.io.File(fullfile(pwd,"spy.png")));
% Upload to the server
fileId = gridFSFilesBucket.uploadFromStream("mongodb-tutorial", streamToUploadFrom);
% Close the local file
streamToUploadFrom.close();
%% Download
% Connect to the server and database
mongoClient = com.mongodb.client.MongoClients.create('mongodb://192.168.99.105:27017');
myDatabase = mongoClient.getDatabase("TestDb");
% Get GridFS Bucket
gridFSFilesBucket = com.mongodb.client.gridfs.GridFSBuckets.create(myDatabase, "PicturesTest");
% Open the download stream
downloadStream = gridFSFilesBucket.openDownloadStream('mongodb-tutorial');
% Download from the stream as image
javaImage = javax.imageio.ImageIO.read(downloadStream);
% Close the download stream
downloadStream.close
% Process the Java image into a MATLAB image
% Determine the image height and width
nrows = javaImage.getHeight; ncols = javaImage.getWidth;
% Convert to MATLAB image
% In Java these images are typically stored in a row first BGR interleaved
% format, so that is BGR value of top-left pixel followed by BGR value
% of the pixel to the right of it, etc. until the end of the line, then BGR
% value of the first pixel on the second line, followed by BGR value of
% pixel to the right of it, etc.
% In MATLAB this is different, we actually store by column instead of row
% first and the different RGB channels are actually in the last dimension
% instead of the first.
% First reshape the Java image into a format where we have access to the
% separate dimensions: BGR x width x height
data = reshape(typecast(javaImage.getData.getDataStorage, 'uint8'), [], ncols, nrows);
% Re-order those to get MATLAB format: height x width x BGR
matImg = permute(data,[3 2 1]);
% Re-order BGR into RGB
matImg2=matImg(:,:,[3 2 1]);
% Display Image
imagesc(matImg2);
Regards,
Martijn
Marc Youcef
Marc Youcef on 19 Jun 2019
Many thanks Martijn, this got the work done faster !

Sign in to comment.

Categories

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by