Java Screen Capture takeScreenShot(boolean showCursor)

Here you can find the source of takeScreenShot(boolean showCursor)

Description

This function handle the sreenshot

License

Open Source License

Parameter

Parameter Description

Return

byte[] that contain the screenshot (jpeg)

Declaration

public static byte[] takeScreenShot(boolean showCursor) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.AWTException;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.MouseInfo;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

public class Main {
    /**/*from  w  w w. ja v a2s.c o m*/
     * This function handle the sreenshot
     * @param {@link Boolean} showCursor if the cursor in the screenshot need to be shown
     * @return byte[] that contain the screenshot (jpeg)
     */
    public static byte[] takeScreenShot(boolean showCursor) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedImage img = null;
        byte[] out = null;

        try {
            img = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

            if (showCursor) {
                Image cursor = ImageIO.read(new File("res/ratty/img/cursor.gif"));
                int x = MouseInfo.getPointerInfo().getLocation().x;
                int y = MouseInfo.getPointerInfo().getLocation().y;

                Graphics2D graphics2D = img.createGraphics();
                graphics2D.drawImage(cursor, x, y, 16, 16, null);
            }
            //TODO: Add multiple images compressions
            ImageIO.write(img, "jpg", baos);
            baos.flush();
            out = baos.toByteArray();
            baos.close();
        } catch (HeadlessException | AWTException | IOException e) {
            e.printStackTrace();
        }

        return out;
    }
}

Related

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