2015-04-07 77 views
7

我想在我的MacOS中截取某些應用程序的屏幕截圖,即使在另一個虛擬屏幕上而不是在活動屏幕中。MacOS中某些應用程序的Java屏幕截圖

我可以用下面的代碼捕獲活動屏幕,但是如何捕獲給定的應用程序?

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

public class Screenshot { 
    public static void main(String args[]) throws AWTException, IOException { 
     while(true) { 
      String timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date()); 

      BufferedImage screencapture = new Robot().createScreenCapture(
       new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()) 
      ); 

      // Save as JPEG 
      File file = new File("./screens/screencapture" + timeStamp + ".jpg"); 
      ImageIO.write(screencapture, "jpg", file); 

      try { 
       Thread.sleep(1000); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

你如何識別你想要捕獲的窗口? – MadProgrammer

+0

@MadProgrammer不知道,可以在代碼中修復應用程序 –

回答

5

嘗試使用/ usr/sbin目錄/抓屏,並與

Runtime.getRuntime().exec("/usr/sbin/screencapture"); 

這裏調用它是抓屏的用法輸出

usage: screencapture [-icMPmwsWxSCUtoa] [files] 
    -c   force screen capture to go to the clipboard 
    -C   capture the cursor as well as the screen. only in non-interactive modes 
    -d   display errors to the user graphically 
    -i   capture screen interactively, by selection or window 
       control key - causes screen shot to go to clipboard 
       space key - toggle between mouse selection and 
          window selection modes 
       escape key - cancels interactive screen shot 
    -m   only capture the main monitor, undefined if -i is set 
    -M   screen capture output will go to a new Mail message 
    -o   in window capture mode, do not capture the shadow of the window 
    -P   screen capture output will open in Preview 
    -s   only allow mouse selection mode 
    -S   in window capture mode, capture the screen not the window 
    -t<format> image format to create, default is png (other options include pdf, jpg, tiff and other formats) 
    -T<seconds> Take the picture after a delay of <seconds>, default is 5 
    -w   only allow window selection mode 
    -W   start interaction in window selection mode 
    -x   do not play sounds 
    -a   do not include windows attached to selected windows 
    -r   do not add dpi meta data to image 
    -l<windowid> capture this windowsid 
    -R<x,y,w,h> capture screen rect 
    files where to save the screen capture, 1 file per screen 

我想與窗口ID -w選項是一個使用。但要獲得窗口ID,您可能需要另一個實用程序。一個這樣的實用程序是pyscreencapture。 否則,我確定使用googling如何獲得窗口ID將導致更多的方式來獲得您需要的窗口ID。

雖然我不完全確定您的虛擬屏幕是什麼,並且您可能在獲取窗口ID或捕獲屏幕時遇到問題。

+0

虛擬桌面:http://www.howtogeek.com/180677/mission-control-101-how-to-use-multiple-desktops-on-a-mac/ –

+0

啊,我知道虛擬桌面。對虛擬屏幕這個詞不太確定。以爲這是一個與虛擬機上運行的應用程序與Mac作爲來賓操作系統。 – Sanj

2

「不幸的是」你需要JNI。基本上你需要訪問Cocoa的一些CoreGraphics和ImageIO API。算法如下:

  1. 調用CGWindowListCopyWindowInfo()獲取有關係統中所有窗口的信息。
  2. 提取物,你有興趣進入windows,最好的辦法是通過業主PID做到這一點,因爲這個信息是通過上述功能
  3. 返回調用CGWindowListCreateImage創建ImageRef()
  4. 要麼獲得上述ImageRef的數據,或通過CGImageDestinationRef函數將其保存到文件中。

由於Java不能直接調用Cocoa函數,因此需要Java和Cocoa之間的橋樑,這是一個利用JNI並允許Java代碼實現ObjectiveC實現函數的橋樑。

您的問題帶來了相當大的挑戰,因此我開始了一個github(https://github.com/cristik/cocoa4java)的項目,該項目提供了拍攝應用程序窗口快照所需的功能。我想你會在那裏找到你需要的一切,找到與進程相關的窗口,並檢索這些窗口的快照。還有一個小例子,它利用這些功能來幫助人們開始使用圖書館。

+0

'JNI,CoreGraphics,ImageIO,Cocoa' - 真的嗎?就像我不知道其中的任何一個,我想大量閱讀:-)謝謝你的回購;但我通常不喜歡JNI。這並不是說它是錯的 - 它完成了工作,但仍然是 –

+0

好吧,不確定是否可以在沒有JNI的情況下完成,除非Java將提供對Cocoa(ObjectiveC Mac API)函數的訪問。關於我列舉的框架,除非你想切換到ObjectiveC,否則你不需要閱讀它們,因爲這些框架是本地的:) – Cristik