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:common.utils.ImageUtils.java

/**
 * closes iis/*from w  w  w. j a  v a  2  s  .  c o  m*/
 * @param iis
 * @return image from given stream or null if no readers found
 * @throws java.io.IOException
 */
public static BufferedImageHolder readImage(ImageInputStream iis) throws IOException {
    ImageReader reader = getImageReader(iis);
    if (reader == null) {
        iis.close();
        return null;
    }
    reader.setInput(iis, true, true);
    ImageReadParam param = reader.getDefaultReadParam();
    String format_name = reader.getFormatName();
    BufferedImage bi;
    try {
        bi = reader.read(0, param);
    } finally {
        reader.dispose();
        iis.close();
    }
    return new BufferedImageHolder(bi, format_name);
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactory.java

private static BufferedImage readJpeg(Object fileOrStream) throws IOException {
    Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("JPEG");
    ImageReader reader = null;
    while (readers.hasNext()) {
        reader = readers.next();/*from w ww. ja  v  a 2  s . co  m*/
        if (reader.canReadRaster()) {
            break;
        }
    }
    requireIOCondition(nonNull(reader), "Cannot find an ImageIO reader for JPEG image");

    try (ImageInputStream iis = ImageIO.createImageInputStream(fileOrStream)) {
        reader.setInput(iis);
        ImageIO.setUseCache(false);
        return reader.read(0);
    } finally {
        reader.dispose();
    }
}

From source file:org.gmdev.pdftrick.utils.CustomExtraImgReader.java

/**
 * Read a JPG image with CMYK ICC profile
 * @param imageByteArray/*from w  w  w .  j  a  v  a 2  s.c  om*/
 * @return The BufferedImage obj
 * @throws IOException
 * @throws ImageReadException
 */
public static BufferedImage readCMYK_JPG(byte[] imageByteArray) throws IOException, ImageReadException {

    colorType = COLOR_TYPE_RGB;
    hasAdobeMarker = false;

    InputStream in = new ByteArrayInputStream(imageByteArray);
    ImageInputStream stream = ImageIO.createImageInputStream(in);
    Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);

    ImageReader reader = iter.next();
    reader.setInput(stream);

    BufferedImage image = null;
    ICC_Profile profile = null;

    colorType = COLOR_TYPE_CMYK;
    checkAdobeMarker(imageByteArray);
    profile = Imaging.getICCProfile(imageByteArray);

    WritableRaster raster = (WritableRaster) reader.readRaster(0, null);

    if (colorType == COLOR_TYPE_YCCK) {
        convertYcckToCmyk(raster);
    }
    if (hasAdobeMarker) {
        //convertInvertedColors(raster);
    }
    image = convertCmykToRgb(raster, profile);

    in.close();
    reader.dispose();
    return image;
}

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

/**
 * Retrieve image dimensions.  This method simply reads headers so it should perform
 * relatively fast.//  w  ww .ja v a  2s.  c o m
 */
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;
}

From source file:com.moviejukebox.scanner.artwork.PosterScanner.java

/**
 * Read an URL and get the dimensions of the image using a specific image
 * type//from   www.j  a v a2s.  c o m
 *
 * @param imageUrl
 * @param imageType
 * @return
 */
public static Dimension getUrlDimensions(String imageUrl, String imageType) {
    Dimension imageDimension = new Dimension(0, 0);

    @SuppressWarnings("rawtypes")
    Iterator readers = ImageIO.getImageReadersBySuffix(imageType);

    if (readers.hasNext()) {
        ImageReader reader = (ImageReader) readers.next();

        try (InputStream in = new URL(imageUrl).openStream();
                ImageInputStream iis = ImageIO.createImageInputStream(in)) {
            reader.setInput(iis, Boolean.TRUE);
            imageDimension.setSize(reader.getWidth(0), reader.getHeight(0));
        } catch (IOException ex) {
            LOG.debug("getUrlDimensions error: {}: can't open url: {}", ex.getMessage(), imageUrl);
        } finally {
            reader.dispose();
        }
    }

    return imageDimension;
}

From source file:org.deegree.tile.persistence.geotiff.ImageReaderFactory.java

@Override
public void destroyObject(Object o) throws Exception {
    ImageReader reader = (ImageReader) o;
    reader.dispose();
}

From source file:nova.core.wrapper.mc.forge.v18.wrapper.render.BWClientRenderManager.java

