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 ...
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.