Java Implementation screen capture Remote Desktop control

Source: Internet
Author: User
Tags deprecated gety min

The main use of the Java.util.Robot class to capture the screen, you can achieve a rectangular area of the screen capture, 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.getdefaulttoolkit (). Getscreensize ());


dialog.setundecorated (TRUE);


dialog.setsize (Dialog.getmaximumsize ());


Dialog.setmodal (TRUE);


}


//singleton Pattern


public static Screencapture getinstance () {


return defaultcapturer;


}


/** Capture Full Screen mu * * *


public Icon Capturefullscreen () {


Fullscreenimage = Robot.createscreencapture (New Rectangle (


Toolkit.getdefaulttoolkit (). Getscreensize ()));


ImageIcon icon = new ImageIcon (fullscreenimage);


return icon;


}


/** an orthopedic area that captures the screen


*/


public void Captureimage () {


fullscreenimage = robot.createscreencapture (New Rectangle (


Toolkit.getdefaulttoolkit (). Getscreensize ()));


ImageIcon icon = new ImageIcon (fullscreenimage);


Labfullscreenimage.seticon (icon);


dialog.setvisible (TRUE);


}


/** gets captured bufferedimage*/


public BufferedImage Getpickedimage () {


return pickedimage;


}


/** gets captured icon*/


public ImageIcon Getpickedicon () {


return new ImageIcon (Getpickedimage ());


}


/** is stored 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);


}


/** is stored as a file, in PNG format * *


public void saveaspng (file file) throws IOException {


Imageio.write (Getpickedimage (), "PNG", file);


}


/** stored as a JPEG format image file * *


public void saveasjpeg (file file) throws IOException {


Imageio.write (Getpickedimage (), "JPEG", file);


}

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.