Search in sources :

Example 36 with ImageReader

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

the class ImagePreviewComponent method readImageFromBytes.

@NotNull
public static BufferedImage readImageFromBytes(@NotNull byte[] content) throws IOException {
    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 {
                ImageReadParam param = imageReader.getDefaultReadParam();
                imageReader.setInput(imageInputStream, true, true);
                int minIndex = imageReader.getMinIndex();
                return imageReader.read(minIndex, param);
            } finally {
                imageReader.dispose();
            }
        }
    } finally {
        imageInputStream.close();
    }
    throw new IOException("Can't read image from given content");
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with ImageReader

use of javax.imageio.ImageReader in project kotlin by JetBrains.

the class IconDetector method checkExtension.

private static void checkExtension(Context context, File file) {
    try {
        ImageInputStream input = ImageIO.createImageInputStream(file);
        if (input != null) {
            try {
                Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
                while (readers.hasNext()) {
                    ImageReader reader = readers.next();
                    try {
                        reader.setInput(input);
                        // Check file extension
                        String formatName = reader.getFormatName();
                        if (formatName != null && !formatName.isEmpty()) {
                            String path = file.getPath();
                            int index = path.lastIndexOf('.');
                            String extension = path.substring(index + 1).toLowerCase(Locale.US);
                            if (!formatName.equalsIgnoreCase(extension)) {
                                if (endsWith(path, DOT_JPG) && formatName.equals("JPEG")) {
                                    //$NON-NLS-1$
                                    return;
                                }
                                String message = String.format("Misleading file extension; named `.%1$s` but the " + "file format is `%2$s`", extension, formatName);
                                Location location = Location.create(file);
                                context.report(ICON_EXTENSION, location, message);
                            }
                            break;
                        }
                    } finally {
                        reader.dispose();
                    }
                }
            } finally {
                input.close();
            }
        }
    } catch (IOException e) {
    // Pass -- we can't handle all image types, warn about those we can
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader)

Example 38 with ImageReader

use of javax.imageio.ImageReader in project kotlin by JetBrains.

the class IconDetector method getSize.

private static Dimension getSize(File file) {
    try {
        ImageInputStream input = ImageIO.createImageInputStream(file);
        if (input != null) {
            try {
                Iterator<ImageReader> readers = ImageIO.getImageReaders(input);
                if (readers.hasNext()) {
                    ImageReader reader = readers.next();
                    try {
                        reader.setInput(input);
                        return new Dimension(reader.getWidth(0), reader.getHeight(0));
                    } finally {
                        reader.dispose();
                    }
                }
            } finally {
                input.close();
            }
        }
        // Fallback: read the image using the normal means
        BufferedImage image = ImageIO.read(file);
        if (image != null) {
            return new Dimension(image.getWidth(), image.getHeight());
        } else {
            return null;
        }
    } catch (IOException e) {
        // Pass -- we can't handle all image types, warn about those we can
        return null;
    }
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage)

Example 39 with ImageReader

use of javax.imageio.ImageReader in project camel by apache.

the class BarcodeTestBase method checkType.

private void checkType(File file, String type) throws IOException {
    ImageInputStream iis = ImageIO.createImageInputStream(file);
    ImageReader reader = ImageIO.getImageReaders(iis).next();
    IOHelper.close(iis);
    String format = reader.getFormatName();
    assertEquals(type, format.toUpperCase());
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ImageReader(javax.imageio.ImageReader)

Example 40 with ImageReader

use of javax.imageio.ImageReader in project zm-mailbox by Zimbra.

the class ImageUtil method getImageReader.

/**
     * Returns the {@code ImageReader}, or {@code null} if none is available.
     * @param contentType the MIME content type, or {@code null} 
     * @param filename the filename, or {@code null}
     */
public static ImageReader getImageReader(String contentType, String filename) {
    log.debug("Looking up ImageReader for %s, %s", contentType, filename);
    ImageReader reader = null;
    if (!StringUtil.isNullOrEmpty(contentType)) {
        reader = Iterators.getNext(ImageIO.getImageReadersByMIMEType(contentType), null);
    }
    if (reader == null && !StringUtil.isNullOrEmpty(filename)) {
        String ext = FileUtil.getExtension(filename);
        if (!StringUtil.isNullOrEmpty(ext)) {
            reader = Iterators.getNext(ImageIO.getImageReadersBySuffix(ext), null);
        }
    }
    log.debug("Returning %s", reader);
    return reader;
}
Also used : ImageReader(javax.imageio.ImageReader)

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