Example usage for com.mongodb.gridfs GridFSDBFile getInputStream

List of usage examples for com.mongodb.gridfs GridFSDBFile getInputStream

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFSDBFile getInputStream.

Prototype

public InputStream getInputStream() 

Source Link

Document

Returns an InputStream from which data can be read.

Usage

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public static ImageHandler createImage(GridFSDBFile file) {

    TiffDecoder td = new TiffDecoder(file.getInputStream(), file.getFilename());
    try {//ww  w.  j av a 2  s.  co m
        FileInfo[] info = td.getTiffInfo();
        ImagePlus imp = null;
        //System.out.println("opening file: depth:"+info.length+ " info0:"+info[0].toString());
        if (info.length > 1) { // try to open as stack
            Opener o = new Opener();
            o.setSilentMode(true);
            imp = o.openTiffStack(info);
            imp.setTitle(file.getFilename());
            if (file.containsField("pixelDepth")) {
                imp.getCalibration().pixelDepth = (Double) file.get("pixelDepth");
            }
            if (file.containsField("unit")) {
                imp.getCalibration().setUnit((String) file.get("unit"));
            }
            file.getInputStream().close();
            if (imp != null) {
                ImageHandler ih = ImageHandler.wrap(imp);
                if (file.containsField("offsetX")) {
                    ih.offsetX = (Integer) file.get("offsetX");
                }
                if (file.containsField("offsetY")) {
                    ih.offsetY = (Integer) file.get("offsetY");
                }
                if (file.containsField("offsetZ")) {
                    ih.offsetZ = (Integer) file.get("offsetZ");
                }
                return ih;
            }
        } else {
            // FIXME not tested!!
            Opener o = new Opener();
            imp = o.openTiff(file.getInputStream(), file.getFilename());
            file.getInputStream().close();
            if (imp != null) {
                return ImageHandler.wrap(imp);
            }
        }
    } catch (Exception e) {
        exceptionPrinter.print(e, "", Core.GUIMode);
    }
    return null;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized ImageIcon getFieldThumbnail(ObjectId field_id) {
    BasicDBObject query = new BasicDBObject("field_id", field_id);
    GridFSDBFile f = gfsFieldThumbnail.findOne(query);
    if (f != null) {
        try {/*  w  w w. ja  v a  2 s. co  m*/
            Image im = ImageIO.read(f.getInputStream());
            if (im != null) {
                return new ImageIcon(im);
            }
        } catch (Exception e) {
            exceptionPrinter.print(e, "", Core.GUIMode);
        }
    }
    return null;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized ImageIcon getFieldThumbnail(ObjectId field_id, int fileIdx) {
    BasicDBObject query = new BasicDBObject("field_id", field_id).append("fileRank", fileIdx);
    GridFSDBFile f = gfsFieldThumbnail.findOne(query);
    if (f != null) {
        try {/*from  ww w .ja v  a  2s  .  c  o  m*/
            Image im = ImageIO.read(f.getInputStream());
            if (im != null) {
                return new ImageIcon(im);
            }
        } catch (Exception e) {
            exceptionPrinter.print(e, "", Core.GUIMode);
        }
    }
    return null;
}

From source file:tango.mongo.MongoConnector.java

License:Open Source License

public synchronized ImageIcon getChannelThumbnail(ObjectId nucleus_id, int fileIdx) {
    BasicDBObject query = new BasicDBObject("nucleus_id", nucleus_id).append("fileRank", fileIdx);
    GridFSDBFile f = gfsNucleusThumbnail.findOne(query);
    if (f != null) {
        InputStream is = f.getInputStream();
        try {/*w  ww. j  av  a 2 s  . c  o m*/
            Image im = ImageIO.read(is);
            if (im != null) {
                return new ImageIcon(im);
            }
        } catch (Exception e) {
            exceptionPrinter.print(e, "", Core.GUIMode);
        }
    }
    return null;
}

From source file:th.co.entronica.mjolnir.controller.home.MainController.java

@RequestMapping(value = "getPhoto/{imgId}", method = RequestMethod.GET)
public @ResponseBody void getPhoto(HttpServletRequest request, HttpServletResponse response,
        @PathVariable String imgId) {
    try {/*from   w  w  w  .j a  v a2s .com*/
        GridFSDBFile imageForOutput = companyInformationManager.getByFilename(imgId);
        InputStream is = imageForOutput.getInputStream();
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] data = new byte[16384];
        while ((nRead = is.read(data, 0, data.length)) != -1) {
            buffer.write(data, 0, nRead);
        }
        buffer.flush();
        byte[] imagenEnBytes = buffer.toByteArray();

        response.setHeader("Accept-ranges", "bytes");
        response.setContentType("image/jpeg");
        response.setContentLength(imagenEnBytes.length);
        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Content-Description", "File Transfer");
        response.setHeader("Content-Transfer-Encoding:", "binary");

        OutputStream out = response.getOutputStream();
        out.write(imagenEnBytes);
        out.flush();
        out.close();
    } catch (Exception e) {
    }
}

From source file:xbdd.webapp.resource.feature.Attachment.java

License:Apache License

@GET
@Path("/{id}")
public javax.ws.rs.core.Response getAttachment(@PathParam("id") final String id) throws IOException {
    final DB db = this.client.getDB("grid");
    final GridFS gridFS = new GridFS(db);
    final GridFSDBFile file = gridFS.findOne(id);
    // log.info(file);
    if (file == null) {
        throw new WebApplicationException(404);
    }//from  w  w w . j  a  va2s.c o  m
    return Response.ok(org.apache.commons.io.IOUtils.toByteArray(file.getInputStream()), file.getContentType())
            .build();

}