2015-04-07 46 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

相关问题