Example usage for javax.imageio ImageIO write

List of usage examples for javax.imageio ImageIO write

Introduction

In this page you can find the example usage for javax.imageio ImageIO write.

Prototype

public static boolean write(RenderedImage im, String formatName, OutputStream output) throws IOException 

Source Link

Document

Writes an image using an arbitrary ImageWriter that supports the given format to an OutputStream .

Usage

From source file:com.mgmtp.jfunk.core.util.ScreenCapturer.java

/**
 * Captures the specified rectangle and saves it as png to the specified file. The file must
 * have the extension {@code png}. Any necessary but non-existent parent directories of the
 * specified file are created.//from  w  w  w . j a  v a2s  .com
 * 
 * @param file
 *            the file to save the screenshot to
 * @param rectangle
 *            the rectangle to capture in screen coordinates
 */
public static void captureScreenRect(final File file, final Rectangle rectangle) {
    checkState(file.getName().endsWith("png"), "Screenshot file must have extension 'png': %s", file);
    LOGGER.trace("Creating screenshot: {}", file);

    try {
        createParentDirs(file);
        BufferedImage capture = new Robot().createScreenCapture(rectangle);
        ImageIO.write(capture, "png", file);
    } catch (AWTException ex) {
        throw new JFunkException("Error capturing screen", ex);
    } catch (IOException ex) {
        throw new JFunkException("Error saving screenshot", ex);
    }
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.canvas.rendering.AwtRenderingBackend.java

/**
 * {@inheritDoc}//w  w  w  . java2s .c o m
 */
public String encodeToString(final String type) throws IOException {
    try (final ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
        ImageIO.write(image_, type, bos);

        final byte[] imageBytes = bos.toByteArray();
        return new String(new Base64().encode(imageBytes));
    }
}

From source file:net.refractions.udig.catalog.wmsc.server.TileImageReadWriter.java

/**
 * Attempt to write the tile to file//from w  ww . j a  v  a  2  s. c  o  m
 * 
 * @param tile
 * @param filetype
 * @return true on success
 */
public boolean writeTile(Tile tile, String filetype) {
    try {
        // lock on the tile so we aren't trying to read it as it is being written to
        Object lock = tile.getTileLock();
        synchronized (lock) {
            // write the image out
            ImageIO.write(tile.getBufferedImage(), filetype, getTileFile(tile, filetype));
            // write the updated metadata file out
            writeTileMetadataFile(tile);
        }
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:visualize.Visualize.java

public static void makeScreenshot(final Rectangle rect, final int index) {
    try {//from  w w  w  .  ja  v a2s.c om
        BufferedImage image = new Robot().createScreenCapture(rect);
        ImageIO.write(image, "png", new File("screenshot_" + index + ".png"));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.scramcode.djinn.ui.panels.GraphPanel.java

/**
 * copy the visible part of the graph to a file as a jpeg image
 * @param file// ww w  . j a v a2s .  c  om
 */
public void writeJPEGImage(File file) {
    int width = visualizationViewer.getWidth();
    int height = visualizationViewer.getHeight();

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics2D graphics = bi.createGraphics();
    visualizationViewer.paint(graphics);
    graphics.dispose();

    try {
        ImageIO.write(bi, "jpeg", file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cpcc.core.utils.PngImageStreamResponseTest.java

@Test
public void shouldConvertBufferedImage() throws IOException {
    BufferedImage image = new BufferedImage(2, 2, BufferedImage.TYPE_INT_ARGB);
    Graphics gr = image.getGraphics();
    gr.setColor(Color.GREEN);/*from   w ww . ja v  a2s  . co  m*/
    gr.drawLine(0, 0, 1, 1);
    gr.setColor(Color.BLACK);
    gr.drawLine(1, 1, 0, 0);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(image, "PNG", baos);
    byte[] expected = baos.toByteArray();

    StreamResponse response = PngImageStreamResponse.convertImageToStreamResponse(image);

    assertThat(response.getStream()).isNotNull();
    byte[] actual = IOUtils.toByteArray(response.getStream());

    assertThat(actual).isEqualTo(expected);
}

From source file:be.ehb.restservermetdatabase.webservice.AchievementController.java

public String getImg(String url) {
    BufferedImage img = null;/*from   ww w .  j av  a  2 s  .co  m*/
    File f = null;
    try {
        f = new File(url);
    } catch (NullPointerException e) {
        f = new File("unnamed.png");
    }
    try {
        img = ImageIO.read(f);
    } catch (IOException e) {
    }
    String imageString = null;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        ImageIO.write(img, "png", bos);
        byte[] imageBytes = bos.toByteArray();
        imageString = new sun.misc.BASE64Encoder().encode(imageBytes);
        bos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return imageString;
}

From source file:cmsc105_mp2.Plot.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new File("Documents"));
    int retrival = chooser.showSaveDialog(null);
    if (retrival == JFileChooser.APPROVE_OPTION) {
        try {//from w  ww  . ja  v a 2 s  .  com
            ImageIO.write(bImage1, "png", new File(chooser.getSelectedFile() + ".png"));
        } catch (IOException ex) {
            System.out.println("Unable to Print!");
        }
    }
}

From source file:weka.core.ChartUtils.java

/**
 * Write a BufferedImage to a destination output stream as a png
 * /*from   w w  w.  ja  va2  s.co m*/
 * @param image the image to write
 * @param dest the destination output stream
 * @throws IOException if a problem occurs
 */
public static void writeImage(BufferedImage image, OutputStream dest) throws IOException {
    ImageIO.write(image, "png", dest);

    dest.flush();
    dest.close();
}

From source file:com.igormaznitsa.sciareto.ui.editors.PictureViewer.java

@Override
public boolean saveDocument() throws IOException {
    boolean result = false;
    final File docFile = this.title.getAssociatedFile();
    if (docFile != null) {
        final String ext = FilenameUtils.getExtension(docFile.getName()).trim().toLowerCase(Locale.ENGLISH);
        if (SUPPORTED_FORMATS.contains(ext)) {
            try {
                ImageIO.write(this.image, ext, docFile);
                result = true;//from   ww w. j  a  va  2  s . c  o  m
            } catch (Exception ex) {
                if (ex instanceof IOException) {
                    throw (IOException) ex;
                }
                throw new IOException("Can't write image", ex);
            }
        } else {
            try {
                LOGGER.warn("unsupported image format, will be saved as png : " + ext);
                ImageIO.write(this.image, "png", docFile);
                result = true;
            } catch (Exception ex) {
                if (ex instanceof IOException) {
                    throw (IOException) ex;
                }
                throw new IOException("Can't write image", ex);
            }
        }
    }
    return result;
}