java程序

package com.qiu.util;
  2import java.io.*;
  3import java.net.*;
  4import javax.swing.*;
  5import java.awt.*;
  6import java.awt.event.*;
  7import java.awt.image.*;
  8import javax.imageio.*;
  9import java.io.*;
10
11
12
13/** *//**@Author Qiu_BaiChao
14 *一个简单的屏幕抓图
15 *
16 **/
17
18public class ScreenCapture {
19 //test main
20 public static void main(String[] args) throws Exception{
21  String userdir = System.getProperty("user.dir");
22  File tempFile = new File("d:","temp.png");
23  ScreenCapture capture = ScreenCapture.getInstance();
24  capture.captureImage(); 
25  JFrame frame = new JFrame();
26  JPanel panel = new JPanel();
27  panel.setLayout(new BorderLayout());
28  JLabel imagebox = new JLabel();
29  panel.add(BorderLayout.CENTER,imagebox); 
30  imagebox.setIcon(capture.getPickedIcon());
31  capture.saveToFile(tempFile);
32  capture.captureImage();
33  imagebox.setIcon(capture.getPickedIcon());
34  frame.setContentPane(panel);
35  frame.setSize(400,300);
36  frame.show();
37  System.out.println("Over");
38 }
39
40 private ScreenCapture() {
41 
42  try{
43   robot = new Robot();
44  }
45  catch(AWTException e) {
46   System.err.println("Internal Error: " + e);
47   e.printStackTrace();
48  } 
49  JPanel cp = (JPanel)dialog.getContentPane();
50  cp.setLayout(new BorderLayout()); 
51  labFullScreenImage.addMouseListener(new MouseAdapter() {
52    public void mouseReleased(MouseEvent evn) {
53      isFirstPoint = true;
54      pickedImage = fullScreenImage.getSubimage(recX,recY,recW,recH);
55      dialog.setVisible(false);    
56    }
57   });
58  
59  labFullScreenImage.addMouseMotionListener(new MouseMotionAdapter() {
60    public void mouseDragged(MouseEvent evn) {
61     if(isFirstPoint) {
62      x1 = evn.getX();
63      y1 = evn.getY();
64      isFirstPoint = false; 
65     }
66     else {
67      x2 = evn.getX();
68      y2 = evn.getY();
69      int maxX = Math.max(x1,x2);
70      int maxY = Math.max(y1,y2);
71      int minX = Math.min(x1,x2);
72      int minY = Math.min(y1,y2);
73      recX = minX;
74      recY = minY;
75      recW = maxX-minX;
76      recH = maxY-minY;
77      labFullScreenImage.drawRectangle(recX,recY,recW,recH);   
78     }
79    }
80   
81    public void mouseMoved(MouseEvent e) {
82     labFullScreenImage.drawCross(e.getX(),e.getY());
83    }
84   });
85  
86  cp.add(BorderLayout.CENTER,labFullScreenImage);
87  dialog.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
88  dialog.setAlwaysOnTop(true);
89  dialog.setMaximumSize(
90   Toolkit.getDefaultToolkit().getScreenSize());
91  dialog.setUndecorated(true);
92  dialog.setSize(dialog.getMaximumSize());
93  dialog.setModal(true);
94 }
95 //Singleton Pattern
96 public static ScreenCapture getInstance() {
97  return defaultCapturer;
98 }
99 /** *//**捕捉全屏慕*/
100 public Icon captureFullScreen() {
101  fullScreenImage = robot.createScreenCapture(new Rectangle(
102   Toolkit.getDefaultToolkit().getScreenSize()));
103  ImageIcon icon = new ImageIcon(fullScreenImage);
104  return icon;
105 }
106 /** *//**捕捉屏幕的一个矫形区域
107  */
108 public void captureImage() {
109  fullScreenImage = robot.createScreenCapture(new Rectangle(
110   Toolkit.getDefaultToolkit().getScreenSize()));
111  ImageIcon icon = new ImageIcon(fullScreenImage);
112  labFullScreenImage.setIcon(icon); 
113  dialog.setVisible(true);
114 }
115 /** *//**得到捕捉后的BufferedImage*/
116 public BufferedImage getPickedImage() {
117  return pickedImage;
118 }
119 /** *//**得到捕捉后的Icon*/
120 public ImageIcon getPickedIcon() {
121  return new ImageIcon(getPickedImage());
122 }
123 /** *//**储存为一个文件,为PNG格式
124  *@deprecated
125  *replaced by saveAsPNG(File file)
126  **/
127 @Deprecated
128 public void saveToFile(File file) throws IOException{
129  ImageIO.write(getPickedImage(),defaultImageFormater,file);
130 }
131 /** *//**储存为一个文件,为PNG格式*/
132 public void saveAsPNG(File file) throws IOException {
133  ImageIO.write(getPickedImage(),"png",file);
134 }
135 /** *//**储存为一个JPEG格式图像文件*/
136 public void saveAsJPEG(File file) throws IOException {
137  ImageIO.write(getPickedImage(),"JPEG",file);
138 }
139
140 /** *//**写入一个OutputStream*/
141 public void write(OutputStream out) throws IOException{
142  ImageIO.write(getPickedImage(),defaultImageFormater,out);
143 }
144
145 //singleton design pattern
146 private static ScreenCapture defaultCapturer = new ScreenCapture();
147 private int x1,y1,x2,y2;
148 private int recX,recY,recH,recW; //截取的图像
149 private boolean isFirstPoint  = true;
150 private BackgroundImage labFullScreenImage = new BackgroundImage();
151 private Robot robot;
152 private BufferedImage fullScreenImage;
153 private BufferedImage pickedImage;
154 private String defaultImageFormater = "png";
155 private JDialog dialog = new JDialog();
156}
157
158
159
160/** *//**显示图片的Label*/
161class BackgroundImage extends JLabel{
162 public void paintComponent(Graphics g) {
163  super.paintComponent(g);
164  g.drawRect(x,y,w,h);
165  String area = Integer.toString(w)+" * "+ Integer.toString(h);
166  g.drawString(area,x+(int)w/2-15,y+(int)h/2);
167  g.drawLine(lineX,0,lineX,getHeight());
168  g.drawLine(0,lineY,getWidth(),lineY);
169 }
170
171 public void drawRectangle(int x,int y,int width,int height) {
172  this.x = x;
173  this.y = y;
174  h = height;
175  w = width;
176  repaint();
177 }
178
179 public void drawCross(int x,int y) {
180  lineX = x;
181  lineY = y;
182  repaint();
183 }
184
185 int lineX,lineY;
186 int x,y,h,w;
187}
188
189

 

转载自:http://www.xinbcw.com

你可能感兴趣的:(java,职场,程序,休闲)