[英]Programmatically take ScreenShot of Desktop in Ruby?
I asked this question about taking a picture of a webpage programmatically , and I've downloaded and got webkit2png working (taking pictures of HTML pages like blogs and whatnot). 我问过这个关于以编程方式拍摄网页图片的问题,我已经下载并使webkit2png正常工作(拍摄像博客和诸如此类的HTML页面的照片)。 So cool, thanks for showing me that!
太酷了,谢谢你告诉我的!
Now I would like to start doing more, like being able to take pictures of Flash websites after they have loaded, and of my desktop. 现在我想开始做更多事情,比如能够在加载Flash桌面和桌面后拍摄Flash网站。
Is it possible to take pictures of Flash websites with webkit2png (considering you might have to wait a few seconds for it to load)? 是否可以使用webkit2png拍摄Flash网站(考虑到你可能需要等待几秒才能加载)?
But the main question is, how do I programmatically take pictures of the desktop? 但主要的问题是,我如何以编程方式拍摄桌面照片? That would allow me to have much more control over what's going on.
这将使我能够更好地控制正在发生的事情。
您可以使用xwd(1)截取根窗口的屏幕截图:
xwd -display :0 -root|xwdtopnm |pnmtopng > $1
This is an awesome question! 这是一个很棒的问题!
A couple of years ago I has to work on a similar project. 几年前,我必须从事类似的项目。 I found a library, called watir , you can use to control system browsers from Ruby.
我发现了一个名为watir的库,可以用来控制Ruby的系统浏览器。 At the time I checked, it wasn't really reliable in a Linux environment, but right now it seems to be pretty solid.
在我检查的时候,它在Linux环境中并不真正可靠,但是现在它看起来非常可靠。
Here's a couple of links: 这里有几个链接:
I have never tried this solution so I would be really happy if you can write here a feedback if you decide to go with Watir. 我从未尝试过这个解决方案,所以如果你决定和Watir一起去,可以在这里写一个反馈,我会很高兴的。 All the examples targets a Windows server, I didn't found a valid tutorial using a Linux + Firefox environment.
所有示例都以Windows服务器为目标,我没有找到使用Linux + Firefox环境的有效教程。
You can use Watir WebDriver like this: 您可以像这样使用Watir WebDriver :
$ irb
irb(main):001:0> require 'watir-webdriver'
=> true
irb(main):002:0> browser = Watir::Browser.new
=> #<Watir::Browser:0x136da92fad77d562 url="about:blank" title="">
irb(main):003:0> browser.goto 'http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby'
=> "http://stackoverflow.com/questions/1733715/programmatically-take-screenshot-of-desktop-in-ruby"
irb(main):004:0> browser.screenshot.save 'screenshot.png'
=> #<File:screenshot.png (closed)>
irb(main):005:0>
which seems to do the job rather well! 这似乎做得很好! I tested this on my Mac, but I'd be surprised if it didn't work well on Linux too.
我在Mac上对此进行了测试,但如果在Linux上也不能正常工作,我会感到惊讶。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.