屏幕截图功能的实现------Java篇

    屏幕截图功能程序在聊天软件与浏览器插件中运用最广泛,一般使用c#,vc+,

    now,现在介绍用Java实现屏幕截图效果,因为现在Java语言流行。

   代码敲击,如下:第一种方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
importjava.awt.Desktop;
importjava.awt.Dimension;
importjava.awt.Rectangle;
importjava.awt.Robot;
importjava.awt.Toolkit;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjavax.imageio.ImageIO;
publicclassCaptureScreen {
 publicstaticvoidcaptureScreen(String fileName, String folder)throwsException {
  Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
  Rectangle screenRectangle =newRectangle(screenSize);
  Robot robot =newRobot();
  BufferedImage image = robot.createScreenCapture(screenRectangle);
  //保存路径
  File screenFile =newFile(fileName);
  if(!screenFile.exists()) {
   screenFile.mkdir();
  }
  File f =newFile(screenFile, folder);
  ImageIO.write(image,"png", f);
  //自动打开
  if(Desktop.isDesktopSupported()
     && Desktop.getDesktop().isSupported(Desktop.Action.OPEN))
     Desktop.getDesktop().open(f);
 }
 publicstaticvoidmain(String[] args) {
  try{
   captureScreen("e:\\你好","11.png");
  }catch(Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
第二种方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
packagecom.qiu.util;
importjava.io.*;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.image.*;
importjavax.imageio.*;
/**
 * 一个简单的屏幕抓图
 *
 **/
publicclassScreenCapture {
  // test main
  publicstaticvoidmain(String[] args)throwsException {
    String userdir = System.getProperty("user.dir");
    File tempFile =newFile("d:","temp.png");
    ScreenCapture capture = ScreenCapture.getInstance();
    capture.captureImage();
    JFrame frame =newJFrame();
    JPanel panel =newJPanel();
    panel.setLayout(newBorderLayout());
    JLabel imagebox =newJLabel();
    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");
  }
  privateScreenCapture() {
    try{
      robot =newRobot();
    }catch(AWTException e) {
      System.err.println("Internal Error: "+ e);
      e.printStackTrace();
    }
    JPanel cp = (JPanel) dialog.getContentPane();
    cp.setLayout(newBorderLayout());
    labFullScreenImage.addMouseListener(newMouseAdapter() {
      publicvoidmouseReleased(MouseEvent evn) {
        isFirstPoint =true;
        pickedImage = fullScreenImage.getSubimage(recX, recY, recW,
            recH);
        dialog.setVisible(false);
      }
    });
    labFullScreenImage.addMouseMotionListener(newMouseMotionAdapter() {
      publicvoidmouseDragged(MouseEvent evn) {
        if(isFirstPoint) {
          x1 = evn.getX();
          y1 = evn.getY();
          isFirstPoint =false;
        }else{
          x2 = evn.getX();
          y2 = evn.getY();
          intmaxX = Math.max(x1, x2);
          intmaxY = Math.max(y1, y2);
          intminX = Math.min(x1, x2);
          intminY = Math.min(y1, y2);
          recX = minX;
          recY = minY;
          recW = maxX - minX;
          recH = maxY - minY;
          labFullScreenImage.drawRectangle(recX, recY, recW, recH);
        }
      }
      publicvoidmouseMoved(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
  publicstaticScreenCapture getInstance() {
    returndefaultCapturer;
  }
  /** 捕捉全屏慕 */
  publicIcon captureFullScreen() {
    fullScreenImage = robot.createScreenCapture(newRectangle(Toolkit
        .getDefaultToolkit().getScreenSize()));
    ImageIcon icon =newImageIcon(fullScreenImage);
    returnicon;
  }
  /** 捕捉屏幕的一个矫形区域 */
  publicvoidcaptureImage() {
    fullScreenImage = robot.createScreenCapture(newRectangle(Toolkit
        .getDefaultToolkit().getScreenSize()));
    ImageIcon icon =newImageIcon(fullScreenImage);
    labFullScreenImage.setIcon(icon);
    dialog.setVisible(true);
  }
  /** 得到捕捉后的BufferedImage */
  publicBufferedImage getPickedImage() {
    returnpickedImage;
  }
  /** 得到捕捉后的Icon */
  publicImageIcon getPickedIcon() {
    returnnewImageIcon(getPickedImage());
  }
  /**
   * 储存为一个文件,为PNG格式
   *
   * @deprecated replaced by saveAsPNG(File file)
   **/
  @Deprecated
  publicvoidsaveToFile(File file)throwsIOException {
    ImageIO.write(getPickedImage(), defaultImageFormater, file);
  }
  /** 储存为一个文件,为PNG格式 */
  publicvoidsaveAsPNG(File file)throwsIOException {
    ImageIO.write(getPickedImage(),"png", file);
  }
  /** 储存为一个JPEG格式图像文件 */
  publicvoidsaveAsJPEG(File file)throwsIOException {
    ImageIO.write(getPickedImage(),"JPEG", file);
  }
  /** 写入一个OutputStream */
  publicvoidwrite(OutputStream out)throwsIOException {
    ImageIO.write(getPickedImage(), defaultImageFormater, out);
  }
  // singleton design pattern
  privatestaticScreenCapture defaultCapturer =newScreenCapture();
  privateintx1, y1, x2, y2;
  privateintrecX, recY, recH, recW;// 截取的图像
  privatebooleanisFirstPoint =true;
  privateBackgroundImage labFullScreenImage =newBackgroundImage();
  privateRobot robot;
  privateBufferedImage fullScreenImage;
  privateBufferedImage pickedImage;
  privateString defaultImageFormater ="png";
  privateJDialog dialog =newJDialog();
}
/** 显示图片的Label */
classBackgroundImageextendsJLabel {
  publicvoidpaintComponent(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);
  }
  publicvoiddrawRectangle(intx,inty,intwidth,intheight) {
    this.x = x;
    this.y = y;
    h = height;
    w = width;
    repaint();
  }
  publicvoiddrawCross(intx,inty) {
    lineX = x;
    lineY = y;
    repaint();
  }
  intlineX, lineY;
  intx, y, h, w;
}

可以试图运行界面

。OK。

你可能感兴趣的:(屏幕截图功能的实现------Java篇)