Search in sources :

Example 36 with ImageInputStream

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

the class ImageIO method read.

// All-in-one methods
/**
     * Returns a <code>BufferedImage</code> as the result of decoding
     * a supplied <code>File</code> with an <code>ImageReader</code>
     * chosen automatically from among those currently registered.
     * The <code>File</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> Note that there is no <code>read</code> method that takes a
     * filename as a <code>String</code>; use this method instead after
     * creating a <code>File</code> from the filename.
     *
     * <p> This method does not attempt to locate
     * <code>ImageReader</code>s that can read directly from a
     * <code>File</code>; that may be accomplished using
     * <code>IIORegistry</code> and <code>ImageReaderSpi</code>.
     *
     * @param input a <code>File</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(File input) throws IOException {
    if (input == null) {
        throw new IllegalArgumentException("input == null!");
    }
    if (!input.canRead()) {
        throw new IIOException("Can't read input file!");
    }
    ImageInputStream stream = createImageInputStream(input);
    if (stream == null) {
        throw new IIOException("Can't create an ImageInputStream!");
    }
    BufferedImage bi = read(stream);
    if (bi == null) {
        stream.close();
    }
    return bi;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) BufferedImage(java.awt.image.BufferedImage)

Example 37 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project jersey by jersey.

the class RenderedImageProvider method readFrom.

@Override
public RenderedImage readFrom(Class<RenderedImage> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
    if (IMAGE_MEDIA_TYPE.isCompatible(mediaType)) {
        Iterator<ImageReader> readers = ImageIO.getImageReadersByMIMEType(mediaType.toString());
        if (!readers.hasNext()) {
            throw new IOException("The image-based media type " + mediaType + "is not supported for reading");
        }
        ImageReader reader = readers.next();
        ImageInputStream in = ImageIO.createImageInputStream(entityStream);
        reader.setInput(in, true, true);
        BufferedImage bi = reader.read(0, reader.getDefaultReadParam());
        in.close();
        reader.dispose();
        return bi;
    } else {
        return ImageIO.read(entityStream);
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage)

Example 38 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project screenbird by adamhub.

the class Frame method getJPEGBounds.

/** @return the bounds of the image provided.
     * @throws UnsupportedOperationException if the file cannot be read
     * as an image by ImageIO classes.
     * @throws IOException if an error occurred while reading the file
     */
private static Dimension getJPEGBounds(File file) throws IOException, IIOException {
    FileInputStream in = null;
    try {
        //Hack for UNIX machines
        if (MediaUtil.osIsUnix())
            ImageIO.setUseCache(false);
        in = new FileInputStream(file);
        ImageIO.setCacheDirectory(new File(Settings.SCREEN_CAPTURE_DIR));
        ImageInputStream stream = ImageIO.createImageInputStream(in);
        Iterator iter = ImageIO.getImageReaders(stream);
        ImageReader reader = (ImageReader) iter.next();
        String formatName = reader.getFormatName().toLowerCase();
        if (formatName.indexOf("jpeg") == -1 && formatName.indexOf("jpg") == -1) {
            throw new IllegalArgumentException("This image file is not a JPEG image.  This encoder only supports JPEG images.");
        }
        if (reader == null) {
            throw new UnsupportedOperationException("The file \"" + file.getName() + "\" is not a supported image.");
        }
        reader.setInput(stream, false);
        int w = reader.getWidth(0);
        int h = reader.getHeight(0);
        reader.dispose();
        stream.close();
        return new Dimension(w, h);
    } catch (IllegalArgumentException e) {
        log(e);
    } catch (UnsupportedOperationException e) {
        log(e);
    } finally {
        try {
            if (in != null) {
                in.close();
            }
        } catch (Exception e) {
            log(e);
        }
    }
    return null;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) Iterator(java.util.Iterator) Dimension(java.awt.Dimension) ImageReader(javax.imageio.ImageReader) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) IIOException(javax.imageio.IIOException) IOException(java.io.IOException)

Example 39 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project intellij-community by JetBrains.

the class IfsUtil method refresh.

/**
   * Load image data for file and put user data attributes into file.
   *
   * @param file File
   * @return true if file image is loaded.
   * @throws java.io.IOException if image can not be loaded
   */
private static boolean refresh(@NotNull VirtualFile file) throws IOException {
    Long loadedTimeStamp = file.getUserData(TIMESTAMP_KEY);
    SoftReference<BufferedImage> imageRef = file.getUserData(BUFFERED_IMAGE_REF_KEY);
    if (loadedTimeStamp == null || loadedTimeStamp.longValue() != file.getTimeStamp() || SoftReference.dereference(imageRef) == null) {
        try {
            final byte[] content = file.contentsToByteArray();
            if (ICO_FORMAT.equalsIgnoreCase(file.getExtension())) {
                try {
                    final BufferedImage image = ICO_IMAGE_PARSER.getBufferedImage(new ByteSourceArray(content), null);
                    file.putUserData(FORMAT_KEY, ICO_FORMAT);
                    file.putUserData(BUFFERED_IMAGE_REF_KEY, new SoftReference<>(image));
                    return true;
                } catch (ImageReadException ignore) {
                }
            }
            InputStream inputStream = new ByteArrayInputStream(content, 0, content.length);
            ImageInputStream imageInputStream = ImageIO.createImageInputStream(inputStream);
            try {
                Iterator<ImageReader> imageReaders = ImageIO.getImageReaders(imageInputStream);
                if (imageReaders.hasNext()) {
                    ImageReader imageReader = imageReaders.next();
                    try {
                        file.putUserData(FORMAT_KEY, imageReader.getFormatName());
                        ImageReadParam param = imageReader.getDefaultReadParam();
                        imageReader.setInput(imageInputStream, true, true);
                        int minIndex = imageReader.getMinIndex();
                        BufferedImage image = imageReader.read(minIndex, param);
                        file.putUserData(BUFFERED_IMAGE_REF_KEY, new SoftReference<>(image));
                        return true;
                    } finally {
                        imageReader.dispose();
                    }
                }
            } finally {
                imageInputStream.close();
            }
        } finally {
            // We perform loading no more needed
            file.putUserData(TIMESTAMP_KEY, file.getTimeStamp());
        }
    }
    return false;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteSourceArray(org.apache.sanselan.common.byteSources.ByteSourceArray) ImageInputStream(javax.imageio.stream.ImageInputStream) BufferedImage(java.awt.image.BufferedImage) ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageReadException(org.apache.sanselan.ImageReadException) ImageReader(javax.imageio.ImageReader)

Example 40 with ImageInputStream

use of javax.imageio.stream.ImageInputStream in project spring-framework by spring-projects.

the class BufferedImageHttpMessageConverter method read.

@Override
public BufferedImage read(Class<? extends BufferedImage> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    ImageInputStream imageInputStream = null;
    ImageReader imageReader = null;
    try {
        imageInputStream = createImageInputStream(inputMessage.getBody());
        MediaType contentType = inputMessage.getHeaders().getContentType();
        Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
        if (imageReaders.hasNext()) {
            imageReader = imageReaders.next();
            ImageReadParam irp = imageReader.getDefaultReadParam();
            process(irp);
            imageReader.setInput(imageInputStream, true);
            return imageReader.read(0, irp);
        } else {
            throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
        }
    } finally {
        if (imageReader != null) {
            imageReader.dispose();
        }
        if (imageInputStream != null) {
            try {
                imageInputStream.close();
            } catch (IOException ex) {
            // ignore
            }
        }
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) FileCacheImageInputStream(javax.imageio.stream.FileCacheImageInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader)

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