O'Reilly logo

QuickTime for Java: A Developer's Notebook by Chris Adamson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting a Screen Capture

One frequently useful source of image data is, unsurprisingly, the screen—or screens, if you’re so fortunate. Each screen is represented by an object that can give you its current contents, though it takes a little work to do anything with it.

How do I do that?

ScreenToPNG, shown in Example 5-5, is a headless application that starts up, grabs the screen, and writes out the image to a PNG file called screen.png.

Note

I use PNG for screenshots because it’s lossless, widely supported, compressed, and patent-unencumbered.

Example 5-5. Grabbing screen pixels

package com.oreilly.qtjnotebook.ch05; import quicktime.*; import quicktime.std.*; import quicktime.std.image.*; import quicktime.qd.*; import quicktime.io.*; import quicktime.util.*; import com.oreilly.qtjnotebook.ch01.QTSessionCheck; public class ScreenToPNG extends Object { public static void main (String[ ] args) { new ScreenToPNG( ); } public ScreenToPNG( ) { try { QTSessionCheck.check( ); GDevice gd = GDevice.getMain( ); System.out.println ("Got GDevice: " + gd); PixMap pm = gd.getPixMap( ); System.out.println ("Got PixMap: " + pm); ImageDescription id = new ImageDescription (pm); System.out.println ("Got ImageDescription: " + id); QDRect bounds = pm.getBounds( ); RawEncodedImage rei = pm.getPixelData( ); QDGraphics decompGW = new QDGraphics (id, 0); QTImage.decompress (rei, id, decompGW, bounds, 0); GraphicsExporter exporter = new GraphicsExporter (StdQTConstants4.kQTFileTypePNG); exporter.setInputPixmap ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required