0

I have a java program to capture screenshot on Windows. Here is part of the code. It is generating a black image when wrapped by YAJSW and run as a windows service.

BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(image, "png", file.toFile());

I know this is due to some limitation of windows service. But is there any way to make it work?

1 Answers1

0

I found another post on the same topic How to take a screenshot in Java?

Try the following code from the second answer : If this doesnt work, it would exclude the possibility, that one hidden part of your code is responsible for the bug.

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


public class screen2image
{
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd hh mm ss a");

    public void robo() throws Exception
    {
        Calendar now = Calendar.getInstance();
        Robot robot = new Robot();
        BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(screenShot, "JPG", new File("d:\\"+formatter.format(now.getTime())+".jpg"));
        System.out.println(formatter.format(now.getTime()));
    }

    public static void main(String[] args) throws Exception
    {
        screen2image s2i = new screen2image();
        while(1==1)
        {
            s2i.robo();
            Thread.sleep(10000);
        }
    }
}

Hope it helps you ! If this doesnt work, you should probably check your Windows/Java installation...

Community
  • 1
  • 1
Luatic
  • 8,513
  • 2
  • 13
  • 34