A simple Java-implemented screen capture (source code)
Last Update:2017-02-28
Source: Internet
Author: User
The source code is mainly used 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 caught after the bufferedimage*/
Public BufferedImage Getpickedimage () {
return pickedimage;
}
/** gets caught after the 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);
}
/** stored as a file for 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);
}
/** writes a outputstream*/
public void Write (OutputStream out) throws ioexception{
Imageio.write (Getpickedimage (), defaultimageformater,out);
}
Singleton design
private static Screencapture Defaultcapturer = new Screencapture ();
private int x1,y1,x2,y2;
private int RECX,RECY,RECH,RECW; Captured Images
Private Boolean isfirstpoint = true;
Private BackgroundImage labfullscreenimage = new BackgroundImage ();
Private Robot Robot;
Private BufferedImage fullscreenimage;
Private BufferedImage pickedimage;
Private String Defaultimageformater = "png";
Private JDialog Dialog = new JDialog ();
}
/** Displays the label*/of the picture
Class BackgroundImage extends jlabel{
public void Paintcomponent (Graphics g) {
Super.paintcomponent (g);
G.drawrect (X,Y,W,H);
String area = integer.tostring (w) + "*" + integer.tostring (h);
g.DrawString (area,x+ (int) w/2-15,y+ (int) H/2);
G.drawline (Linex,0,linex,getheight ());
G.drawline (0,liney,getwidth (), Liney);
}
public void DrawRectangle (int x,int y,int width,int height) {
this.x = x;
This.y = y;
h = height;
w = width;
Repaint ();
}
public void Drawcross (int x,int y) {
LineX = x;
Liney = y;
Repaint ();
}
int Linex,liney;
int x,y,h,w;
}