Example usage for javax.imageio.stream ImageInputStream close

List of usage examples for javax.imageio.stream ImageInputStream close

Introduction

In this page you can find the example usage for javax.imageio.stream ImageInputStream close.

Prototype

void close() throws IOException;

Source Link

Document

Closes the stream.

Usage

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  om
 * @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:com.occamlab.te.parsers.ImageParser.java

/**
 * Determines the height of the first image in an image file in pixels.
 * /*from  w w  w  .j  a v a 2  s  .  co 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;
}

From source file:VASSAL.tools.io.IOUtilsTest.java

@Test
public void testCloseQuietlyImageInputStreamClosed() throws IOException {
    final ImageInputStream in = new MemoryCacheImageInputStream(new ClosedInputStream());
    in.close();

    IOUtils.closeQuietly(in);//from   ww  w  .  j a  v a2s . c  o  m
}

From source file:VASSAL.tools.io.IOUtilsTest.java

@Test
public void testCloseQuietlyImageInputStreamOpen() {
    final ImageInputStream in = new MemoryCacheImageInputStream(new NullInputStream(1000));

    IOUtils.closeQuietly(in);/*from   w w w .j ava2  s .  co  m*/

    try {
        in.close();
        fail();
    } catch (IOException e) {
        // This, oddly, the expected behavior of close().
    }
}

From source file:it.tidalwave.imageio.test.ImageReaderTestSupport.java

/*******************************************************************************************************************
 * //from   ww  w.  j  ava2 s . c om
 * 
 ******************************************************************************************************************/
protected void close(final ImageReader ir) throws IOException {
    final ImageInputStream iis = (ImageInputStream) ir.getInput();
    iis.close();
    ir.dispose();
}

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

private int getImageWidth(Node node) {
    int imageWidth = 0;
    try {//from   w w  w.j ava  2  s  . c  om
        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);
        imageWidth = reader.getWidth(0);
        iis.close();
        reader.dispose();
    } catch (RepositoryException | IOException e) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("Can not get image width in " + this.getClass().getName());
        }
    }
    return imageWidth;
}

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

private int getImageHeight(Node node) {
    int imageHeight = 0;
    try {//  w  w w .j ava2s .  com
        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.carcv.core.model.file.FileCarImage.java

/**
 * Reads a rectangular region from an image in the inStream.
 *
 * @param inStream the InputStream from which to load the image fraction
 * @param rect specifies the rectangular region to load as the image
 * @throws IOException if an error during loading occurs
 *//*from   w  ww.  jav  a 2s  . co  m*/
public void loadFragment(InputStream inStream, Rectangle rect) throws IOException {
    ImageInputStream imageStream = ImageIO.createImageInputStream(inStream);
    ImageReader reader = ImageIO.getImageReaders(imageStream).next();
    ImageReadParam param = reader.getDefaultReadParam();

    param.setSourceRegion(rect);
    reader.setInput(imageStream, true, true);

    this.image = reader.read(0, param);

    reader.dispose();
    imageStream.close();
}

From source file:org.apache.pdfbox.pdmodel.graphics.shading.Type5ShadingContext.java

private List<ShadedTriangle> collectTriangles(PDShadingType5 latticeTriangleShadingType, AffineTransform xform,
        Matrix matrix) throws IOException {
    COSDictionary cosDictionary = latticeTriangleShadingType.getCOSObject();
    PDRange rangeX = latticeTriangleShadingType.getDecodeForParameter(0);
    PDRange rangeY = latticeTriangleShadingType.getDecodeForParameter(1);
    int numPerRow = latticeTriangleShadingType.getVerticesPerRow();
    PDRange[] colRange = new PDRange[numberOfColorComponents];
    for (int i = 0; i < numberOfColorComponents; ++i) {
        colRange[i] = latticeTriangleShadingType.getDecodeForParameter(2 + i);
    }/*  w  ww . j a v a 2s .com*/
    List<Vertex> vlist = new ArrayList<Vertex>();
    long maxSrcCoord = (long) Math.pow(2, bitsPerCoordinate) - 1;
    long maxSrcColor = (long) Math.pow(2, bitsPerColorComponent) - 1;
    COSStream cosStream = (COSStream) cosDictionary;

    ImageInputStream mciis = new MemoryCacheImageInputStream(cosStream.createInputStream());
    try {
        while (true) {
            Vertex p;
            try {
                p = readVertex(mciis, maxSrcCoord, maxSrcColor, rangeX, rangeY, colRange, matrix, xform);
                vlist.add(p);
            } catch (EOFException ex) {
                break;
            }
        }
    } finally {
        mciis.close();
    }
    int sz = vlist.size(), rowNum = sz / numPerRow;
    Vertex[][] latticeArray = new Vertex[rowNum][numPerRow];
    List<ShadedTriangle> list = new ArrayList<ShadedTriangle>();
    if (rowNum < 2) {
        // must have at least two rows; if not, return empty list
        return list;
    }
    for (int i = 0; i < rowNum; i++) {
        for (int j = 0; j < numPerRow; j++) {
            latticeArray[i][j] = vlist.get(i * numPerRow + j);
        }
    }

    for (int i = 0; i < rowNum - 1; i++) {
        for (int j = 0; j < numPerRow - 1; j++) {
            Point2D[] ps = new Point2D[] { latticeArray[i][j].point, latticeArray[i][j + 1].point,
                    latticeArray[i + 1][j].point };

            float[][] cs = new float[][] { latticeArray[i][j].color, latticeArray[i][j + 1].color,
                    latticeArray[i + 1][j].color };

            list.add(new ShadedTriangle(ps, cs));

            ps = new Point2D[] { latticeArray[i][j + 1].point, latticeArray[i + 1][j].point,
                    latticeArray[i + 1][j + 1].point };

            cs = new float[][] { latticeArray[i][j + 1].color, latticeArray[i + 1][j].color,
                    latticeArray[i + 1][j + 1].color };

            list.add(new ShadedTriangle(ps, cs));
        }
    }
    return list;
}

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;
    try {/*from  www.j av a2s .  c  o  m*/
        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();
        }
    }
}