Example usage for javax.imageio ImageIO createImageInputStream

List of usage examples for javax.imageio ImageIO createImageInputStream

Introduction

In this page you can find the example usage for javax.imageio ImageIO createImageInputStream.

Prototype

public static ImageInputStream createImageInputStream(Object input) throws IOException 

Source Link

Document

Returns an ImageInputStream that will take its input from the given Object .

Usage

From source file:org.exoplatform.wcm.notification.plugin.FileActivityChildPlugin.java

private int getImageHeight(Node node) {
    int imageHeight = 0;
    try {/*w w  w. ja  v a2  s .  c o m*/
        if (node.hasNode(NodetypeConstant.JCR_CONTENT))
            node = node.getNode(NodetypeConstant.JCR_CONTENT);
        ImageReader reader = ImageIO.getImageReadersByMIMEType(mimeType).next();
        ImageInputStream iis = ImageIO.createImageInputStream(node.getProperty("jcr:data").getStream());
        reader.setInput(iis, true);
        imageHeight = reader.getHeight(0);
        iis.close();
        reader.dispose();
    } catch (RepositoryException | IOException e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("Can not get image height in " + this.getClass().getName());
        }
    }
    return imageHeight;
}

From source file:org.apache.pdfbox.tools.imageio.TestImageIOUtils.java

/**
 * checks whether the compression of a TIFF file is as expected.
 *
 * @param filename Filename// www  .  j a  v a2s  .  com
 * @param expectedCompression expected TIFF compression
 *
 * @throws IOException if something goes wrong
 */
void checkTiffCompression(String filename, String expectedCompression) throws IOException {
    Iterator readers = ImageIO.getImageReadersBySuffix("tiff");
    ImageReader reader = (ImageReader) readers.next();
    ImageInputStream iis = ImageIO.createImageInputStream(new File(filename));
    reader.setInput(iis);
    IIOMetadata imageMetadata = reader.getImageMetadata(0);
    Element root = (Element) imageMetadata.getAsTree(STANDARD_METADATA_FORMAT);
    Element comprElement = (Element) root.getElementsByTagName("Compression").item(0);
    Node comprTypeNode = comprElement.getElementsByTagName("CompressionTypeName").item(0);
    String actualCompression = comprTypeNode.getAttributes().getNamedItem("value").getNodeValue();
    assertEquals("Incorrect TIFF compression in file " + filename, expectedCompression, actualCompression);
    iis.close();
    reader.dispose();
}

From source file:com.funambol.foundation.util.MediaUtils.java

/**
 * Creates the thumbnail.//  w  w  w  .  j a  v a2 s .  c o  m
 *
 * @param imageFile the image file
 * @param thumbFile the empty thumbnail file
 * @param thumbX the width of the thumbnail
 * @param thumbY the height of the thumbnail
 * @param imageName the image file name with extension
 * @param tolerance the percentage of tolerance before creating a thumbnail
 * @return true is the thumbnail has been created, false otherwise
 * @throws IOException if an error occurs
 */
