我正在截取当前屏幕,然后保存图像。我想打开那个图像,并能够选择一个特定元素的框或者我想要的图片是什么,并且能够将那个较小的选择图像保存到 一份文件。请帮忙。
RemoteControlConfiguration config = new RemoteControlConfiguration(); config.setPort(4447);
SeleniumServer server = new SeleniumServer(config);
try{
// TODO Auto-generated method stub
server.start();
DefaultSelenium selenium = new DefaultSelenium("localhost", 4447, "*firefox", "http://www.google.com/");
selenium.start();
selenium.open("http://www.google.com/");
selenium.waitForPageToLoad("10000");
selenium.windowMaximize();
BufferedImage image1 = Screenshot("screen1.jpg");
//selenium.type("q", "Hello world");
Thread.sleep(2000);
BufferedImage image2 = Screenshot("screen2.jpg");
public static BufferedImage Screenshot(String fileName) throws Exception
{
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
File file = new File(fileName);
ImageIO.write(image, "jpg", file);
return image;
}
答案 0 :(得分:2)
假设您知道新边界的坐标,请使用新大小创建新的BufferedImage,为新图像创建图形对象,并在此图形对象上绘制大图像,指定x,y的负值。源图像大于目标,因此只会写入目标中的位。然后使用ImageIO.write()
保存较小的一个修改强> 感谢Andrew Thompson建议使用subImage
BufferedImage image1 = Screenshot("screen1.jpg");
BufferedImage subImage = image1.getSubImage(x, y, width, height);