Search in sources :

Example 21 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project Botnak by Gocnak.

the class FaceManager method sanityCheck.

/**
     * Tests to see if an image is within reasonable downloading bounds (5000x5000)
     *
     * @param url The URL to the image to check.
     * @return True if within downloadable bounds else false.
     */
private static boolean sanityCheck(URL url) {
    try (ImageInputStream in = ImageIO.createImageInputStream(url.openStream())) {
        final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
        if (readers.hasNext()) {
            ImageReader reader = readers.next();
            try {
                reader.setInput(in);
                Dimension d = new Dimension(reader.getWidth(0), reader.getHeight(0));
                return d.getHeight() < 5000 && d.getWidth() < 5000;
            } finally {
                reader.dispose();
            }
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ImageReader(javax.imageio.ImageReader)

Example 22 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project poi by apache.

the class ImageUtils method getImageDimension.

/**
     * Return the dimension of this image
     *
     * @param is the stream containing the image data
     * @param type type of the picture: {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_JPEG},
     * {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_PNG} or {@link org.apache.poi.ss.usermodel.Workbook#PICTURE_TYPE_DIB}
     *
     * @return image dimension in pixels
     */
public static Dimension getImageDimension(InputStream is, int type) {
    Dimension size = new Dimension();
    switch(type) {
        //other formats like WMF, EMF and PICT are not supported in Java
        case Workbook.PICTURE_TYPE_JPEG:
        case Workbook.PICTURE_TYPE_PNG:
        case Workbook.PICTURE_TYPE_DIB:
            try {
                //read the image using javax.imageio.*
                ImageInputStream iis = ImageIO.createImageInputStream(is);
                try {
                    Iterator<ImageReader> i = ImageIO.getImageReaders(iis);
                    ImageReader r = i.next();
                    try {
                        r.setInput(iis);
                        BufferedImage img = r.read(0);
                        int[] dpi = getResolution(r);
                        //since cannot divide by zero
                        if (dpi[0] == 0)
                            dpi[0] = PIXEL_DPI;
                        if (dpi[1] == 0)
                            dpi[1] = PIXEL_DPI;
                        size.width = img.getWidth() * PIXEL_DPI / dpi[0];
                        size.height = img.getHeight() * PIXEL_DPI / dpi[1];
                    } finally {
                        r.dispose();
                    }
                } finally {
                    iis.close();
                }
            } catch (IOException e) {
                //silently return if ImageIO failed to read the image
                logger.log(POILogger.WARN, e);
            }
            break;
        default:
            logger.log(POILogger.WARN, "Only JPEG, PNG and DIB pictures can be automatically sized");
    }
    return size;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) Dimension(java.awt.Dimension) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage)

Example 23 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project android by JetBrains.

the class ConvertToWebpAction method isEligibleForConversion.

public static boolean isEligibleForConversion(@Nullable VirtualFile file, @Nullable WebpConversionSettings settings) {
    if (file != null && !file.isDirectory()) {
        String name = file.getName();
        if (name.endsWith(DOT_PNG)) {
            if (settings != null && settings.skipNinePatches && endsWithIgnoreCase(name, DOT_9PNG)) {
                return false;
            }
            if (settings != null && settings.skipTransparentImages) {
                try {
                    BufferedImage image = ImageIO.read(file.getInputStream());
                    if (image != null && ImageUtils.isNonOpaque(image)) {
                        return false;
                    }
                } catch (IOException ignore) {
                }
            }
            return true;
        }
        if (endsWithIgnoreCase(name, DOT_JPG) || SdkUtils.endsWith(name, DOT_JPEG)) {
            return true;
        }
        if (endsWithIgnoreCase(name, DOT_BMP)) {
            // Really?
            return true;
        }
        if (endsWithIgnoreCase(name, DOT_GIF)) {
            // Can convert only if not an animated gif (TODO: Support animated webp!)
            ImageReader is = ImageIO.getImageReadersBySuffix("GIF").next();
            ImageInputStream iis;
            try {
                iis = ImageIO.createImageInputStream(file.getInputStream());
                is.setInput(iis);
                return is.getNumImages(true) == 1;
            } catch (IOException ignore) {
                return false;
            }
        }
    }
    return false;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage)

Example 24 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project android by JetBrains.

the class WebpImageReaderSpi method canDecodeInput.

@Override
public boolean canDecodeInput(@NotNull Object source) throws IOException {
    assert source instanceof ImageInputStream;
    ImageInputStream stream = (ImageInputStream) source;
    long length = stream.length();
    // Accept them for now and if needed, throw an IOException later.
    if (length > MAX_FILE_SIZE) {
        return false;
    }
    stream.mark();
    try {
        byte[] header = new byte[12];
        int bytesRead = stream.read(header, 0, 12);
        return bytesRead == 12 && arrayEquals(header, 0, RIFF_HEADER.length, RIFF_HEADER) && arrayEquals(header, 8, WEBP_HEADER.length, WEBP_HEADER) && WebpNativeLibHelper.loadNativeLibraryIfNeeded();
    } finally {
        try {
            stream.reset();
        } catch (IOException e) {
            Logger.getInstance(WebpImageReaderSpi.class).error(e);
        }
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException)

Example 25 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project jdk8u_jdk by JetBrains.

the class ImageReader method setInput.

/**
     * Sets the input source to use to the given
     * <code>ImageInputStream</code> or other <code>Object</code>.
     * The input source must be set before any of the query or read
     * methods are used.  If <code>input</code> is <code>null</code>,
     * any currently set input source will be removed.  In any case,
     * the value of <code>minIndex</code> will be initialized to 0.
     *
     * <p> The <code>seekForwardOnly</code> parameter controls whether
     * the value returned by <code>getMinIndex</code> will be
     * increased as each image (or thumbnail, or image metadata) is
     * read.  If <code>seekForwardOnly</code> is true, then a call to
     * <code>read(index)</code> will throw an
     * <code>IndexOutOfBoundsException</code> if {@code index < this.minIndex};
     * otherwise, the value of
     * <code>minIndex</code> will be set to <code>index</code>.  If
     * <code>seekForwardOnly</code> is <code>false</code>, the value of
     * <code>minIndex</code> will remain 0 regardless of any read
     * operations.
     *
     * <p> The <code>ignoreMetadata</code> parameter, if set to
     * <code>true</code>, allows the reader to disregard any metadata
     * encountered during the read.  Subsequent calls to the
     * <code>getStreamMetadata</code> and
     * <code>getImageMetadata</code> methods may return
     * <code>null</code>, and an <code>IIOImage</code> returned from
     * <code>readAll</code> may return <code>null</code> from their
     * <code>getMetadata</code> method.  Setting this parameter may
     * allow the reader to work more efficiently.  The reader may
     * choose to disregard this setting and return metadata normally.
     *
     * <p> Subclasses should take care to remove any cached
     * information based on the previous stream, such as header
     * information or partially decoded image data.
     *
     * <p> Use of a general <code>Object</code> other than an
     * <code>ImageInputStream</code> is intended for readers that
     * interact directly with a capture device or imaging protocol.
     * The set of legal classes is advertised by the reader's service
     * provider's <code>getInputTypes</code> method; most readers
     * will return a single-element array containing only
     * <code>ImageInputStream.class</code> to indicate that they
     * accept only an <code>ImageInputStream</code>.
     *
     * <p> The default implementation checks the <code>input</code>
     * argument against the list returned by
     * <code>originatingProvider.getInputTypes()</code> and fails
     * if the argument is not an instance of one of the classes
     * in the list.  If the originating provider is set to
     * <code>null</code>, the input is accepted only if it is an
     * <code>ImageInputStream</code>.
     *
     * @param input the <code>ImageInputStream</code> or other
     * <code>Object</code> to use for future decoding.
     * @param seekForwardOnly if <code>true</code>, images and metadata
     * may only be read in ascending order from this input source.
     * @param ignoreMetadata if <code>true</code>, metadata
     * may be ignored during reads.
     *
     * @exception IllegalArgumentException if <code>input</code> is
     * not an instance of one of the classes returned by the
     * originating service provider's <code>getInputTypes</code>
     * method, or is not an <code>ImageInputStream</code>.
     *
     * @see ImageInputStream
     * @see #getInput
     * @see javax.imageio.spi.ImageReaderSpi#getInputTypes
     */
public void setInput(Object input, boolean seekForwardOnly, boolean ignoreMetadata) {
    if (input != null) {
        boolean found = false;
        if (originatingProvider != null) {
            Class[] classes = originatingProvider.getInputTypes();
            for (int i = 0; i < classes.length; i++) {
                if (classes[i].isInstance(input)) {
                    found = true;
                    break;
                }
            }
        } else {
            if (input instanceof ImageInputStream) {
                found = true;
            }
        }
        if (!found) {
            throw new IllegalArgumentException("Incorrect input type!");
        }
        this.seekForwardOnly = seekForwardOnly;
        this.ignoreMetadata = ignoreMetadata;
        this.minIndex = 0;
    }
    this.input = input;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) Point(java.awt.Point)

Aggregations

ImageInputStream (javax.imageio.stream.ImageInputStream)60 ImageReader (javax.imageio.ImageReader)32 BufferedImage (java.awt.image.BufferedImage)23 IOException (java.io.IOException)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 ImageOutputStream (javax.imageio.stream.ImageOutputStream)9 File (java.io.File)8 InputStream (java.io.InputStream)7 IIOImage (javax.imageio.IIOImage)7 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)7 ImageReadParam (javax.imageio.ImageReadParam)6 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)6 ImageWriter (javax.imageio.ImageWriter)6 ImageWriteParam (javax.imageio.ImageWriteParam)5 IIOMetadata (javax.imageio.metadata.IIOMetadata)5 Iterator (java.util.Iterator)4 IIOException (javax.imageio.IIOException)4 Dimension (java.awt.Dimension)2 Graphics (java.awt.Graphics)2