Java Code Examples for javax.imageio.ImageWriter#prepareWriteSequence()
The following examples show how to use
javax.imageio.ImageWriter#prepareWriteSequence() .
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: CanWriteSequence.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
private static void test(final ImageWriter writer) throws Exception { final File file = File.createTempFile("temp", ".img"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); final ImageOutputStream ios = ImageIO.createImageOutputStream(fos); writer.setOutput(ios); final IIOMetadata data = writer.getDefaultStreamMetadata(null); if (writer.canWriteSequence()) { writer.prepareWriteSequence(data); } else { try { writer.prepareWriteSequence(data); throw new RuntimeException( "UnsupportedOperationException was not thrown"); } catch (final UnsupportedOperationException ignored) { // expected } } writer.dispose(); ios.close(); }
Example 2
Source File: LUTCompareTest.java From openjdk-8 with GNU General Public License v2.0 | 6 votes |
private static Image createTestImage() throws IOException { BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 }); BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 }); ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif")); ImageWriteParam param = writer.getDefaultWriteParam(); writer.setOutput(ios); writer.prepareWriteSequence(null); writer.writeToSequence(new IIOImage(frame1, null, null), param); writer.writeToSequence(new IIOImage(frame2, null, null), param); writer.endWriteSequence(); writer.reset(); writer.dispose(); ios.flush(); ios.close(); return Toolkit.getDefaultToolkit().createImage("lut_test.gif"); }
Example 3
Source File: LUTCompareTest.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private static Image createTestImage() throws IOException { BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 }); BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 }); ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif")); ImageWriteParam param = writer.getDefaultWriteParam(); writer.setOutput(ios); writer.prepareWriteSequence(null); writer.writeToSequence(new IIOImage(frame1, null, null), param); writer.writeToSequence(new IIOImage(frame2, null, null), param); writer.endWriteSequence(); writer.reset(); writer.dispose(); ios.flush(); ios.close(); return Toolkit.getDefaultToolkit().createImage("lut_test.gif"); }
Example 4
Source File: LUTCompareTest.java From jdk8u-dev-jdk with GNU General Public License v2.0 | 6 votes |
private static Image createTestImage() throws IOException { BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 }); BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 }); ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif")); ImageWriteParam param = writer.getDefaultWriteParam(); writer.setOutput(ios); writer.prepareWriteSequence(null); writer.writeToSequence(new IIOImage(frame1, null, null), param); writer.writeToSequence(new IIOImage(frame2, null, null), param); writer.endWriteSequence(); writer.reset(); writer.dispose(); ios.flush(); ios.close(); return Toolkit.getDefaultToolkit().createImage("lut_test.gif"); }
Example 5
Source File: PrepareWriteSequenceTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { String format = "GIF"; ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); ImageWriteParam param = writer.getDefaultWriteParam(); IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(param); boolean gotException = false; try { writer.prepareWriteSequence(streamMetadata); } catch (IllegalStateException e) { gotException = true; System.out.println("Test passed."); e.printStackTrace(System.out); } if (!gotException) { throw new RuntimeException("Test failed."); } }
Example 6
Source File: CanWriteSequence.java From hottub with GNU General Public License v2.0 | 6 votes |
private static void test(final ImageWriter writer) throws Exception { final File file = File.createTempFile("temp", ".img"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); final ImageOutputStream ios = ImageIO.createImageOutputStream(fos); writer.setOutput(ios); final IIOMetadata data = writer.getDefaultStreamMetadata(null); if (writer.canWriteSequence()) { writer.prepareWriteSequence(data); } else { try { writer.prepareWriteSequence(data); throw new RuntimeException( "UnsupportedOperationException was not thrown"); } catch (final UnsupportedOperationException ignored) { // expected } } writer.dispose(); ios.close(); }
Example 7
Source File: LUTCompareTest.java From hottub with GNU General Public License v2.0 | 6 votes |
private static Image createTestImage() throws IOException { BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 }); BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 }); ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif")); ImageWriteParam param = writer.getDefaultWriteParam(); writer.setOutput(ios); writer.prepareWriteSequence(null); writer.writeToSequence(new IIOImage(frame1, null, null), param); writer.writeToSequence(new IIOImage(frame2, null, null), param); writer.endWriteSequence(); writer.reset(); writer.dispose(); ios.flush(); ios.close(); return Toolkit.getDefaultToolkit().createImage("lut_test.gif"); }
Example 8
Source File: LUTCompareTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 6 votes |
private static Image createTestImage() throws IOException { BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 }); BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 }); ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif")); ImageWriteParam param = writer.getDefaultWriteParam(); writer.setOutput(ios); writer.prepareWriteSequence(null); writer.writeToSequence(new IIOImage(frame1, null, null), param); writer.writeToSequence(new IIOImage(frame2, null, null), param); writer.endWriteSequence(); writer.reset(); writer.dispose(); ios.flush(); ios.close(); return Toolkit.getDefaultToolkit().createImage("lut_test.gif"); }
Example 9
Source File: LUTCompareTest.java From openjdk-jdk8u-backup with GNU General Public License v2.0 | 6 votes |
private static Image createTestImage() throws IOException { BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 }); BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 }); ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif")); ImageWriteParam param = writer.getDefaultWriteParam(); writer.setOutput(ios); writer.prepareWriteSequence(null); writer.writeToSequence(new IIOImage(frame1, null, null), param); writer.writeToSequence(new IIOImage(frame2, null, null), param); writer.endWriteSequence(); writer.reset(); writer.dispose(); ios.flush(); ios.close(); return Toolkit.getDefaultToolkit().createImage("lut_test.gif"); }
Example 10
Source File: CanWriteSequence.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private static void test(final ImageWriter writer) throws Exception { final File file = File.createTempFile("temp", ".img"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); final ImageOutputStream ios = ImageIO.createImageOutputStream(fos); writer.setOutput(ios); final IIOMetadata data = writer.getDefaultStreamMetadata(null); if (writer.canWriteSequence()) { writer.prepareWriteSequence(data); } else { try { writer.prepareWriteSequence(data); throw new RuntimeException( "UnsupportedOperationException was not thrown"); } catch (final UnsupportedOperationException ignored) { // expected } } writer.dispose(); ios.close(); }
Example 11
Source File: LUTCompareTest.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
private static Image createTestImage() throws IOException { BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 }); BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 }); ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif")); ImageWriteParam param = writer.getDefaultWriteParam(); writer.setOutput(ios); writer.prepareWriteSequence(null); writer.writeToSequence(new IIOImage(frame1, null, null), param); writer.writeToSequence(new IIOImage(frame2, null, null), param); writer.endWriteSequence(); writer.reset(); writer.dispose(); ios.flush(); ios.close(); return Toolkit.getDefaultToolkit().createImage("lut_test.gif"); }
Example 12
Source File: PrepareWriteSequenceTest.java From jdk8u_jdk with GNU General Public License v2.0 | 6 votes |
public static void main(String[] args) throws IOException { String format = "GIF"; ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); ImageWriteParam param = writer.getDefaultWriteParam(); IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(param); boolean gotException = false; try { writer.prepareWriteSequence(streamMetadata); } catch (IllegalStateException e) { gotException = true; System.out.println("Test passed."); e.printStackTrace(System.out); } if (!gotException) { throw new RuntimeException("Test failed."); } }
Example 13
Source File: LUTCompareTest.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private static Image createTestImage() throws IOException { BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 }); BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 }); ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif")); ImageWriteParam param = writer.getDefaultWriteParam(); writer.setOutput(ios); writer.prepareWriteSequence(null); writer.writeToSequence(new IIOImage(frame1, null, null), param); writer.writeToSequence(new IIOImage(frame2, null, null), param); writer.endWriteSequence(); writer.reset(); writer.dispose(); ios.flush(); ios.close(); return Toolkit.getDefaultToolkit().createImage("lut_test.gif"); }
Example 14
Source File: CanWriteSequence.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private static void test(final ImageWriter writer) throws Exception { final File file = File.createTempFile("temp", ".img"); file.deleteOnExit(); final FileOutputStream fos = new FileOutputStream(file); final ImageOutputStream ios = ImageIO.createImageOutputStream(fos); writer.setOutput(ios); final IIOMetadata data = writer.getDefaultStreamMetadata(null); if (writer.canWriteSequence()) { writer.prepareWriteSequence(data); } else { try { writer.prepareWriteSequence(data); throw new RuntimeException( "UnsupportedOperationException was not thrown"); } catch (final UnsupportedOperationException ignored) { // expected } } writer.dispose(); ios.close(); }
Example 15
Source File: LUTCompareTest.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
private static Image createTestImage() throws IOException { BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 }); BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 }); ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next(); ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif")); ImageWriteParam param = writer.getDefaultWriteParam(); writer.setOutput(ios); writer.prepareWriteSequence(null); writer.writeToSequence(new IIOImage(frame1, null, null), param); writer.writeToSequence(new IIOImage(frame2, null, null), param); writer.endWriteSequence(); writer.reset(); writer.dispose(); ios.flush(); ios.close(); return Toolkit.getDefaultToolkit().createImage("lut_test.gif"); }
Example 16
Source File: AnimationTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) { try { AnimationTest t = new AnimationTest(); t.initFrame(); ImageWriter w = t.initWriter(); ImageWriteParam p = w.getDefaultWriteParam(); IIOMetadata streamMetadata = w.getDefaultStreamMetadata(p); w.prepareWriteSequence(streamMetadata); for (int i = 0; i < 50; i++) { BufferedImage f = t.createNextFrame(); ImageTypeSpecifier type = new ImageTypeSpecifier(f); IIOMetadata m = w.getDefaultImageMetadata(type, p); w.writeToSequence(new IIOImage(f, null, m), p); } w.endWriteSequence(); t.checkAnimation(); } catch (Exception e) { throw new RuntimeException("Test failed.", e); } }
Example 17
Source File: AnimatedGifExport.java From snap-desktop with GNU General Public License v3.0 | 5 votes |
private void exportAnimation(String delayTime, File file, ProgressMonitor pm) { ImageWriter imageWriter = ImageIO.getImageWritersByFormatName("gif").next(); try { ImageOutputStream outputStream = ImageIO.createImageOutputStream(file); imageWriter.setOutput(outputStream); imageWriter.prepareWriteSequence(null); pm.beginTask("Exporting time series as animated gif", frames.length); for (int i = 0; i < frames.length; i++) { RenderedImage currentImage = frames[i]; ImageWriteParam writeParameters = imageWriter.getDefaultWriteParam(); IIOMetadata metadata = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(currentImage), writeParameters); configure(metadata, delayTime, i); IIOImage image = new IIOImage(currentImage, null, metadata); imageWriter.writeToSequence(image, null); pm.worked(1); } imageWriter.endWriteSequence(); outputStream.close(); pm.done(); } catch (IOException e) { SnapApp.getDefault().handleError("Unable to create animated gif", e); } }
Example 18
Source File: Utils.java From lizzie with GNU General Public License v3.0 | 4 votes |
public static void toGif( String path, List<BufferedImage> frames, int delayTime, boolean override) { if (Utils.isBlank(path) || frames == null || frames.size() == 0) return; Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("gif"); try { File file = new File(path); if (override) { file.deleteOnExit(); } ImageWriter writer = it.hasNext() ? it.next() : null; ImageOutputStream stream = ImageIO.createImageOutputStream(file); if (Objects.isNull(writer)) { throw new IOException(); } writer.setOutput(stream); writer.prepareWriteSequence(null); IIOMetadataNode gce = new IIOMetadataNode("GraphicControlExtension"); gce.setAttribute("disposalMethod", "none"); gce.setAttribute("userInputFlag", "FALSE"); gce.setAttribute("transparentColorFlag", "FALSE"); gce.setAttribute("transparentColorIndex", "0"); gce.setAttribute("delayTime", Objects.toString(delayTime)); IIOMetadataNode ae = new IIOMetadataNode("ApplicationExtension"); ae.setAttribute("applicationID", "NETSCAPE"); ae.setAttribute("authenticationCode", "2.0"); ae.setUserObject(new byte[] {0x1, 0x0, 0x0}); IIOMetadataNode aes = new IIOMetadataNode("ApplicationExtensions"); aes.appendChild(ae); for (BufferedImage image : frames) { ImageWriteParam iwp = writer.getDefaultWriteParam(); IIOMetadata metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), iwp); String metaFormat = metadata.getNativeMetadataFormatName(); Node root = metadata.getAsTree(metaFormat); root.appendChild(gce); root.appendChild(aes); metadata.setFromTree(metaFormat, root); writer.writeToSequence(new IIOImage(image, null, metadata), null); metadata = null; } writer.endWriteSequence(); stream.close(); } catch (IOException ex) { ex.printStackTrace(); } }
Example 19
Source File: WriterReuseTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
public static void doTest(boolean writeSequence) throws IOException { String format = "GIF"; ImageWriter writer = ImageIO.getImageWritersByFormatName(format).next(); if (writer == null) { throw new RuntimeException("No writer available for " + format); } BufferedImage img = createTestImage(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageOutputStream ios = ImageIO.createImageOutputStream(baos); writer.setOutput(ios); WriterReuseTest t = new WriterReuseTest(); writer.addIIOWriteProgressListener(t); ImageWriteParam param = writer.getDefaultWriteParam(); IIOMetadata streamMetadata = writer.getDefaultStreamMetadata(param); IIOImage iioImg = new IIOImage(img, null, null); if (writeSequence) { writer.prepareWriteSequence(streamMetadata); writer.writeToSequence(iioImg, param); } else { writer.write(img); } if (!t.isWritingAborted || t.isWritingCompleted) { throw new RuntimeException("Test failed."); } t.reset(); // next attempt after abort ImageOutputStream ios2 = ImageIO.createImageOutputStream(new ByteArrayOutputStream()); writer.setOutput(ios2); if (writeSequence) { writer.writeToSequence(iioImg, param); } else { writer.write(img); } if (t.isWritingAborted || !t.isWritingCompleted) { throw new RuntimeException("Test failed."); } System.out.println("Test passed."); }
Example 20
Source File: WriteToSequenceAfterAbort.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
private void test(final ImageWriter writer) throws IOException { String suffix = writer.getOriginatingProvider().getFileSuffixes()[0]; // Image initialization BufferedImage imageUpperLeft = new BufferedImage(WIDTH, HEIGHT, TYPE_BYTE_BINARY); Graphics2D g = imageUpperLeft.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, WIDTH/2, HEIGHT/2); g.dispose(); BufferedImage imageLowerRight = new BufferedImage(WIDTH, HEIGHT, TYPE_BYTE_BINARY); g = imageLowerRight.createGraphics(); g.setColor(Color.WHITE); g.fillRect(WIDTH/2, HEIGHT/2, WIDTH/2, HEIGHT/2); g.dispose(); TiledImage[] images = new TiledImage[] { new TiledImage(imageUpperLeft, NUM_TILES_XY, NUM_TILES_XY), new TiledImage(imageUpperLeft, NUM_TILES_XY, NUM_TILES_XY), new TiledImage(imageLowerRight, NUM_TILES_XY, NUM_TILES_XY), new TiledImage(imageLowerRight, NUM_TILES_XY, NUM_TILES_XY) }; // File initialization File file = File.createTempFile("temp", "." + suffix); file.deleteOnExit(); FileOutputStream fos = new SkipWriteOnAbortOutputStream(file); ImageOutputStream ios = ImageIO.createImageOutputStream(fos); writer.setOutput(ios); writer.addIIOWriteProgressListener(this); writer.prepareWriteSequence(null); boolean[] abortions = new boolean[] {true, false, true, false}; for (int i = 0; i < 4; i++) { abortFlag = abortions[i]; isAbortCalled = false; isCompleteCalled = false; isProgressCalled = false; isStartedCalled = false; TiledImage image = images[i]; if (abortFlag) { // This write will be aborted, and file will not be touched writer.writeToSequence(new IIOImage(image, null, null), null); if (!isStartedCalled) { throw new RuntimeException("Started should be called"); } if (!isProgressCalled) { throw new RuntimeException("Progress should be called"); } if (!isAbortCalled) { throw new RuntimeException("Abort should be called"); } if (isCompleteCalled) { throw new RuntimeException("Complete should not be called"); } } else { // This write should be completed successfully and the file should // contain correct image data. writer.writeToSequence(new IIOImage(image, null, null), null); if (!isStartedCalled) { throw new RuntimeException("Started should be called"); } if (!isProgressCalled) { throw new RuntimeException("Progress should be called"); } if (isAbortCalled) { throw new RuntimeException("Abort should not be called"); } if (!isCompleteCalled) { throw new RuntimeException("Complete should be called"); } } } writer.endWriteSequence(); writer.dispose(); ios.close(); // Validates content of the file. ImageReader reader = ImageIO.getImageReader(writer); ImageInputStream iis = ImageIO.createImageInputStream(file); reader.setInput(iis); for (int i = 0; i < 2; i++) { System.out.println("Testing image " + i); BufferedImage imageRead = reader.read(i); BufferedImage imageWrite = images[2 * i].getAsBufferedImage(); for (int x = 0; x < WIDTH; ++x) { for (int y = 0; y < HEIGHT; ++y) { if (imageRead.getRGB(x, y) != imageWrite.getRGB(x, y)) { throw new RuntimeException("Test failed for image " + i); } } } } }