FileDocCategorySizeDatePackage
ConvertToJavaImageBetter.javaAPI DocExample6196Wed Nov 10 12:38:04 GMT 2004com.oreilly.qtjnotebook.ch05

ConvertToJavaImageBetter

public class ConvertToJavaImageBetter extends Frame implements ActionListener

Fields Summary
Movie
movie
MovieController
controller
QTComponent
qtc
GraphicsImporter
gi
GraphicsImporterDrawer
gid
static int
nextFrameX
static int
nextFrameY
Constructors Summary
public ConvertToJavaImageBetter()

        super ("QuickTime Movie");
        try {
            // get movie
            QTSessionCheck.check();
            QTFile file =
                QTFile.standardGetFilePreview (QTFile.kStandardQTFileTypes);
            OpenMovieFile omFile = OpenMovieFile.asRead(file);
            movie = Movie.fromFile(omFile);
            controller = new MovieController (movie);
            // build gui
            qtc = QTFactory.makeQTComponent (controller);
            Component c = qtc.asComponent();
            setLayout (new BorderLayout());
            add (c, BorderLayout.CENTER);
            Button imageButton = new Button ("Make Java Image");
            add (imageButton, BorderLayout.SOUTH);
            imageButton.addActionListener (this);
            // set up graphicsimporter
            gi = new GraphicsImporter (StdQTConstants.kQTFileTypePicture);
            gid = new GraphicsImporterDrawer (gi);
            // set up close-to-quit
            addWindowListener (new WindowAdapter() {
                    public void windowClosing (WindowEvent we) {
                        System.exit(0);
                    }
                });
        } catch (QTException qte) {
            qte.printStackTrace();
        }
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        grabMovieImage();
    
public voidgrabMovieImage()

        try {
            // stop movie to take picture
            boolean wasPlaying = false;
            if (movie.getRate() > 0) {
                movie.stop();
                wasPlaying = true;
            }

            // take a pict
            Pict pict = movie.getPict (movie.getTime());
            
            // add 512-byte header that pict would have as file
            byte[] newPictBytes =
                new byte [pict.getSize() + 512];
            pict.copyToArray (0,
                              newPictBytes,
                              512,
                              newPictBytes.length - 512);
            pict = new Pict (newPictBytes);
            
            /*
            // debug to disk
            try {
                FileOutputStream out =
                    new FileOutputStream (new File ("newpict.pict"));
                out.write (newPictBytes, 0, newPictBytes.length);
                out.flush();
                out.close();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            */

            // export it
            DataRef ref = new DataRef (pict,
                                      StdQTConstants.kDataRefQTFileTypeTag,
                                      "PICT");
            gi.setDataReference (ref);
            QDRect rect = gi.getSourceRect ();
            Dimension dim = new Dimension (rect.getWidth(),
                                           rect.getHeight());
            QTImageProducer ip = new QTImageProducer (gid, dim);
            
            /*
            // sanity check by getting a qtcomponent
            QTComponent giqtc = QTFactory.makeQTComponent (gi);
            Component gic = giqtc.asComponent();
            Frame f = new Frame ("GraphicsImporter");
            f.setLayout (new BorderLayout());
            f.add (gic);
            f.pack();
            f.setVisible(true);
            */

            // convert from MoviePlayer to java.awt.Image
            Image image = Toolkit.getDefaultToolkit().createImage (ip);
            // make a swing icon out of it and show it in a frame
            ImageIcon icon = new ImageIcon (image);
            JLabel label = new JLabel (icon);
            JFrame frame = new JFrame ("Java image");
            frame.getContentPane().add(label);
            frame.pack();
            frame.setLocation (nextFrameX += 10,
                               nextFrameY += 10);
            frame.setVisible(true);
            
            // restart movie
            if (wasPlaying)
                movie.start();
        } catch (QTException qte) {
            qte.printStackTrace();
        }
    
public static voidmain(java.lang.String[] arrImAPirate)

        ConvertToJavaImageBetter ctji =
            new ConvertToJavaImageBetter();
        ctji.pack();
        ctji.setVisible(true);
        Rectangle ctjiBounds = ctji.getBounds();
        nextFrameX = ctjiBounds.x + ctjiBounds.width;
        nextFrameY = ctjiBounds.y + ctjiBounds.height;