Search in sources :

Example 51 with ImageReader

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

the class PluginSpiTest method testFormat.

public static void testFormat(String format) {
    ImageReader reader = ImageIO.getImageReadersByFormatName(format).next();
    if (reader == null) {
        throw new RuntimeException("Failed to get reader for " + format);
    }
    ImageReaderSpi readerSpi = reader.getOriginatingProvider();
    System.out.println(format + " Reader SPI: " + readerSpi);
    String[] writerSpiNames = readerSpi.getImageWriterSpiNames();
    if (writerSpiNames == null || writerSpiNames.length == 0) {
        throw new RuntimeException("Failed to get writer spi names for " + format);
    }
    System.out.println("Available writer spi names:");
    for (int i = 0; i < writerSpiNames.length; i++) {
        System.out.println(writerSpiNames[i]);
        try {
            Class spiClass = Class.forName(writerSpiNames[i]);
            if (spiClass == null) {
                throw new RuntimeException("Failed to get spi class " + writerSpiNames[i]);
            }
            System.out.println("Got class " + spiClass.getName());
            Object spiObject = spiClass.newInstance();
            if (spiObject == null) {
                throw new RuntimeException("Failed to instantiate spi " + writerSpiNames[i]);
            }
            System.out.println("Got instance " + spiObject);
        } catch (Throwable e) {
            throw new RuntimeException("Failed to test spi " + writerSpiNames[i]);
        }
    }
    ImageWriter writer = ImageIO.getImageWriter(reader);
    if (writer == null) {
        throw new RuntimeException("Failed to get writer for " + format);
    }
}
Also used : ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) ImageWriter(javax.imageio.ImageWriter) ImageReader(javax.imageio.ImageReader)

Example 52 with ImageReader

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

the class CanDecodeTest method main.

public static void main(String[] args) throws IOException {
    ImageReader r = ImageIO.getImageReadersByFormatName("WBMP").next();
    ImageReaderSpi spi = r.getOriginatingProvider();
    Vector<TestCase> tests = getTestCases();
    for (TestCase t : tests) {
        t.doTest(spi);
    }
    System.out.println("Test passed.");
}
Also used : ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) ImageReader(javax.imageio.ImageReader)

Example 53 with ImageReader

use of javax.imageio.ImageReader in project yyl_example by Relucent.

the class GetImageTypeTest method getFormatName.

/**
	 * 读取图片类型
	 * @param input 图片数据流
	 * @return 图片类型
	 */
private static String getFormatName(InputStream input) {
    ImageInputStream is = null;
    try {
        // Create an image input stream on the image
        is = ImageIO.createImageInputStream(input);
        // Find all image readers that recognize the image format
        Iterator<ImageReader> it = ImageIO.getImageReaders(is);
        if (!it.hasNext()) {
            return null;
        }
        // Use the first reader
        ImageReader reader = it.next();
        // Return the format name
        return reader.getFormatName();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    // The image could not be read
    return null;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) IOException(java.io.IOException)

Example 54 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)

Example 55 with ImageReader

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

the class BitmapImageRenderer method readImage.

/**
     * Read the image data via ImageIO and optionally try to workaround metadata errors.
     * The resulting image is of image type {@link BufferedImage#TYPE_INT_ARGB}
     *
     * @param data the data stream
     * @param contentType the content type
     * @return the bufferedImage or null, if there was no image reader for this content type
     * @throws IOException thrown if there was an error while processing the image
     */
private static BufferedImage readImage(InputStream data, String contentType) throws IOException {
    IOException lastException = null;
    BufferedImage img = null;
    if (data.markSupported()) {
        data.mark(data.available());
    }
    // currently don't use FileCacheImageInputStream,
    // because of the risk of filling the file handles (see #59166)
    ImageInputStream iis = new MemoryCacheImageInputStream(data);
    try {
        iis = new MemoryCacheImageInputStream(data);
        iis.mark();
        Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        while (img == null && iter.hasNext()) {
            ImageReader reader = iter.next();
            ImageReadParam param = reader.getDefaultReadParam();
            // 0:default mode, 1:fallback mode
            for (int mode = 0; img == null && mode < 3; mode++) {
                lastException = null;
                try {
                    iis.reset();
                } catch (IOException e) {
                    if (data.markSupported()) {
                        data.reset();
                        data.mark(data.available());
                        iis.close();
                        iis = new MemoryCacheImageInputStream(data);
                    } else {
                        // can't restore the input stream, so we need to stop processing here
                        lastException = e;
                        break;
                    }
                }
                iis.mark();
                try {
                    switch(mode) {
                        case 0:
                            reader.setInput(iis, false, true);
                            img = reader.read(0, param);
                            break;
                        case 1:
                            {
                                // try to load picture in gray scale mode
                                // fallback mode for invalid image band metadata
                                // see http://stackoverflow.com/questions/10416378
                                Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
                                while (imageTypes.hasNext()) {
                                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                                    int bufferedImageType = imageTypeSpecifier.getBufferedImageType();
                                    if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) {
                                        param.setDestinationType(imageTypeSpecifier);
                                        break;
                                    }
                                }
                                reader.setInput(iis, false, true);
                                img = reader.read(0, param);
                                break;
                            }
                        case 2:
                            {
                                // try to load truncated pictures by supplying a BufferedImage
                                // and use the processed data up till the point of error
                                reader.setInput(iis, false, true);
                                int height = reader.getHeight(0);
                                int width = reader.getWidth(0);
                                Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0);
                                if (imageTypes.hasNext()) {
                                    ImageTypeSpecifier imageTypeSpecifier = imageTypes.next();
                                    img = imageTypeSpecifier.createBufferedImage(width, height);
                                    param.setDestination(img);
                                } else {
                                    lastException = new IOException("unable to load even a truncated version of the image.");
                                    break;
                                }
                                try {
                                    reader.read(0, param);
                                } finally {
                                    if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
                                        int y = findTruncatedBlackBox(img, width, height);
                                        if (y < height) {
                                            BufferedImage argbImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
                                            Graphics2D g = argbImg.createGraphics();
                                            g.clipRect(0, 0, width, y);
                                            g.drawImage(img, 0, 0, null);
                                            g.dispose();
                                            img.flush();
                                            img = argbImg;
                                        }
                                    }
                                }
                                break;
                            }
                    }
                } catch (IOException e) {
                    if (mode < 2) {
                        lastException = e;
                    }
                } catch (RuntimeException e) {
                    if (mode < 2) {
                        lastException = new IOException("ImageIO runtime exception - " + (mode == 0 ? "normal" : "fallback"), e);
                    }
                }
            }
            reader.dispose();
        }
    } finally {
        iis.close();
    }
    // If you don't have an image at the end of all readers
    if (img == null) {
        if (lastException != null) {
            // multiple locations above ...
            throw lastException;
        }
        LOG.log(POILogger.WARN, "Content-type: " + contentType + " is not support. Image ignored.");
        return null;
    }
    // add alpha channel
    if (img.getType() != BufferedImage.TYPE_INT_ARGB) {
        BufferedImage argbImg = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics g = argbImg.getGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();
        return argbImg;
    }
    return img;
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Graphics2D(java.awt.Graphics2D) Graphics(java.awt.Graphics) ImageReadParam(javax.imageio.ImageReadParam) Iterator(java.util.Iterator) ImageReader(javax.imageio.ImageReader)

Aggregations

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