Screenshot capture in Window 8 OS

半世苍凉 提交于 2019-12-12 06:45:48

问题


To capture screen shot in my java application i have write following code

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

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

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

This is working successfully and capture screen shot but this is not working in windows 8 operating system. any one else who have face this type of problem and get soluction?


回答1:


my application is install into the program file folder and the windows 8 not give permission to write there how i can write there now?

Do not write it there! OS manufacturers as well as Sun/Oracle have been saying for years not to write files to the application's installation directory. It is not only the wrong place to write them, but as you have discovered, does not provide write permissions for a typical Java app.

Instead put the screen-shot in user.home e.g. as seen in this answer.




回答2:


you can do that without writing file in your local machine by using the following code

 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);

}

send direct the byte array in plase of the image



来源:https://stackoverflow.com/questions/23211133/screenshot-capture-in-window-8-os

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!