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:org.geowebcache.layer.MetaTile.java

@SuppressWarnings("rawtypes")
protected static void disposePlanarImageChain(PlanarImage pi, HashSet<PlanarImage> visited) {
    Vector sinks = pi.getSinks();
    // check all the sinks (the image might be in the middle of a chain)
    if (sinks != null) {
        for (Object sink : sinks) {
            if (sink instanceof PlanarImage && !visited.contains(sink)) {
                disposePlanarImageChain((PlanarImage) sink, visited);
            } else if (sink instanceof BufferedImage) {
                ((BufferedImage) sink).flush();
            }/*from ww w.  java  2s  . co  m*/
        }
    }
    // dispose the image itself
    pi.dispose();
    visited.add(pi);

    // check the image sources
    Vector sources = pi.getSources();
    if (sources != null) {
        for (Object child : sources) {
            if (child instanceof PlanarImage && !visited.contains(child)) {
                disposePlanarImageChain((PlanarImage) child, visited);
            } else if (child instanceof BufferedImage) {
                ((BufferedImage) child).flush();
            }
        }
    }

    // ImageRead might also hold onto a image input stream that we have to close
    if (pi instanceof RenderedOp) {
        RenderedOp op = (RenderedOp) pi;
        for (Object param : op.getParameterBlock().getParameters()) {
            if (param instanceof ImageInputStream) {
                ImageInputStream iis = (ImageInputStream) param;
                try {
                    iis.close();
                } catch (IOException e) {
                    // fine, we tried
                }
            }
        }
    }
}

From source file:net.sf.webphotos.tools.Thumbnail.java

private static String getFormatName(Object o) {
    try {/*from ww  w .  j  a va  2s .c o m*/
        // Create an image input stream on the image
        ImageInputStream iis = ImageIO.createImageInputStream(o);

        // Find all image readers that recognize the image format
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            // No readers found
            return null;
        }

        // Use the first reader
        ImageReader reader = iter.next();

        // Close stream
        iis.close();

        // Return the format name
        return reader.getFormatName();
    } catch (Exception e) {
        // The image could not be read
        return null;
    }
}

From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java

private final static void closeQuietly(ImageInputStream ins) {
    if (ins != null) {
        try {/*  w  w w  .ja v a 2 s.  c o m*/
            ins.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:at.gv.egiz.pdfas.common.utils.ImageUtils.java

public static Dimension getImageDimensions(InputStream is) throws IOException {
    ImageInputStream in = ImageIO.createImageInputStream(is);
    try {// www.  j  ava  2 s .  com
        final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            try {
                reader.setInput(in);
                return new Dimension(reader.getWidth(0), reader.getHeight(0));
            } finally {
                reader.dispose();
            }
        }
        throw new IOException("Failed to read Image file");
    } finally {
        if (in != null)
            in.close();
    }
}

From source file:nl.b3p.viewer.print.PrintInfo.java

/**
 * Load image dimensions, ideally without decoding the entire image.
 *//*from   w  w  w .j a va2 s  .  co  m*/
private static Dimension getImageDimension(InputStream image) throws IOException {
    // http://stackoverflow.com/questions/1559253/java-imageio-getting-image-dimension-without-reading-the-entire-file

    ImageInputStream in = ImageIO.createImageInputStream(image);
    try {
        final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            try {
                reader.setInput(in);
                return new Dimension(reader.getWidth(0), reader.getHeight(0));
            } finally {
                reader.dispose();
            }
        }
    } finally {
        if (in != null) {
            in.close();
        }
    }
    return null;
}

From source file:se.trixon.almond.GraphicsHelper.java

public static Dimension getImgageDimension(File imageFile) throws IOException {
    ImageInputStream inputStream = ImageIO.createImageInputStream(imageFile);

    try {/*w w w.  ja  v a 2 s  . c  om*/
        final Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(inputStream);
        if (imageReaders.hasNext()) {
            ImageReader imageReader = imageReaders.next();
            try {
                imageReader.setInput(inputStream);
                return new Dimension(imageReader.getWidth(0), imageReader.getHeight(0));
            } finally {
                imageReader.dispose();
            }
        }
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
    }

    return null;
}

From source file:com.lm.lic.manager.util.GenUtil.java

public static String senseImageFormat(byte[] icon) throws Exception {
    try {//from   w  ww. j  av  a  2 s  . c o m
        ByteArrayInputStream bais = new ByteArrayInputStream(icon);
        // Create an image input stream on the image
        ImageInputStream iis = ImageIO.createImageInputStream(bais);
        ImageIO.getReaderFormatNames();

        // Find all image readers that recognize the image format
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            return null;
        }
        ImageReader reader = iter.next();
        iis.close();
        return reader.getFormatName();
    } catch (IOException e) {
    }

    return null;
}

