Search in sources :

Example 46 with ImageReader

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

the class EmptyInputBmpMetadataTest method main.

public static void main(String[] args) {
    boolean isPassed = false;
    ImageReader ir = (ImageReader) ImageIO.getImageReadersByFormatName(fmt).next();
    if (ir == null) {
        throw new RuntimeException("No available reader for " + fmt);
    }
    IIOMetadata meta = null;
    try {
        meta = ir.getImageMetadata(0);
    } catch (IllegalStateException e) {
        System.out.println("Correct exception was thrown. Test passed.");
        isPassed = true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (!isPassed) {
        throw new RuntimeException("The IllegalStateException was not thrown." + "Test failed.");
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader)

Example 47 with ImageReader

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

the class ITXtTest method readFrom.

private static ITXtTest readFrom(File f) {
    try {
        ImageInputStream iis = ImageIO.createImageInputStream(f);
        ImageReader r = ImageIO.getImageReaders(iis).next();
        r.setInput(iis);
        IIOImage dst = r.readAll(0, null);
        // look for iTXt node
        IIOMetadata m = dst.getMetadata();
        Node root = m.getAsTree(m.getNativeMetadataFormatName());
        Node n = root.getFirstChild();
        while (n != null && !"iTXt".equals(n.getNodeName())) {
            n = n.getNextSibling();
        }
        if (n == null) {
            throw new RuntimeException("No iTXt node!");
        }
        ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode) n);
        return t;
    } catch (Throwable e) {
        throw new RuntimeException("Reading test failed.", e);
    }
}
Also used : IIOMetadata(javax.imageio.metadata.IIOMetadata) ImageInputStream(javax.imageio.stream.ImageInputStream) IIOMetadataNode(javax.imageio.metadata.IIOMetadataNode) Node(org.w3c.dom.Node) ImageReader(javax.imageio.ImageReader) IIOImage(javax.imageio.IIOImage)

Example 48 with ImageReader

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

the class ReadAsGrayTest method doTest.

private static void doTest(int type) throws IOException {
    BufferedImage src = createTestImage(type);
    File f = new File("test.jpg");
    if (!ImageIO.write(src, "jpg", f)) {
        throw new RuntimeException("Failed to write test image.");
    }
    ImageInputStream iis = ImageIO.createImageInputStream(f);
    ImageReader reader = ImageIO.getImageReaders(iis).next();
    reader.setInput(iis);
    Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0);
    ImageTypeSpecifier srgb = null;
    ImageTypeSpecifier gray = null;
    // look for gray and srgb types
    while ((srgb == null || gray == null) && types.hasNext()) {
        ImageTypeSpecifier t = types.next();
        if (t.getColorModel().getColorSpace().getType() == TYPE_GRAY) {
            gray = t;
        }
        if (t.getColorModel().getColorSpace() == sRGB) {
            srgb = t;
        }
    }
    if (gray == null) {
        throw new RuntimeException("No gray type available.");
    }
    if (srgb == null) {
        throw new RuntimeException("No srgb type available.");
    }
    System.out.println("Read as GRAY...");
    testType(reader, gray, src);
    System.out.println("Read as sRGB...");
    testType(reader, srgb, src);
}
Also used : ImageInputStream(javax.imageio.stream.ImageInputStream) ImageReader(javax.imageio.ImageReader) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 49 with ImageReader

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

the class JPEGsNotAcceleratedTest method readTestImage.

public static BufferedImage readTestImage(String fileName, BufferedImage dest, Rectangle srcROI) {
    BufferedImage bi = null;
    try {
        FileImageInputStream is = new FileImageInputStream(new File(fileName));
        ImageReader reader = (ImageReader) ImageIO.getImageReaders(is).next();
        ImageReadParam param = reader.getDefaultReadParam();
        if (dest != null) {
            param.setDestination(dest);
        }
        if (srcROI != null) {
            param.setSourceRegion(srcROI);
        }
        reader.setInput(is);
        bi = reader.read(0, param);
    } catch (IOException e) {
        System.err.println("Error " + e + " when reading file: " + fileName);
        throw new RuntimeException(e);
    }
    return bi;
}
Also used : FileImageInputStream(javax.imageio.stream.FileImageInputStream) ImageReadParam(javax.imageio.ImageReadParam) IOException(java.io.IOException) ImageReader(javax.imageio.ImageReader) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 50 with ImageReader

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

the class NullInputOutput method main.

public static void main(String[] args) throws Exception {
    IIORegistry registry = IIORegistry.getDefaultInstance();
    // test ImageReader.read() for all available ImageReaders
    Iterator readerspis = registry.getServiceProviders(ImageReaderSpi.class, false);
    while (readerspis.hasNext()) {
        ImageReaderSpi readerspi = (ImageReaderSpi) readerspis.next();
        ImageReader reader = readerspi.createReaderInstance();
        try {
            reader.read(0);
        } catch (IllegalStateException ise) {
        // caught exception, everything's okay
        }
    }
    // test ImageWriter.write() for all available ImageWriters
    BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    Iterator writerspis = registry.getServiceProviders(ImageWriterSpi.class, false);
    while (writerspis.hasNext()) {
        ImageWriterSpi writerspi = (ImageWriterSpi) writerspis.next();
        ImageWriter writer = writerspi.createWriterInstance();
        try {
            writer.write(bi);
        } catch (IllegalStateException ise) {
        // caught exception, everything's okay
        }
    }
}
Also used : IIORegistry(javax.imageio.spi.IIORegistry) ImageReaderSpi(javax.imageio.spi.ImageReaderSpi) Iterator(java.util.Iterator) ImageWriter(javax.imageio.ImageWriter) ImageReader(javax.imageio.ImageReader) ImageWriterSpi(javax.imageio.spi.ImageWriterSpi) 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