1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| public class FrameGrabber {
public static void main(String[] args) throws Exception {
VideoFormat vidformat = new VideoFormat(VideoFormat.YUV);
Vector devices = CaptureDeviceManager.getDeviceList(vidformat);
CaptureDeviceInfo di = (CaptureDeviceInfo) devices.firstElement();
Processor p = Manager.createProcessor(di.getLocator());
p.configure();
while (p.getState() != Processor.Configured) Thread.sleep(10);
p.realize();
while (p.getState() != Processor.Realized)Thread.sleep(10);
PushBufferDataSource source = (PushBufferDataSource) p.getDataOutput();
PushBufferStream[] pstreams = source.getStreams();
Format streamFormat = pstreams[0].getFormat();
BufferToImage bti = new BufferToImage((VideoFormat)streamFormat);
p.start();
System.out.println("Processor started.");
int delay = Integer.parseInt(args[2]);
int imgCount = Integer.parseInt(args[1]);
for (int i = 0; i < imgCount; ++i) {
Buffer b = new Buffer();
pstreams[0].read(b);
Image img = bti.createImage(b);
ImageIO.write((RenderedImage) img, "jpeg", new File(args[0] + i + ".jpg"));
System.out.println("image "+ i +" saved.");
Thread.sleep(delay);
}
System.out.println("done.");
p.close();
System.exit(0);
}
} |
Partager