在Window 8 OS中捕获截图

时间:2014-04-22 05:32:28

标签: java windows-8 awt robot

要在我的java应用程序中捕获屏幕截图,我已编写以下代码

Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());

BufferedImage capture = new Robot().createScreenCapture(screenRect);

ImageIO.write(capture, "png", new File("resources/img/screenshot.png"));

这是成功运行并捕获屏幕截图但这在Windows 8操作系统中不起作用。是否有其他人遇到过这类问题并获得解决方案?

2 个答案:

答案 0 :(得分:1)

  

我的应用程序安装到程序文件文件夹中,而Windows 8没有给出写入权限,我现在可以在那里写一下吗?

在那里写吧!操作系统制造商以及Sun / Oracle多年来一直在说 将文件写入应用程序的安装目录。编写它们不仅是一个错误的地方,而且正如您所发现的那样,它不会为典型的Java应用程序提供写入权限。

而是将屏幕截图放在user.home中,例如如this answer中所示。

答案 1 :(得分:0)

您无需使用以下代码在本地计算机中写入文件

即可
 ByteArrayOutputStream bos=new ByteArrayOutputStream();
    byte[] imageByte = null;
   try
   {
        //To Get the the size of the screen.
        Rectangle screenRect = new Rectangle(Toolkit
                .getDefaultToolkit().getScreenSize());          

        //To Store the scrreen shot in buffer.
        BufferedImage capture = new Robot()
                .createScreenCapture(screenRect);   

        //To Save the image on specific location.
        ImageIO.write(capture, "png",bos);
        bos.flush();
        imageByte=bos.toByteArray();
        bos.close();

         // File file = new File("resources/img/screenshot.png");
        MultipartEntity mpEntity = new MultipartEntity();
      //  ContentBody cfBody = new FileBody(file);
          ContentBody cfBody = new ByteArrayBody(imageByte,"screenshot.png");
        mpEntity.addPart("screenshot", cfBody);

}

直接将图像数组发送到图像