Search in sources :

Example 31 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 32 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 33 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project fess by codelibs.

the class HtmlTagBasedGenerator method generate.

@Override
public boolean generate(final String thumbnailId, final String url, final File outputFile) {
    if (logger.isDebugEnabled()) {
        logger.debug("Generate Thumbnail: " + url);
    }
    if (outputFile.exists()) {
        if (logger.isDebugEnabled()) {
            logger.debug("The thumbnail file exists: " + outputFile.getAbsolutePath());
        }
        return true;
    }
    final File parentFile = outputFile.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();
    }
    if (!parentFile.isDirectory()) {
        logger.warn("Not found: " + parentFile.getAbsolutePath());
        return false;
    }
    Curl.get(url).execute(con -> {
        boolean created = false;
        try (ImageInputStream input = ImageIO.createImageInputStream(con.getInputStream())) {
            if (saveImage(input, outputFile)) {
                created = true;
            } else {
                logger.warn("Failed to create a thumbnail for " + url);
            }
        } catch (final Throwable t) {
            if (logger.isDebugEnabled()) {
                logger.warn("Failed to create a thumbnail for " + url, t);
            } else {
                logger.warn("Failed to create a thumbnail for " + url + " " + t.getClass() + ": " + t.getMessage());
            }
        } finally {
            if (!created) {
                updateThumbnailField(thumbnailId, url, StringUtil.EMPTY);
                if (outputFile.exists() && !outputFile.delete()) {
                    logger.warn("Failed to delete " + outputFile.getAbsolutePath());
                }
            }
        }
    });
    return outputFile.exists();
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) File(java.io.File)

Example 34 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project fess by codelibs.

the class HtmlTagBasedGeneratorTest method test_saveImage.

public void test_saveImage() throws Exception {
    HtmlTagBasedGenerator generator = new HtmlTagBasedGenerator();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    File outputFile = File.createTempFile("generator_", ".png");
    String imagePath = "thumbnail/600x400.png";
    try (ImageInputStream input = ImageIO.createImageInputStream(classLoader.getResourceAsStream(imagePath))) {
        generator.saveImage(input, outputFile);
    }
    assertImageSize(outputFile, 100, 66);
    imagePath = "thumbnail/600x400.gif";
    try (ImageInputStream input = ImageIO.createImageInputStream(classLoader.getResourceAsStream(imagePath))) {
        generator.saveImage(input, outputFile);
    }
    assertImageSize(outputFile, 100, 66);
    imagePath = "thumbnail/600x400.jpg";
    try (ImageInputStream input = ImageIO.createImageInputStream(classLoader.getResourceAsStream(imagePath))) {
        generator.saveImage(input, outputFile);
    }
    assertImageSize(outputFile, 100, 66);
    imagePath = "thumbnail/400x400.png";
    try (ImageInputStream input = ImageIO.createImageInputStream(classLoader.getResourceAsStream(imagePath))) {
        generator.saveImage(input, outputFile);
    }
    assertImageSize(outputFile, 100, 100);
    imagePath = "thumbnail/400x400.gif";
    try (ImageInputStream input = ImageIO.createImageInputStream(classLoader.getResourceAsStream(imagePath))) {
        generator.saveImage(input, outputFile);
    }
    assertImageSize(outputFile, 100, 100);
    imagePath = "thumbnail/400x400.jpg";
    try (ImageInputStream input = ImageIO.createImageInputStream(classLoader.getResourceAsStream(imagePath))) {
        generator.saveImage(input, outputFile);
    }
    assertImageSize(outputFile, 100, 100);
    imagePath = "thumbnail/400x600.png";
    try (ImageInputStream input = ImageIO.createImageInputStream(classLoader.getResourceAsStream(imagePath))) {
        generator.saveImage(input, outputFile);
    }
    assertImageSize(outputFile, 100, 100);
    imagePath = "thumbnail/400x600.gif";
    try (ImageInputStream input = ImageIO.createImageInputStream(classLoader.getResourceAsStream(imagePath))) {
        generator.saveImage(input, outputFile);
    }
    assertImageSize(outputFile, 100, 100);
    imagePath = "thumbnail/400x600.jpg";
    try (ImageInputStream input = ImageIO.createImageInputStream(classLoader.getResourceAsStream(imagePath))) {
        generator.saveImage(input, outputFile);
    }
    assertImageSize(outputFile, 100, 100);
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) File(java.io.File)

Example 35 with ImageInputStream

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

the class JPEGImageReaderSpi method canDecodeInput.

public boolean canDecodeInput(Object source) throws IOException {
    if (!(source instanceof ImageInputStream)) {
        return false;
    }
    ImageInputStream iis = (ImageInputStream) source;
    iis.mark();
    // If the first two bytes are a JPEG SOI marker, it's probably
    // a JPEG file.  If they aren't, it definitely isn't a JPEG file.
    int byte1 = iis.read();
    int byte2 = iis.read();
    iis.reset();
    if ((byte1 == 0xFF) && (byte2 == JPEG.SOI)) {
        return true;
    }
    return false;
}
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