我正在使用Java ROBOT进行屏幕捕获。但它将图像保存为纯黑色屏幕。我不明白我正在使用的代码有什么问题。任何人都可以知道我面临的问题
这是代码 -
//生成异常的屏幕截图
File tempFile = null;
try {
BufferedImage img = getScreenAsBufferedImage();
tempFile = createTempFile();
ImageIO.write(img, "jpg", tempFile);
template.setScreenshot(tempFile);
} catch (Exception e1) {
// error creating screenshot
log.error("Error creating image screenshot ", e1);
}
private BufferedImage getScreenAsBufferedImage() {
BufferedImage img = null;
try {
Robot r;
r = new Robot();
Toolkit t = Toolkit.getDefaultToolkit();
Rectangle rect = new Rectangle(t.getScreenSize());
img = r.createScreenCapture(rect);
} catch (AWTException e) {
e.printStackTrace();
}
return img;
}
private static File createTempFile() {
try {
// create a temp file
File temp = File.createTempFile("temp-file-name", ".jpg");
// log.info("Temp file : " + temp.getAbsolutePath());
return temp;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
HybridTemplate.java
public File getScreenshot() {
return screenshot;
}
public void setScreenshot(File screenshot) {
this.screenshot = screenshot;
}
答案 0 :(得分:0)
尝试使用此代码,删除一行后它可以正常工作..检查下面的注释行
File tempFile = null;
try {
BufferedImage img = getScreenAsBufferedImage();
tempFile = createTempFile();
ImageIO.write(img, "jpg", tempFile);
//template.setScreenshot(tempFile); //problem in this line
} catch (Exception e1) {
// error creating screenshot
//log.error("Error creating image screenshot ", e1);
}
private BufferedImage getScreenAsBufferedImage() {
BufferedImage img = null;
try {
Robot r;
r = new Robot();
Toolkit t = Toolkit.getDefaultToolkit();
Rectangle rect = new Rectangle(t.getScreenSize());
img = r.createScreenCapture(rect);
} catch (AWTException e) {
e.printStackTrace();
}
return img;
}
private static File createTempFile() {
try {
// create a temp file
File temp = File.createTempFile("temp-file-name", ".jpg");
System.out.println(temp.getAbsolutePath());
//log.info("Temp file : " + temp.getAbsolutePath());
return temp;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
答案 1 :(得分:0)
使用Chrome驱动程序的Selenium Robot
public static void main(String[] args) throws IOException, AWTException {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://searchsoa.techtarget.com/definition/stickiness");
//http://www.theedublogger.com/2012/11/08/how-to-add-a-link-to-a-specific-location-in-a-post-or-page/
driver.manage().window().maximize();
JavascriptExecutor jse = (JavascriptExecutor)driver;
Long clent_height = (Long) jse.executeScript("return window.document.documentElement.clientHeight");
// AWT Toolkit For Window
java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle winSize = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
int taskBarHeight = screenSize.height - winSize.height;
System.out.println("Screen : "+screenSize.height +","+screenSize.width);
int yaxis = (int) (screenSize.getHeight() - (taskBarHeight + clent_height.intValue()));
Robot robot = new Robot();
int x = 0, y = yaxis;
int width = (int) screenSize.getWidth(), height = (int) screenSize.getHeight();
Rectangle area = new Rectangle(x, y, width, height-taskBarHeight-y);
BufferedImage bufferedImage = robot.createScreenCapture(area);
ImageIO.write(bufferedImage, "jpg", new File("D:\\Yash\\Robot.jpg"));
driver.quit();
}