From source file:iqq.util.ImageUtil.java

/**
 * ??//from  w ww .j  a v  a  2  s . c  o  m
 *
 * @param imageFile 
 * @return ?
 */
public static String getFormatName(File imageFile) {
    if (imageFile == null || imageFile.length() == 0) {
        return null;
    }
    try {
        String formatName = null;
        ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageFile);
        Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInputStream);
        if (!iterator.hasNext()) {
            return null;
        }
        ImageReader imageReader = iterator.next();
        if (imageReader instanceof JPEGImageReader) {
            formatName = JPEG_FORMAT_NAME;
        } else if (imageReader instanceof GIFImageReader) {
            formatName = GIF_FORMAT_NAME;
        } else if (imageReader instanceof BMPImageReader) {
            formatName = BMP_FORMAT_NAME;
        } else if (imageReader instanceof PNGImageReader) {
            formatName = PNG_FORMAT_NAME;
        }
        imageInputStream.close();
        return formatName;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

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

/**
 * Determines the type of image, or null if not a valid image type
 * /*www .  j a va 2s .c  o m*/
 * @param imageLoc
 *            the string location of the image (uri syntax expected)
 * @return String the name of the image type/format, or null if not valid
 */
public static String getImageType(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, "getImageType", e);

        return null;
    }

    // Determine the image type and return it if valid
    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 null;
        }

        // Use the first reader
        ImageReader reader = (ImageReader) iter.next();
        iis.close();

        // Return the format name
        return reader.getFormatName();
    } catch (IOException e) {
        jlogger.log(Level.SEVERE, "getImageType", e);

    }

    // The image could not be read
    return null;
}

From source file:org.jamwiki.parser.image.ImageProcessor.java

/**
 * Retrieve image dimensions.  This method simply reads headers so it should perform
 * relatively fast./*from w w  w .ja  va 2  s  .com*/
 */
protected static Dimension retrieveImageDimensions(File imageFile) throws IOException {
    long start = System.currentTimeMillis();
    if (!imageFile.exists()) {
        logger.info("No file found while determining image dimensions: " + imageFile.getAbsolutePath());
        return null;
    }
    ImageInputStream iis = null;
    Dimension dimensions = null;
    ImageReader reader = null;
    // use a FileInputStream and make sure it gets closed to prevent unclosed file
    // errors on some operating systems
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(imageFile);
        iis = ImageIO.createImageInputStream(fis);
        Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
        if (readers.hasNext()) {
            reader = readers.next();
            reader.setInput(iis, true);
            dimensions = new Dimension(reader.getWidth(0), reader.getHeight(0));
        }
    } finally {
        if (reader != null) {
            reader.dispose();
        }
        if (iis != null) {
            try {
                iis.close();
            } catch (IOException e) {
                // ignore
            }
        }
        IOUtils.closeQuietly(fis);
    }
    if (logger.isDebugEnabled()) {
        long execution = (System.currentTimeMillis() - start);
        logger.debug("Image dimension lookup for " + imageFile.getAbsolutePath() + " took "
                + (execution / 1000.000) + " s");
    }
    return dimensions;
}