FileDocCategorySizeDatePackage
VideoCaptureToDisk.javaAPI DocExample5198Wed Nov 10 12:53:54 GMT 2004com.oreilly.qtjnotebook.ch06

VideoCaptureToDisk

public class VideoCaptureToDisk extends Frame implements ActionListener

Fields Summary
SequenceGrabber
grabber
SGVideoChannel
videoChannel
QDGraphics
gw
QDRect
grabBounds
boolean
grabbing
Button
stopButton
QTFile
grabFile
Constructors Summary
public VideoCaptureToDisk()

        super ("Video Capture");
        QTSessionCheck.check();
        setLayout (new GridLayout (2, 1));
        add (new Label ("Capturing video..."));
        stopButton = new Button ("Stop");
        stopButton.addActionListener (this);
        add (stopButton);
        setUpVideoGrab();
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        if (e.getSource() == stopButton) {
            System.out.println ("Stop grabbing");
            try {
                grabbing = false;
                if (grabber != null) {
                    grabber.stop();
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                System.exit (0);
            }
        }
    
public static voidmain(java.lang.String[] args)

        try {
            Frame f = new VideoCaptureToDisk();
            f.pack();
            f.setVisible(true);
        } catch (QTException qte) {
            qte.printStackTrace();
        }
    
protected voidsetUpVideoGrab()

        grabber = new SequenceGrabber();
        System.out.println ("got grabber");

        // force an offscreen gworld
        grabBounds = new QDRect (320, 240);
        gw = new QDGraphics (grabBounds);
        grabber.setGWorld (gw, null);

        // get videoChannel and set its bounds
        videoChannel = new SGVideoChannel (grabber);
        System.out.println ("Got SGVideoChannel");
        videoChannel.setBounds (grabBounds);

        /*
        // get settings
        // yikes! this crashes java 1.4.2 on mac os x!
        videoChannel.settingsDialog();
        */
        // kludgey - should do a wait()/notify() block instead
        final SGVideoChannel vc = videoChannel;
        Thread t = new Thread() {
                public void run() {
                    try {
                        vc.settingsDialog();
                    } catch (QTException qte) {
                        qte.printStackTrace();
                    }
                }
            };
        t.start();
        while (t.isAlive())
            Thread.yield();

        // prepare and start previewing
        // note - second prepare arg should seemingly be false,
        // but if it is, you get erroneous dskFulErr's
        videoChannel.setUsage (StdQTConstants.seqGrabRecord);
        grabber.prepare(false, true);
        // TODO: do I still want this?
        grabber.startPreview();

        // create output file
        grabFile = new QTFile (new java.io.File ("videograb.mov"));
        grabber.setDataOutput(grabFile,
                              StdQTConstants.seqGrabToDisk 
                              //seqGrabDontAddMovieResource);
                              );
        grabber.startRecord();

        grabbing = true;

        // set up thread to idle
        ActionListener timerCallback =
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (grabbing) {
                        try {
                            System.out.println ("idle");
                            grabber.idle();
                            grabber.update(null);
                        } catch (QTException qte) {
                            qte.printStackTrace();
                        }
                    }
                }
            };
        Timer timer = new Timer (50, timerCallback);
        timer.start();