@SideOnly(Side.CLIENT)
@Override/*from  w  w  w .j a  va 2 s.  c  om*/
public Vector2D getDimension(Texture texture) {
    ResourceLocation loc = toResourceLocation(texture);

    try {
        ImageInputStream in = ImageIO.createImageInputStream(
                Minecraft.getMinecraft().getResourceManager().getResource(loc).getInputStream());
        Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            try {
                reader.setInput(in);
                return new Vector2D(reader.getWidth(0), reader.getHeight(0));
            } finally {
                reader.dispose();
            }
        }
    } catch (Exception e) {
        throw new RenderException("Couldn't load texture " + texture.getPath(), e);
    }
    return new Vector2D(16, 16);
}

From source file:nova.core.wrapper.mc.forge.v17.wrapper.render.BWClientRenderManager.java

@SideOnly(Side.CLIENT)
@Override/* w  ww. java 2  s .  com*/
public Vector2D getDimension(Texture texture) {
    ResourceLocation loc = toResourceLocation(texture);

    try {
        ImageInputStream in = ImageIO.createImageInputStream(
                Minecraft.getMinecraft().getResourceManager().getResource(loc).getInputStream());
        Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            try {
                reader.setInput(in);
                return new Vector2D(reader.getWidth(0), reader.getHeight(0));
            } finally {
                reader.dispose();
            }
        }
    } catch (Exception e) {
        throw new RenderException("Couldn't load texture " + texture.getPath(), e);
    }
    return super.getDimension(texture);
}

From source file:com.moviejukebox.scanner.artwork.PosterScanner.java

/**
 * Return the dimensions of a local image file
 *
 * @param imageFile//from   w  w  w. ja v a  2  s.  c o m
 * @return Dimension
 */
public static Dimension getFileImageSize(File imageFile) {
    Dimension imageSize = new Dimension(0, 0);

    ImageReader reader = null;

    try (ImageInputStream in = ImageIO.createImageInputStream(imageFile)) {
        @SuppressWarnings("rawtypes")
        Iterator readers = ImageIO.getImageReaders(in);
        if (readers.hasNext()) {
            reader = (ImageReader) readers.next();
            if (reader != null) {
                reader.setInput(in);
                return new Dimension(reader.getWidth(0), reader.getHeight(0));
            }
        }
    } catch (IOException | CMMException ex) {
        LOG.error("Failed to read image dimensions for {}", imageFile.getName());
        LOG.error("Error: {}", ex.getMessage());
        return imageSize;
    } finally {
        if (reader != null) {
            reader.dispose();
        }
    }

    return imageSize;
}

From source file:org.sleuthkit.autopsy.coreutils.ImageUtils.java

/**
 * Private template method designed to be used as the implementation of
 * public methods that pull particular (usually meta-)data out of a image
 * file.//from  w w w  .ja  v a  2 s.  c  om
 *
 * @param file              the file to extract the data from
 * @param errorTemplate     a message template used to log errors. Should
 *                          take one parameter: the file's unique path or
 *                          name.
 * @param propertyExtractor an implementation of {@link PropertyExtractor}
 *                          used to retrieve the specific property.
 *
 * @return the the value of the property extracted by the given
 *         propertyExtractor
 *
 * @throws IOException if there was a problem reading the property from the
 *                     file.
 *
 * @see PropertyExtractor
 * @see #getImageHeight(org.sleuthkit.datamodel.AbstractFile)
 */
private static <T> T getImageProperty(AbstractFile file, final String errorTemplate,
        PropertyExtractor<T> propertyExtractor) throws IOException {
    try (InputStream inputStream = new BufferedInputStream(new ReadContentInputStream(file));) {
        try (ImageInputStream input = ImageIO.createImageInputStream(inputStream)) {
            if (input == null) {
                IIOException iioException = new IIOException("Could not create ImageInputStream.");
                LOGGER.log(Level.WARNING, errorTemplate + iioException.toString(), getContentPathSafe(file));
                throw iioException;
            }
            Iterator<ImageReader> readers = ImageIO.getImageReaders(input);

            if (readers.hasNext()) {
                ImageReader reader = readers.next();
                reader.setInput(input);
                try {

                    return propertyExtractor.extract(reader);
                } catch (IOException ex) {
                    LOGGER.log(Level.WARNING, errorTemplate + ex.toString(), getContentPathSafe(file));
                    throw ex;
                } finally {
                    reader.dispose();
                }
            } else {
                IIOException iioException = new IIOException("No ImageReader found.");
                LOGGER.log(Level.WARNING, errorTemplate + iioException.toString(), getContentPathSafe(file));

                throw iioException;
            }
        }
    }
}