Search in sources :

Example 31 with ImageReader

use of javax.imageio.ImageReader 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 32 with ImageReader

use of javax.imageio.ImageReader 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 33 with ImageReader

use of javax.imageio.ImageReader 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 34 with ImageReader

use of javax.imageio.ImageReader 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)

Example 35 with ImageReader

use of javax.imageio.ImageReader in project d54 by mitrisdev.

the class GNU30Plugin method loadGifObject.

public ArrayList<BufferedImage> loadGifObject(InputStream is) throws IOException {
    ImageInputStream image = ImageIO.createImageInputStream(is);
    ImageReader ir = (ImageReader) ImageIO.getImageReadersBySuffix("gif").next();
    ir.setInput(image, false);
    int frames = ir.getNumImages(true);
    ArrayList<BufferedImage> br = new ArrayList<BufferedImage>();
    for (int i = 0; i < frames; ++i) {
        BufferedImage frame = ir.read(i);
        br.add(frame);
    }
    return br;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ArrayList(java.util.ArrayList) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage)

Aggregations

ImageReader (javax.imageio.ImageReader)59 BufferedImage (java.awt.image.BufferedImage)32 ImageInputStream (javax.imageio.stream.ImageInputStream)32 IOException (java.io.IOException)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 ImageReadParam (javax.imageio.ImageReadParam)14 ImageWriter (javax.imageio.ImageWriter)13 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)12 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)11 InputStream (java.io.InputStream)10 Iterator (java.util.Iterator)10 IIOMetadata (javax.imageio.metadata.IIOMetadata)10 IIOImage (javax.imageio.IIOImage)9 ImageOutputStream (javax.imageio.stream.ImageOutputStream)7 File (java.io.File)6 ImageWriteParam (javax.imageio.ImageWriteParam)5 ImageReaderSpi (javax.imageio.spi.ImageReaderSpi)4 Graphics (java.awt.Graphics)3 Graphics2D (java.awt.Graphics2D)3