• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing the screen

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello ranchers,

I was wondering: is it possible to read the screen (not my application window, if it was swing)? I made some tests trying to use java.awt.Toolkit but nothing worked. As a multiplatform application, I suppose there may not be a way to read the screen as if it were an image, but I don't pay for asking, do I?
Oh, if I could just simulate the pressing of the PrintScreen key and parse the clipboard into an image it would probably yield the same results. Thanks in advance
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure... it's pretty easy... I once wanted to make a made a little applet that took screen captures and sent them back to the server...

This app grabs the screen, saves it to a file, and shows then displays it.

/*******************************************************************************

* Copyright (c) 2000, 2003 Stanford University School of Medicine

* http://med.stanford.edu/irt All rights reserved. Created: Jun 11, 2004

******************************************************************************/

import java.awt.AWTException;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.Robot;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.swing.ImageIcon;

import javax.swing.JButton;

import javax.swing.JFileChooser;

import javax.swing.JFrame;

import com.sun.image.codec.jpeg.ImageFormatException;

import com.sun.image.codec.jpeg.JPEGCodec;

import com.sun.image.codec.jpeg.JPEGImageEncoder;

/**

* @author Philip Constantinou ScreenShare TODO Create class documentation

*/

public class ScreenShare {

/**

*

*/

public ScreenShare() {

super();

// TODO Auto-generated constructor stub

}

public void captureScreen(Rectangle area, FileOutputStream stream)

throws AWTException, ImageFormatException, IOException {

Robot robot = new Robot();

BufferedImage capture = robot.createScreenCapture(area);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(stream);

encoder.encode(capture);

stream.flush();

stream.close();

}

public static void main(String[] args) {

try {

ScreenShare ss = new ScreenShare();

JFileChooser chooser = new JFileChooser();

chooser.showSaveDialog(null);

File file = chooser.getSelectedFile();

Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();

ss.captureScreen(new Rectangle(0, 0, dim.width, dim.height),

new FileOutputStream(file));

JFrame frame = new JFrame();

frame.setContentPane(new JButton(new ImageIcon(file.toURL())));

frame.pack();

frame.show();

frame.addWindowListener(new WindowAdapter() {

public void windowClosed(WindowEvent e) {

System.exit(1);

}

});

} catch (Exception e) {

e.printStackTrace();

}

}

}
 
reply
    Bookmark Topic Watch Topic
  • New Topic