因此,在网上长时间搜索后,我想出了以下代码来拍摄jpeg
屏幕截图,然后将其转换为mov
:
public class ScreenshotTests {
public static int counter = 1;
public static void main(String[] args) throws IOException, AWTException, InterruptedException {
ActionListener taskPerformer = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage image = robot.createScreenCapture(rectangle);
try {
ImageIO.write(image, "jpg", new File("images/test" + counter + ".jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
counter++;
}
};
Timer timer = new Timer(0, taskPerformer);
timer.setRepeats(true);
timer.start();
Thread.sleep(5000);
timer.stop();
Vector<String> imgLst = new Vector<String>();
for (int i = 0; i < new File("images").list().length; i++) {
imgLst.add("images/test" + i + 1 + ".jpg");
}
JpegImagesToMovie imageToMovie = new JpegImagesToMovie();
MediaLocator oml;
if ((oml = imageToMovie.createMediaLocator("test1.mov")) == null) {
System.err.println("Cannot build media locator from: " + "test1.mov");
System.exit(0);
}
int interval = 50;
imageToMovie.doIt(1920, 1080, (1000 / interval), imgLst, oml);
}
}
而且运行正常,没有任何错误。但是,虽然我确实在image
文件夹中获得了几十个图像,但视频仅包含最后一个图像(视频的长度为零秒)。怎么了?