A Better Movie-to-Java Image Converter

The code shown in Section 5.3 is error-prone and nasty. On the other hand, a QTImageProducer is available from the GraphicsImporterDrawer. It does not have to work with a moving target like the MoviePlayer does. If only you could use that one instead . . . .

How do I do that?

The example program ConvertToJavaImageBetter has a different implementation of the grabMovieImage( ) method, as shown in Example 5-3.

Note

Run this example with ant run-ch05-convert-tojava-imagebetter.

Example 5-3. In-memory pict import to use GraphicsImporterDrawer’s QTImageProducer

 public void grabMovieImage( ) { 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); // 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); // 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); ...

Get QuickTime for Java: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.