Example usage for javax.imageio ImageReader dispose

List of usage examples for javax.imageio ImageReader dispose

Introduction

In this page you can find the example usage for javax.imageio ImageReader dispose.

Prototype

public void dispose() 

Source Link

Document

Allows any resources held by this object to be released.

Usage

From source file:net.algart.simagis.imageio.IIOMetadataToJsonConverter.java

private JSONObject extendedReadExif(InputStream inputStream) throws IOException, JSONException {
    assert inputStream != null;
    ImageInputStream iis = ImageIO.createImageInputStream(inputStream);
    Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
    try {//from   ww  w.j  av a  2  s  .  com
        if (!readers.hasNext()) {
            return null; // no readers to read such "Exif"
        }
        ImageReader reader = readers.next();
        try {
            reader.setInput(iis);
            return toJson(reader.getImageMetadata(0));
        } finally {
            reader.dispose();
        }
    } finally {
        iis.close();
    }
}

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
 *//* w  w w.  j  a  v  a  2  s.c o  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:it.tidalwave.imageio.test.ImageReaderTestSupport.java

/*******************************************************************************************************************
 * // ww w. j a  va 2s.c o m
 * 
 ******************************************************************************************************************/
protected void close(final ImageReader ir) throws IOException {
    final ImageInputStream iis = (ImageInputStream) ir.getInput();
    iis.close();
    ir.dispose();
}

From source file:org.geoserver.wps.ppio.CoveragePPIOTest.java

private void testIsFormat(GridCoverage2D coverage, CoveragePPIO ppio, File encodedFile, String formatName)
        throws Exception {
    try (FileOutputStream fos = new FileOutputStream(encodedFile)) {
        ppio.encode(coverage, fos);/*  w ww . j a  v  a  2s.  c  o m*/
    }
    try (FileImageInputStream fis = new FileImageInputStream(encodedFile)) {
        ImageReader imageReader = null;
        try {
            imageReader = ImageIO.getImageReaders(fis).next();
            imageReader.setInput(fis);
            assertTrue(formatName.equalsIgnoreCase(imageReader.getFormatName()));
            assertNotNull(imageReader.read(0));
        } finally {
            if (imageReader != null) {
                try {
                    imageReader.dispose();
                } catch (Throwable t) {
                    // Ignore it.
                }
            }
        }
    }
}

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

private int getImageWidth(Node node) {
    int imageWidth = 0;
    try {/*  www .j a v  a  2s  .  co  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);
        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 {// www  . jav  a  2  s  .  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.onehippo.forge.gallerymagick.core.command.ScalrProcessorUtils.java

/**
 * Resize the given image {@code sourceFile} with resizing it to {@code width} and {@code height}
 * and store the resized image to {@code targetFile}, with appending {@code extraOptions} in the command line if provided.
 * @param sourceFile source image file//www. j  av a  2 s . c om
 * @param targetFile target image file
 * @param dimension image dimension
 * @param extraOptions extra command line options
 * @throws IOException if IO exception occurs
 */
public static void resizeImage(File sourceFile, File targetFile, ImageDimension dimension,
        String... extraOptions) throws IOException {
    if (dimension == null) {
        throw new IllegalArgumentException("Invalid dimension: " + dimension);
    }

    ImageReader reader = null;
    ImageWriter writer = null;

    try {
        reader = getImageReader(sourceFile);

        if (reader == null) {
            throw new IllegalArgumentException(
                    "Unsupported image file name extension for reading: " + sourceFile);
        }

        writer = getImageWriter(targetFile);

        if (writer == null) {
            throw new IllegalArgumentException(
                    "Unsupported image file name extension for writing: " + targetFile);
        }

        BufferedImage sourceImage = reader.read(0);
        BufferedImage resizedImage = Scalr.resize(sourceImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC,
                dimension.getWidth(), dimension.getHeight());

        final ImageWriteParam writeParam = writer.getDefaultWriteParam();

        if (writeParam.canWriteCompressed()) {
            String[] compressionTypes = writeParam.getCompressionTypes();

            if (compressionTypes != null && compressionTypes.length > 0) {
                writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                writeParam.setCompressionType(compressionTypes[0]);
                writeParam.setCompressionQuality(1.0f);
            }
        }

        final IIOImage iioImage = new IIOImage(resizedImage, null, null);
        writer.write(null, iioImage, writeParam);
    } finally {
        if (reader != null) {
            reader.dispose();
        }
        if (writer != null) {
            writer.dispose();
        }
    }
}

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

/**
 * Executes an image operation.//w  w w .  j  av a2 s .  c  om
 *
 * @param data the image data. The stream is closed by this method.
 * @param mimeType MIME type of the image
 *
 * @throws GalleryException when the image operation fails
 */
public void execute(InputStream data, String mimeType) throws GalleryException {
    ImageReader reader = null;
    ImageWriter writer = null;
    try {
        reader = ImageUtils.getImageReader(mimeType);
        if (reader == null) {
            throw new GalleryException("Unsupported MIME type for reading: " + mimeType);
        }
        writer = ImageUtils.getImageWriter(mimeType);
        if (writer == null) {
            throw new GalleryException("Unsupported MIME type for writing: " + mimeType);
        }
        execute(data, reader, writer);
    } catch (IOException e) {
        throw new GalleryException("Could not execute image operation", e);
    } finally {
        // the != null checks are unnecessary, but otherwise Sonar will report critical issues
        if (reader != null) {
            reader.dispose();
        }
        if (writer != null) {
            writer.dispose();
        }
        IOUtils.closeQuietly(data);
    }
}

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

private Size getImageSize(VFSLeaf media, String suffix) {
    Size result = null;/*from  ww w  .  j ava2s  . c om*/
    Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
    if (iter.hasNext()) {
        ImageInputStream stream = null;
        InputStream mediaStream = null;
        ImageReader reader = iter.next();
        try {
            mediaStream = media.getInputStream();
            if (mediaStream != null) {
                stream = new MemoryCacheImageInputStream(mediaStream);
                reader.setInput(stream);
                int readerMinIndex = reader.getMinIndex();
                int width = reader.getWidth(readerMinIndex);
                int height = reader.getHeight(readerMinIndex);
                result = new Size(width, height, 0, 0, false);
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        } finally {
            IOUtils.closeQuietly(stream);
            IOUtils.closeQuietly(mediaStream);
            reader.dispose();
        }
    } else {
        log.error("No reader found for given format: " + suffix);
    }
    return result;
}

From source file:org.exoplatform.wcm.ext.component.activity.FileUIActivity.java

protected int getImageWidth(Node node) {
    int imageWidth = 0;
    try {//w ww. ja  va2  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);
        imageWidth = reader.getWidth(0);
        iis.close();
        reader.dispose();
    } catch (Exception e) {
        LOG.info("Cannot get node");
    }
    return imageWidth;
}