Java Code Examples for javax.imageio.ImageReader#addIIOReadProgressListener()

The following examples show how to use javax.imageio.ImageReader#addIIOReadProgressListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReadAbortTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public ReadAbortTest(String format) throws Exception {
    try {
        System.out.println("Test for format " + format);
        bimg = new BufferedImage(width, heght,
                BufferedImage.TYPE_INT_RGB);

        Graphics2D g = bimg.createGraphics();
        g.setColor(srccolor);
        g.fillRect(0, 0, width, heght);
        g.dispose();

        file = File.createTempFile("src_", "." + format, new File("."));
        ImageIO.write(bimg, format, file);
        ImageInputStream iis = ImageIO.createImageInputStream(file);

        Iterator iter = ImageIO.getImageReaders(iis);
        while (iter.hasNext()) {
            reader = (ImageReader) iter.next();
            break;
        }
        reader.setInput(iis);
        reader.addIIOReadProgressListener(this);

        // Abort reading in IIOReadProgressListener.imageStarted().
        startAbort = true;
        bimg = reader.read(0);
        startAbort = false;

        // Abort reading in IIOReadProgressListener.imageProgress().
        progressAbort = true;
        bimg = reader.read(0);
        progressAbort = false;

        iis.close();
        /*
         * All abort requests from imageStarted,imageProgress and
         * imageComplete from IIOReadProgressListener should be reached
         * otherwise throw RuntimeException.
         */
        if (!(startAborted
                && progressAborted)) {
            throw new RuntimeException("All IIOReadProgressListener abort"
                    + " requests are not processed for format "
                    + format);
        }
    } catch (Exception e) {
        throw e;
    } finally {
        Files.delete(file.toPath());
    }
}
 
Example 2
Source File: ReaderListenersTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void doTest(String compression) {
    try {
        BufferedImage img = createTestImage();

        ImageWriter iw = (ImageWriter)
            ImageIO.getImageWritersByFormatName("bmp").next();
        if (iw == null) {
            throw new RuntimeException("No writers for bmp format."
                                       + " Test failed.");
        }


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        iw.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = iw.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionType(compression);

        iw.write(null, new IIOImage(img, null, null), param);
        baos.close();

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

        ImageReader ir = (ImageReader)
            ImageIO.getImageReadersByFormatName("bmp").next();
        if (ir == null) {
            throw new RuntimeException("No readers for bmp format."
                                       + " Test failed.");
        }

        IIOReadUpdateAdapter updateAdapter = new IIOReadUpdateAdapter();
        IIOReadProgressAdapter progressAdapter = new IIOReadProgressAdapter();
        ir.addIIOReadProgressListener(progressAdapter);
        ir.addIIOReadUpdateListener(updateAdapter);
        ir.setInput(ImageIO.createImageInputStream(bais));
        BufferedImage dst = ir.read(0);

        progressAdapter.checkResults();

        if (!updateAdapter.isImageUpdateUsed) {
            throw new RuntimeException("imageUpdate was not used."
                                       + " Test failed.");
        }
    } catch(IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed");
    }
}
 
Example 3
Source File: ReaderListenersTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void doTest(String compression) {
    try {
        BufferedImage img = createTestImage();

        ImageWriter iw = (ImageWriter)
            ImageIO.getImageWritersByFormatName("bmp").next();
        if (iw == null) {
            throw new RuntimeException("No writers for bmp format."
                                       + " Test failed.");
        }


        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        iw.setOutput(ImageIO.createImageOutputStream(baos));
        ImageWriteParam param = iw.getDefaultWriteParam();
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionType(compression);

        iw.write(null, new IIOImage(img, null, null), param);
        baos.close();

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

        ImageReader ir = (ImageReader)
            ImageIO.getImageReadersByFormatName("bmp").next();
        if (ir == null) {
            throw new RuntimeException("No readers for bmp format."
                                       + " Test failed.");
        }

        IIOReadUpdateAdapter updateAdapter = new IIOReadUpdateAdapter();
        IIOReadProgressAdapter progressAdapter = new IIOReadProgressAdapter();
        ir.addIIOReadProgressListener(progressAdapter);
        ir.addIIOReadUpdateListener(updateAdapter);
        ir.setInput(ImageIO.createImageInputStream(bais));
        BufferedImage dst = ir.read(0);

        progressAdapter.checkResults();

        if (!updateAdapter.isImageUpdateUsed) {
            throw new RuntimeException("imageUpdate was not used."
                                       + " Test failed.");
        }
    } catch(IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Test failed");
    }
}