Search in sources :

Example 41 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 42 with ImageReader

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

the class GetNumImages method main.

public static void main(String[] args) throws Exception {
    IIORegistry registry = IIORegistry.getDefaultInstance();
    // test ImageReader.getNumImages() for all available ImageReaders,
    // with no source set
    Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
    while (readerspis.hasNext()) {
        boolean caughtEx = false;
        ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
        ImageReader reader = readerspi.createReaderInstance();
        try {
            reader.getNumImages(false);
        } catch (IllegalStateException ise) {
            // caught exception, everything's okay
            caughtEx = true;
        }
        if (!caughtEx) {
            throw new RuntimeException("Test failed: exception was not " + "thrown for null input: " + reader);
        }
    }
    // test ImageReader.getNumImages() for all available ImageReaders,
    // with source set, seekForwardOnly and allowSearch both true
    readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
    while (readerspis.hasNext()) {
        boolean caughtEx = false;
        ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
        ImageReader reader = readerspi.createReaderInstance();
        byte[] barr = new byte[100];
        ByteArrayInputStream bais = new ByteArrayInputStream(barr);
        ImageInputStream iis = ImageIO.createImageInputStream(bais);
        try {
            reader.setInput(iis, true);
            reader.getNumImages(true);
        } catch (IllegalStateException ise) {
            // caught exception, everything's okay
            caughtEx = true;
        }
        if (!caughtEx) {
            throw new RuntimeException("Test failed: exception was not " + "thrown when allowSearch and " + "seekForwardOnly are both true: " + reader);
        }
    }
}
Also used : IIORegistry(javax.imageio.spi.IIORegistry) ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageInputStream(javax.imageio.stream.ImageInputStream) Iterator(java.util.Iterator) ImageReader(javax.imageio.ImageReader)

Example 43 with ImageReader

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

the class AllowSearch method main.

public static void main(String[] args) throws IOException {
    ImageReader gifReader = new GIFImageReader(null);
    ImageReader jpegReader = new JPEGImageReader(null);
    ImageReader pngReader = new PNGImageReader(null);
    test(gifReader, "GIF");
    test(jpegReader, "JPEG");
    test(pngReader, "PNG");
}
Also used : JPEGImageReader(com.sun.imageio.plugins.jpeg.JPEGImageReader) PNGImageReader(com.sun.imageio.plugins.png.PNGImageReader) GIFImageReader(com.sun.imageio.plugins.gif.GIFImageReader) ImageReader(javax.imageio.ImageReader) JPEGImageReader(com.sun.imageio.plugins.jpeg.JPEGImageReader) PNGImageReader(com.sun.imageio.plugins.png.PNGImageReader) GIFImageReader(com.sun.imageio.plugins.gif.GIFImageReader)

Example 44 with ImageReader

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

the class BmpBigDestinationTest method main.

public static void main(String[] args) {
    try {
        BufferedImage src = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = src.createGraphics();
        g.setColor(Color.red);
        g.fillRect(0, 0, 100, 100);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageWriter iw = (ImageWriter) ImageIO.getImageWritersByFormatName(format).next();
        if (iw == null) {
            throw new RuntimeException("No writer available. Test failed.");
        }
        iw.setOutput(ImageIO.createImageOutputStream(baos));
        iw.write(src);
        byte[] data = baos.toByteArray();
        ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName(format).next();
        ir.setInput(ImageIO.createImageInputStream(new ByteArrayInputStream(data)));
        Iterator specifiers = ir.getImageTypes(0);
        ImageTypeSpecifier typeSpecifier = null;
        if (specifiers.hasNext()) {
            typeSpecifier = (ImageTypeSpecifier) specifiers.next();
        }
        ImageReadParam param = new ImageReadParam();
        BufferedImage dst = typeSpecifier.createBufferedImage(200, 200);
        param.setDestination(dst);
        ir.read(0, param);
        checkResults(src, dst);
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Unexpected exception. Test failed.");
    }
}
Also used : ImageReadParam(javax.imageio.ImageReadParam) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageWriter(javax.imageio.ImageWriter) Iterator(java.util.Iterator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) Graphics2D(java.awt.Graphics2D)

Example 45 with ImageReader

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

the class AnimationTest method checkAnimation.

protected void checkAnimation() throws IOException {
    ImageReader r = ImageIO.getImageReadersByFormatName("GIF").next();
    r.setInput(ImageIO.createImageInputStream(new File(fname)));
    int n = r.getNumImages(true);
    for (int i = 0; i < n; i++) {
        BufferedImage f = r.read(i);
        checkFrame(i, f);
    }
    System.out.println("Test passed.");
}
Also used : ImageReader(javax.imageio.ImageReader) File(java.io.File) 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