Search in sources :

Example 26 with ImageInputStream

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

the class ImageIO method read.

/**
     * Returns a <code>BufferedImage</code> as the result of decoding
     * a supplied <code>URL</code> with an <code>ImageReader</code>
     * chosen automatically from among those currently registered.  An
     * <code>InputStream</code> is obtained from the <code>URL</code>,
     * which is wrapped in an <code>ImageInputStream</code>.  If no
     * registered <code>ImageReader</code> claims to be able to read
     * the resulting stream, <code>null</code> is returned.
     *
     * <p> The current cache settings from <code>getUseCache</code>and
     * <code>getCacheDirectory</code> will be used to control caching in the
     * <code>ImageInputStream</code> that is created.
     *
     * <p> This method does not attempt to locate
     * <code>ImageReader</code>s that can read directly from a
     * <code>URL</code>; that may be accomplished using
     * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
     *
     * @param input a <code>URL</code> to read from.
     *
     * @return a <code>BufferedImage</code> containing the decoded
     * contents of the input, or <code>null</code>.
     *
     * @exception IllegalArgumentException if <code>input</code> is
     * <code>null</code>.
     * @exception IOException if an error occurs during reading.
     */
public static BufferedImage read(URL input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }
    InputStream istream = null;
    try {
        istream = input.openStream();
    } catch (IOException e) {
        throw new IIOException("Can't get input stream from URL!", e);
    }
    ImageInputStream stream = createImageInputStream(istream);
    BufferedImage bi;
    try {
        bi = read(stream);
        if (bi == null) {
            stream.close();
        }
    } finally {
        istream.close();
    }
    return bi;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 27 with ImageInputStream

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

the class ImageIO method read.

/**
     * Returns a <code>BufferedImage</code> as the result of decoding
     * a supplied <code>InputStream</code> with an <code>ImageReader</code>
     * chosen automatically from among those currently registered.
     * The <code>InputStream</code> is wrapped in an
     * <code>ImageInputStream</code>.  If no registered
     * <code>ImageReader</code> claims to be able to read the
     * resulting stream, <code>null</code> is returned.
     *
     * <p> The current cache settings from <code>getUseCache</code>and
     * <code>getCacheDirectory</code> will be used to control caching in the
     * <code>ImageInputStream</code> that is created.
     *
     * <p> This method does not attempt to locate
     * <code>ImageReader</code>s that can read directly from an
     * <code>InputStream</code>; that may be accomplished using
     * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
     *
     * <p> This method <em>does not</em> close the provided
     * <code>InputStream</code> after the read operation has completed;
     * it is the responsibility of the caller to close the stream, if desired.
     *
     * @param input an <code>InputStream</code> to read from.
     *
     * @return a <code>BufferedImage</code> containing the decoded
     * contents of the input, or <code>null</code>.
     *
     * @exception IllegalArgumentException if <code>input</code> is
     * <code>null</code>.
     * @exception IOException if an error occurs during reading.
     */
public static BufferedImage read(InputStream input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }
    ImageInputStream stream = createImageInputStream(input);
    BufferedImage bi = read(stream);
    if (bi == null) {
        stream.close();
    }
    return bi;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) BufferedImage(java.awt.image.BufferedImage)

Example 28 with ImageInputStream

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

the class DataTransferer method standardImageBytesToImage.

/**
     * Translates either a byte array or an input stream which contain
     * an image data in the given standard format into an Image.
     *
     * @param mimeType image MIME type, such as: image/png, image/jpeg, image/gif
     */
protected Image standardImageBytesToImage(byte[] bytes, String mimeType) throws IOException {
    Iterator readerIterator = ImageIO.getImageReadersByMIMEType(mimeType);
    if (!readerIterator.hasNext()) {
        throw new IOException("No registered service provider can decode " + " an image from " + mimeType);
    }
    IOException ioe = null;
    while (readerIterator.hasNext()) {
        ImageReader imageReader = (ImageReader) readerIterator.next();
        try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes)) {
            ImageInputStream imageInputStream = ImageIO.createImageInputStream(bais);
            try {
                ImageReadParam param = imageReader.getDefaultReadParam();
                imageReader.setInput(imageInputStream, true, true);
                BufferedImage bufferedImage = imageReader.read(imageReader.getMinIndex(), param);
                if (bufferedImage != null) {
                    return bufferedImage;
                }
            } finally {
                imageInputStream.close();
                imageReader.dispose();
            }
        } catch (IOException e) {
            ioe = e;
            continue;
        }
    }
    if (ioe == null) {
        ioe = new IOException("Registered service providers failed to decode" + " an image from " + mimeType);
    }
    throw ioe;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream)

Example 29 with ImageInputStream

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

the class GIFImageReaderSpi method canDecodeInput.

public boolean canDecodeInput(Object input) throws IOException {
    if (!(input instanceof ImageInputStream)) {
        return false;
    }
    ImageInputStream stream = (ImageInputStream) input;
    byte[] b = new byte[6];
    stream.mark();
    stream.readFully(b);
    stream.reset();
    return b[0] == 'G' && b[1] == 'I' && b[2] == 'F' && b[3] == '8' && (b[4] == '7' || b[4] == '9') && b[5] == 'a';
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream)

Example 30 with ImageInputStream

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

the class PNGImageReaderSpi method canDecodeInput.

public boolean canDecodeInput(Object input) throws IOException {
    if (!(input instanceof ImageInputStream)) {
        return false;
    }
    ImageInputStream stream = (ImageInputStream) input;
    byte[] b = new byte[8];
    stream.mark();
    stream.readFully(b);
    stream.reset();
    return (b[0] == (byte) 137 && b[1] == (byte) 80 && b[2] == (byte) 78 && b[3] == (byte) 71 && b[4] == (byte) 13 && b[5] == (byte) 10 && b[6] == (byte) 26 && b[7] == (byte) 10);
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream)

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