Java Screen Capture screenShot(String format)

Here you can find the source of screenShot(String format)

Description

Stores a screenshot to the bynary array.

License

Open Source License

Declaration

public static byte[] screenShot(String format) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *
 *  * Copyright (C) 2007, 2010 - The University of Liverpool
 *  * This program is free software; you can redistribute it and/or modify it under the terms
 *  * of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License,
 *  * or (at your option) any later version.
 *  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *  * You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  */*from w w w.ja  v a 2s .  co m*/
 *  * Author: Fabio Corubolo
 *  * Email: corubolo@gmail.com
 * 
 *******************************************************************************/

import java.awt.AWTException;
import java.awt.Component;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    /** Stores a screenshot to the bynary array.
     * format: "jpg", "png",..
     * 
     * 
     * */
    public static byte[] screenShot(String format) {
        try {
            Robot robot = new Robot();
            Rectangle captureSize = new Rectangle(Toolkit
                    .getDefaultToolkit().getScreenSize());
            BufferedImage bufferedImage = robot
                    .createScreenCapture(captureSize);
            // robot.mouseMove(100,100);
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, format, bo);
            return bo.toByteArray();
        } catch (AWTException e) {
        } catch (IOException e) {
        }
        return null;

    }

    /** Stores a screenshot of the selected component to the binary array.
     * format: "jpg", "png",.. */
    public static byte[] screenShot(String format, Component c) {
        try {
            Robot robot = new Robot();
            // ImageWriter encoder =
            // ImageIO.getImageWritersByFormatName("JPEG").next();
            // JPEGImageWriteParam p = new JPEGImageWriteParam(null);
            // p.setOptimizeHuffmanTables(true);
            // p.setCompressionQuality(0.8f);
            // p.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
            // encoder.setOutput(out);
            // encoder.write((IIOMetadata) null, new IIOImage(image,null,null),
            // p);
            Rectangle captureSize = c.getBounds();

            BufferedImage bufferedImage = robot
                    .createScreenCapture(captureSize);
            // robot.mouseMove(100,100);
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, format, bo);
            return bo.toByteArray();
        } catch (AWTException e) {
        } catch (IOException e) {
        }
        return null;

    }
}

Related

  1. captureScreen()
  2. captureScreen(final Rectangle screenRect)
  3. captureScreen(Rectangle rect)
  4. captureScreen(String filename)
  5. screenshot(Rectangle dims, Robot r)
  6. takeScreenShot(boolean showCursor)
  7. takeScreenshot(String fileName, String formatName)