簡體   English   中英

Window 8 OS 中的屏幕截圖

[英]Screenshot capture in Window 8 OS

為了在我的 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 操作系統中不起作用。 還有誰遇到過這種類型的問題並獲得解決方案?

我的應用程序安裝到程序文件夾中,而 Windows 8 不允許在那里寫入,我現在如何寫入?

不要把它寫在那里! 操作系統制造商以及 Sun/Oracle 多年來一直在說不要將文件寫入應用程序的安裝目錄。 它不僅是編寫它們的錯誤位置,而且正如您發現的那樣,它不為典型的 Java 應用程序提供寫入權限。

而是將屏幕截圖放在user.home例如在這個答案中看到的。

您可以使用以下代碼在本地計算機中不寫入文件的情況下執行此操作

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

}

直接發送字節數組代替圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM