Example usage for javax.imageio ImageReader getMinIndex

List of usage examples for javax.imageio ImageReader getMinIndex

Introduction

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

Prototype

public int getMinIndex() 

Source Link

Document

Returns the lowest valid index for reading an image, thumbnail, or image metadata.

Usage

From source file:org.dita.dost.writer.ImageMetadataFilter.java

private Attributes readMetadata(final URI imgInput) {
    logger.info("Reading " + imgInput);
    final XMLUtils.AttributesBuilder a = new XMLUtils.AttributesBuilder();
    try {//from   w w w . ja v a2s  .c  o  m
        InputStream in = null;
        ImageReader r = null;
        ImageInputStream iis = null;
        try {
            in = getInputStream(imgInput);
            iis = ImageIO.createImageInputStream(in);
            final Iterator<ImageReader> i = ImageIO.getImageReaders(iis);
            if (!i.hasNext()) {
                logger.info("Image " + imgInput + " format not supported");
            } else {
                r = i.next();
                r.setInput(iis);
                final int imageIndex = r.getMinIndex();
                a.add(DITA_OT_NS, ATTR_IMAGE_WIDTH, DITA_OT_PREFIX + ":" + ATTR_IMAGE_WIDTH, "CDATA",
                        Integer.toString(r.getWidth(imageIndex)));
                a.add(DITA_OT_NS, ATTR_IMAGE_HEIGHT, DITA_OT_PREFIX + ":" + ATTR_IMAGE_HEIGHT, "CDATA",
                        Integer.toString(r.getHeight(imageIndex)));
                final Element node = (Element) r.getImageMetadata(0).getAsTree("javax_imageio_1.0");
                final NodeList hs = node.getElementsByTagName("HorizontalPixelSize");
                if (hs != null && hs.getLength() == 1) {
                    final float v = Float.parseFloat(((Element) hs.item(0)).getAttribute("value"));
                    final int dpi = Math.round(MM_TO_INCH / v);
                    a.add(DITA_OT_NS, ATTR_HORIZONTAL_DPI, DITA_OT_PREFIX + ":" + ATTR_HORIZONTAL_DPI, "CDATA",
                            Integer.toString(dpi));
                }
                final NodeList vs = node.getElementsByTagName("VerticalPixelSize");
                if (vs != null && vs.getLength() == 1) {
                    final float v = Float.parseFloat(((Element) vs.item(0)).getAttribute("value"));
                    final int dpi = Math.round(MM_TO_INCH / v);
                    a.add(DITA_OT_NS, ATTR_VERTICAL_DPI, DITA_OT_PREFIX + ":" + ATTR_VERTICAL_DPI, "CDATA",
                            Integer.toString(dpi));
                }
            }
        } finally {
            if (r != null) {
                r.dispose();
            }
            if (iis != null) {
                iis.close();
            }
            if (in != null) {
                in.close();
            }
        }
    } catch (final Exception e) {
        logger.error("Failed to read image " + imgInput + " metadata: " + e.getMessage(), e);
    }
    return a.build();
}

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

private Size getImageSize(VFSLeaf media, String suffix) {
    Size result = null;//from w  w w .  j av a2 s .co m
    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.olat.core.commons.services.image.spi.ImageHelperImpl.java

private static SizeAndBufferedImage calcScaledSize(ImageInputStream stream, String suffix, int maxWidth,
        int maxHeight, boolean fill) {
    Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
    if (iter.hasNext()) {
        ImageReader reader = iter.next();
        try {/*from   w  w w  .  jav a2s  .c  o m*/
            reader.setInput(stream, true, true);
            int width = reader.getWidth(reader.getMinIndex());
            int height = reader.getHeight(reader.getMinIndex());
            Size size = new Size(width, height, false);
            Size scaledSize = computeScaledSize(width, height, maxWidth, maxHeight, fill);
            SizeAndBufferedImage all = new SizeAndBufferedImage(size, scaledSize);

            int readerMinIndex = reader.getMinIndex();
            ImageReadParam param = reader.getDefaultReadParam();
            Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
            while (imageTypes.hasNext()) {
                try {
                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                    int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
                    if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
                        param.setDestinationType(imageTypeSpecifier);
                    }

                    double memoryKB = (width * height * 4) / 1024d;
                    if (memoryKB > 2000) {// check limit at 20MB
                        double free = Runtime.getRuntime().freeMemory() / 1024d;
                        if (free > memoryKB) {
                            all.setImage(reader.read(readerMinIndex, param));
                        } else {
                            // make sub sampling to save memory
                            int ratio = (int) Math.round(Math.sqrt(memoryKB / free));
                            param.setSourceSubsampling(ratio, ratio, 0, 0);
                            all.setImage(reader.read(readerMinIndex, param));
                        }
                    } else {
                        all.setImage(reader.read(readerMinIndex, param));
                    }
                    return all;
                } catch (IllegalArgumentException e) {
                    log.warn(e.getMessage(), e);
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            reader.dispose();
        }
    } else {
        log.error("No reader found for given format: " + suffix, null);
    }
    return null;
}

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

private static SizeAndBufferedImage getImage(ImageInputStream stream, String suffix) {
    Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix);
    if (iter.hasNext()) {
        ImageReader reader = iter.next();
        try {/*from ww  w.ja  v  a 2s  .  c o m*/
            reader.setInput(stream, true, true);
            int width = reader.getWidth(reader.getMinIndex());
            int height = reader.getHeight(reader.getMinIndex());
            Size size = new Size(width, height, false);
            SizeAndBufferedImage all = new SizeAndBufferedImage(size, null);

            int readerMinIndex = reader.getMinIndex();
            ImageReadParam param = reader.getDefaultReadParam();
            Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
            while (imageTypes.hasNext()) {
                try {
                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                    int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
                    if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
                        param.setDestinationType(imageTypeSpecifier);
                    }
                    all.setImage(reader.read(readerMinIndex, param));
                    return all;
                } catch (IllegalArgumentException e) {
                    log.warn(e.getMessage(), e);
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } finally {
            reader.dispose();
        }
    } else {
        log.error("No reader found for given format: " + suffix, null);
    }
    return null;
}

From source file:org.onehippo.forge.gallerymagick.core.command.ScalrProcessorUtils.java

/**
 * Detects the {@code sourceFile} and returns the size dimension from it.
 * @param sourceFile source image file//from  w  w w. j  a  va2 s.c o  m
 * @return Detects the {@code sourceFile} and returns the size dimension from it
 * @throws IOException if IO exception occurs
 */
public static ImageDimension identifyDimension(File sourceFile) throws IOException {
    ImageDimension dimension = null;
    String extension = FilenameUtils.getExtension(sourceFile.getName());
    Iterator<ImageReader> it = ImageIO.getImageReadersBySuffix(extension);

    if (!it.hasNext()) {
        throw new IllegalArgumentException("Unsupported file name extension: " + sourceFile);
    }

    ImageReader reader = null;

    try {
        reader = getImageReader(sourceFile);
        int width = reader.getWidth(reader.getMinIndex());
        int height = reader.getHeight(reader.getMinIndex());
        dimension = ImageDimension.from(width, height);
    } finally {
        if (reader != null) {
            reader.dispose();
        }
    }

    return dimension;
}