Java Windows8操作系统中的屏幕截图

Java Windows8操作系统中的屏幕截图,java,windows-8,awt,robot,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

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

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

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

ImageIO.write(capture, "png", new File("resources/img/screenshot.png"));
 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);
此功能正在成功运行并捕获屏幕快照,但在windows 8操作系统中不起作用。还有谁曾经面对过这类问题并得到解决

我的应用程序已安装到程序文件文件夹中,windows 8未授予在其中写入的权限我现在如何在那里写入

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


相反,将屏幕截图放在
user.home
中,如中所示。

您可以使用以下代码在本地计算机中执行此操作,而无需写入文件

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

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

ImageIO.write(capture, "png", new File("resources/img/screenshot.png"));
 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);
}


直接发送图像中的字节数组

对我来说,它工作得很好(Windows 8.1 Update 1 64位)。不过,为什么要使用bmp格式并保存在.png中?我已经找到了原始问题。我的应用程序已安装到程序文件文件夹中,windows 8未授予在其中写入的权限我现在如何在那里写入?