private static boolean createThumbnail(File imageFile, File thumbFile, int thumbX, int thumbY, String imageName,
        double tolerance) throws IOException {

    FileInputStream fileis = null;
    ImageInputStream imageis = null;

    Iterator readers = null;

    try {

        readers = ImageIO.getImageReadersByFormatName(imageName.substring(imageName.lastIndexOf('.') + 1));
        if (readers == null || (!readers.hasNext())) {
            throw new IOException("File not supported");
        }

        ImageReader reader = (ImageReader) readers.next();

        fileis = new FileInputStream(imageFile);
        imageis = ImageIO.createImageInputStream(fileis);
        reader.setInput(imageis, true);

        // Determines thumbnail height, width and quality
        int thumbWidth = thumbX;
        int thumbHeight = thumbY;

        double thumbRatio = (double) thumbWidth / (double) thumbHeight;
        int imageWidth = reader.getWidth(0);
        int imageHeight = reader.getHeight(0);

        //
        // Don't create the thumbnail if the original file is smaller
        // than required size increased by % tolerance
        //
        if (imageWidth <= (thumbWidth * (1 + tolerance / 100))
                && imageHeight <= (thumbHeight * (1 + tolerance / 100))) {

            return false;
        }

        double imageRatio = (double) imageWidth / (double) imageHeight;
        if (thumbRatio < imageRatio) {
            thumbHeight = (int) (thumbWidth / imageRatio);
        } else {
            thumbWidth = (int) (thumbHeight * imageRatio);
        }

        ImageReadParam param = reader.getDefaultReadParam();
        param.setSourceSubsampling(3, 3, 0, 0);

        BufferedImage bi = reader.read(0, param);

        Image thumb = bi.getScaledInstance(thumbWidth, thumbHeight, Image.SCALE_SMOOTH);

        BufferedImage thumbImage = new BufferedImage(thumbWidth, thumbHeight, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = thumbImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(thumb, 0, 0, thumbWidth, thumbHeight, null);

        FileOutputStream fileOutputStream = new FileOutputStream(thumbFile);
        ImageIO.write(thumbImage, "jpg", fileOutputStream);

        thumb.flush();
        thumbImage.flush();
        fileOutputStream.flush();
        fileOutputStream.close();
        graphics2D.dispose();

    } finally {
        if (fileis != null) {
            fileis.close();
        }
        if (imageis != null) {
            imageis.close();
        }
    }

    return true;
}

From source file:com.occamlab.te.parsers.ImageParser.java

/**
 * Determines the width of the first image in an image file in pixels.
 * //  w  w w.  j av  a2 s  . c  o m
 * @param imageLoc
 *            the string location of the image (uri syntax expected)
 * @return int the image width in pixels, or -1 if unable.
 * @author Paul Daisey added 2011-05-13 to support WMTS ETS
 */
public static int getImageWidth(String imageLoc) {

    // Get the image as an InputStream
    InputStream is = null;
    try {
        URI imageUri = new URI(imageLoc);
        URL imageUrl = imageUri.toURL();
        is = imageUrl.openStream();
    } catch (Exception e) {
        jlogger.log(Level.SEVERE, "getImageWidth", e);

        return -1;
    }
    // Determine the image width
    try {
        // Create an image input stream on the image
        ImageInputStream iis = ImageIO.createImageInputStream(is);

        // Find all image readers that recognize the image format
        Iterator iter = ImageIO.getImageReaders(iis);

        // No readers found
        if (!iter.hasNext()) {
            return -1;
        }

        // Use the first reader
        ImageReader reader = (ImageReader) iter.next();
        reader.setInput(iis, true);
        int width = reader.getWidth(0);
        iis.close();

        return width;
    } catch (IOException e) {
        jlogger.log(Level.SEVERE, "getImageWidth", e);
        // The image could not be read
    }
    return -1;
}

From source file:org.hippoecm.frontend.plugins.gallery.imageutil.ScaleImageOperationTest.java

private void checkImageDimensions(ScaleImageOperation scaleOp, String mimeType, int expectedWidth,
        int expectedHeight) throws IOException {
    assertEquals(expectedWidth, scaleOp.getScaledWidth());
    assertEquals(expectedHeight, scaleOp.getScaledHeight());

    ImageReader reader = ImageIO.getImageReadersByMIMEType(mimeType).next();
    ImageInputStream iis = null;/*from ww w. ja va2s . c o  m*/
    try {
        iis = ImageIO.createImageInputStream(scaleOp.getScaledData());
        reader.setInput(iis);
        assertEquals(scaleOp.getScaledWidth(), reader.getWidth(0));
        assertEquals(scaleOp.getScaledHeight(), reader.getHeight(0));
    } finally {
        if (iis != null) {
            iis.close();
        }
    }
}

From source file:org.geoserver.wcs.GetCoverageTest.java

@Test
public void testRasterFilterGreen() throws Exception {
    String queryString = "wcs?identifier=" + getLayerId(MOSAIC) + "&request=getcoverage"
            + "&service=wcs&version=1.1.1&&format=image/tiff"
            + "&BoundingBox=0,0,1,1,urn:ogc:def:crs:EPSG:6.6:4326" + "&CQL_FILTER=location like 'green%25'";

    MockHttpServletResponse response = getAsServletResponse(queryString);

    // parse the multipart, check there are two parts
    Multipart multipart = getMultipart(response);
    assertEquals(2, multipart.getCount());
    BodyPart coveragePart = multipart.getBodyPart(1);
    assertEquals("image/tiff", coveragePart.getContentType());
    assertEquals("<theCoverage>", coveragePart.getHeader("Content-ID")[0]);

    // make sure we can read the coverage back
    ImageReader reader = ImageIO.getImageReadersByFormatName("tiff").next();
    reader.setInput(ImageIO.createImageInputStream(coveragePart.getInputStream()));
    RenderedImage image = reader.read(0);

    // check the pixel
    int[] pixel = new int[3];
    image.getData().getPixel(0, 0, pixel);
    assertEquals(0, pixel[0]);/*from  w w w.  j  a va 2  s  . com*/
    assertEquals(255, pixel[1]);
    assertEquals(0, pixel[2]);
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlImage.java

private void readImageIfNeeded() throws IOException {
    downloadImageIfNeeded();/*from   w w  w  .  ja  va 2s .c  o m*/
    if (imageData_ == null) {
        if (null == imageWebResponse_) {
            throw new IOException("No image response available (src=" + getSrcAttribute() + ")");
        }
        final ImageInputStream iis = ImageIO.createImageInputStream(imageWebResponse_.getContentAsStream());
        final Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            iis.close();
            throw new IOException("No image detected in response");
        }
        final ImageReader imageReader = iter.next();
        imageReader.setInput(iis);
        imageData_ = new ImageData(imageReader);

        // dispose all others
        while (iter.hasNext()) {
            iter.next().dispose();
        }
    }
}

From source file:com.occamlab.te.parsers.ImageParser.java

/**
 * Determines the height of the first image in an image file in pixels.
 * /*w  ww. j  a  v  a2 s. c  o m*/
 * @param imageLoc
 *            the string location of the image (uri syntax expected)
 * @return int the image width in pixels, or -1 if unable.
 * @author Paul Daisey added 2011-05-13 to support WMTS ETS
 */
public static int getImageHeight(String imageLoc) {

    // Get the image as an InputStream
    InputStream is = null;
    try {
        URI imageUri = new URI(imageLoc);
        URL imageUrl = imageUri.toURL();
        is = imageUrl.openStream();
    } catch (Exception e) {
        jlogger.log(Level.SEVERE, "getImageWidth", e);

        return -1;
    }
    // Determine the image width
    try {
        // Create an image input stream on the image
        ImageInputStream iis = ImageIO.createImageInputStream(is);

        // Find all image readers that recognize the image format
        Iterator iter = ImageIO.getImageReaders(iis);

        // No readers found
        if (!iter.hasNext()) {
            return -1;
        }

        // Use the first reader
        ImageReader reader = (ImageReader) iter.next();
        reader.setInput(iis, true);
        int height = reader.getHeight(0);
        iis.close();

        return height;
    } catch (IOException e) {
        jlogger.log(Level.SEVERE, "getImageWidth", e);
        // The image could not be read
    }
    return -1;
}