Implement screen capture Remote Desktop control using JAVA

Source: Internet
Author: User

We mainly use the java. util. Robot class to capture the screen, which can capture a rectangular area of the screen. Through this class, we can also implement a remote desktop control program.

Package com. qiu. util;

Import java. io .*;

Import java.net .*;

Import javax. swing .*;

Import java. awt .*;

Import java. awt. event .*;

Import java. awt. image .*;

Import javax. imageio .*;

Import java. io .*;

/** @ Author Qiu_BaiChao

* A simple screen capture

*

**/

Public class ScreenCapture {

// Test main

Public static void main (String [] args) throws Exception {

String userdir = System. getProperty ("user. dir ");

File tempFile = new File ("d:", "temp.png ");

ScreenCapture capture = ScreenCapture. getInstance ();

Capture. captureImage ();

JFrame frame = new JFrame ();

JPanel panel = new JPanel ();

Panel. setLayout (new BorderLayout ());

JLabel imagebox = new JLabel ();

Panel. add (BorderLayout. CENTER, imagebox );

Imagebox. setIcon (capture. getPickedIcon ());

Capture. saveToFile (tempFile );

Capture. captureImage ();

Imagebox. setIcon (capture. getPickedIcon ());

Frame. setContentPane (panel );

Frame. setSize (400,300 );

Frame. show ();

System. out. println ("Over ");

}

Private ScreenCapture (){

Try {

Robot = new Robot ();

}

Catch (AWTException e ){

System. err. println ("Internal Error:" + e );

E. printStackTrace ();

}

JPanel cp = (JPanel) dialog. getContentPane ();

Cp. setLayout (new BorderLayout ());

LabFullScreenImage. addMouseListener (new MouseAdapter (){

Public void mouseReleased (MouseEvent evn ){

IsFirstPoint = true;

PickedImage = fullScreenImage. getSubimage (recX, recY, recW, recH );

Dialog. setVisible (false );

}

});

LabFullScreenImage. addMouseMotionListener (new MouseMotionAdapter (){

Public void mouseDragged (MouseEvent evn ){

If (isFirstPoint ){

X1 = evn. getX ();

Y1 = evn. getY ();

IsFirstPoint = false;

}

Else {

X2 = evn. getX ();

Y2 = evn. getY ();

Int maxX = Math. max (x1, x2 );

Int maxY = Math. max (y1, y2 );

Int minX = Math. min (x1, x2 );

Int minY = Math. min (y1, y2 );

RecX = minX;

RecY = minY;

RecW = maxX-minX;

RecH = maxY-minY;

LabFullScreenImage. drawRectangle (recX, recY, recW, recH );

}

}

Public void mouseMoved (MouseEvent e ){

LabFullScreenImage. drawCross (e. getX (), e. getY ());

}

});

Cp. add (BorderLayout. CENTER, labFullScreenImage );

Dialog. setCursor (Cursor. getPredefinedCursor (Cursor. CROSSHAIR_CURSOR ));

Dialog. setAlwaysOnTop (true );

Dialog. setMaximumSize (

Toolkit. getdefatooltoolkit (). getScreenSize ());

Dialog. setUndecorated (true );

Dialog. setSize (dialog. getMaximumSize ());

Dialog. setModal (true );

}

// Singleton Pattern

Public static ScreenCapture getInstance (){

Return defacapcapturer;

}

/** Capture full screen */

Public Icon captureFullScreen (){


FullScreenImage = robot. createScreenCapture (new Rectangle (

Toolkit. getdefatooltoolkit (). getScreenSize ()));

ImageIcon icon = new ImageIcon (fullScreenImage );

Return icon;

}

/** Capture an orthopedic area of the screen

*/

Public void captureImage (){

FullScreenImage = robot. createScreenCapture (new Rectangle (

Toolkit. getdefatooltoolkit (). getScreenSize ()));

ImageIcon icon = new ImageIcon (fullScreenImage );

LabFullScreenImage. setIcon (icon );

Dialog. setVisible (true );

}

/** Obtain the captured BufferedImage */

Public BufferedImage getPickedImage (){

Return pickedImage;

}

/** Get the captured Icon */

Public ImageIcon getPickedIcon (){

Return new ImageIcon (getPickedImage ());

}

/** Save as a file in PNG format

* @ Deprecated

* Replaced by saveAsPNG (File file)

**/

@ Deprecated

Public void saveToFile (File file) throws IOException {

ImageIO. write (getPickedImage (), defaultImageFormater, file );

}

/** Save as a file in PNG format */

Public void saveAsPNG (File file) throws IOException {

ImageIO. write (getPickedImage (), "png", file );

}

/** Save as a JPEG image file */

Public void saveAsJPEG (File file) throws IOException {

ImageIO. write (getPickedImage (), "JPEG", file );

}

/** Write an OutputStream */

Public void write (OutputStream out) throws IOException {

ImageIO. write (getPickedImage (), defaultImageFormater, out );

}

// Singleton design pattern

Private static ScreenCapture defaultCapturer = new ScreenCapture ();

Private int x1, y1, x2, y2;

Private int recX, recY, recH, recW; // captured image

Private boolean isFirstPoint = true;

Private BackgroundImage labFullScreenImage = new BackgroundImage ();

Private Robot robot;

Private BufferedImage fullScreenImage;

Private BufferedImage pickedImage;

& Nb

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.