简体   繁体   English

如何使用包装为Windows Service的Java程序捕获屏幕截图

[英]How to capture screenshot using a java program wrapped as windows service

I have a java program to capture screenshot on Windows. 我有一个Java程序来捕获Windows上的屏幕截图。 Here is part of the code. 这是代码的一部分。 It is generating a black image when wrapped by YAJSW and run as a windows service. 当被YAJSW包裹并作为Windows服务运行时,它将生成黑色图像。

BufferedImage image = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
        ImageIO.write(image, "png", file.toFile());

I know this is due to some limitation of windows service. 我知道这是由于Windows服务的某些限制。 But is there any way to make it work? 但是有什么方法可以使它起作用吗?

I found another post on the same topic How to take a screenshot in Java? 我在同一主题上找到了另一篇文章如何用Java截屏?

Try the following code from the second answer : If this doesnt work, it would exclude the possibility, that one hidden part of your code is responsible for the bug. 请从第二个答案中尝试以下代码:如果不起作用,则将排除代码的一个隐藏部分导致该错误的可能性。

 import java.awt.Rectangle; import java.awt.Robot; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; public class screen2image { SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd hh mm ss a"); public void robo() throws Exception { Calendar now = Calendar.getInstance(); Robot robot = new Robot(); BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize())); ImageIO.write(screenShot, "JPG", new File("d:\\\\"+formatter.format(now.getTime())+".jpg")); System.out.println(formatter.format(now.getTime())); } public static void main(String[] args) throws Exception { screen2image s2i = new screen2image(); while(1==1) { s2i.robo(); Thread.sleep(10000); } } } 

Hope it helps you ! 希望对您有帮助! If this doesnt work, you should probably check your Windows/Java installation... 如果这不起作用,则可能应该检查Windows / Java安装...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium Java 使用 Krypton 捕获屏幕截图 - How to capture a screenshot using Krypton using Selenium Java Java程序作为Windows服务 - Java program as windows service 如何使用getDrawingCache捕获带阴影的屏幕截图? - How to capture screenshot with shadow using getDrawingCache? 使用htmlunitdriver捕获屏幕截图? - Capture screenshot using htmlunitdriver? 如何使用Java在Selenium2中捕获屏幕截图,需要在最小化浏览器时捕获带有其父窗口(浏览器)的弹出窗口 - How to capture Screenshot in Selenium2 using Java, need to capture popup window with its parent window(Browser) when browser is minimized windows java程序作为linux中的服务? - windows java program as a service in linux? 如何从Java程序内部运行PowerShell脚本作为Windows服务? - how to run a powershell script as a windows service from inside a Java program? 使用Allure在失败时捕获屏幕截图 - Using Allure to capture a screenshot on fail 如何使用扩展报告在Emulator / Real设备上捕获屏幕截图 - How to Capture screenshot on Emulator/Real device using extended report 如何使用awt.Robot以原始hdpi分辨率捕获屏幕截图? - How to capture a Screenshot in native hdpi resolution using awt.Robot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM