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