A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 乔兵 高级黑马   /  2013-10-6 19:19  /  2269 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

主要使用的是java.util.Robot类来捕获屏幕,可以实现对屏幕一个矩形区域的捕获,通过这个类,我们也可以实现一个远程桌面控制的程序。
  1. package com.qiu.util;
  2. import java.io.*;
  3. import java.net.*;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import java.awt.image.*;
  8. import javax.imageio.*;
  9. import java.io.*;
  10. /*
  11. 一个简单的屏幕抓图
  12. */
  13. public class ScreenCapture {
  14. //test main
  15. public static void main(String[] args) throws Exception{
  16. String userdir = System.getProperty("user.dir");
  17. File tempFile = new File("d:","temp.png");
  18. ScreenCapture capture = ScreenCapture.getInstance();
  19. capture.captureImage();
  20. JFrame frame = new JFrame();
  21. JPanel panel = new JPanel();
  22. panel.setLayout(new BorderLayout());
  23. JLabel imagebox = new JLabel();
  24. panel.add(BorderLayout.CENTER,imagebox);
  25. imagebox.setIcon(capture.getPickedIcon());
  26. capture.saveToFile(tempFile);
  27. capture.captureImage();
  28. imagebox.setIcon(capture.getPickedIcon());
  29. frame.setContentPane(panel);
  30. frame.setSize(400,300);
  31. frame.show();
  32. System.out.println("Over");
  33. }
  34. private ScreenCapture() {
  35. try{
  36. robot = new Robot();
  37. }
  38. catch(AWTException e) {
  39. System.err.println("Internal Error: " + e);
  40. e.printStackTrace();
  41. }
  42. JPanel cp = (JPanel)dialog.getContentPane();
  43. cp.setLayout(new BorderLayout());
  44. labFullScreenImage.addMouseListener(new MouseAdapter() {
  45. public void mouseReleased(MouseEvent evn) {
  46. isFirstPoint = true;
  47. pickedImage = fullScreenImage.getSubimage(recX,recY,recW,recH);
  48. dialog.setVisible(false);
  49. }
  50. });
  51. labFullScreenImage.addMouseMotionListener(new MouseMotionAdapter() {
  52. public void mouseDragged(MouseEvent evn) {
  53. if(isFirstPoint) {
  54. x1 = evn.getX();
  55. y1 = evn.getY();
  56. isFirstPoint = false;
  57. }
  58. else {
  59. x2 = evn.getX();
  60. y2 = evn.getY();
  61. int maxX = Math.max(x1,x2);
  62. int maxY = Math.max(y1,y2);
  63. int minX = Math.min(x1,x2);
  64. int minY = Math.min(y1,y2);
  65. recX = minX;
  66. recY = minY;
  67. recW = maxX-minX;
  68. recH = maxY-minY;
  69. labFullScreenImage.drawRectangle(recX,recY,recW,recH);
  70. }
  71. }
  72. public void mouseMoved(MouseEvent e) {
  73. labFullScreenImage.drawCross(e.getX(),e.getY());
  74. }
  75. });
  76. cp.add(BorderLayout.CENTER,labFullScreenImage);
  77. dialog.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
  78. dialog.setAlwaysOnTop(true);
  79. dialog.setMaximumSize(
  80. Toolkit.getDefaultToolkit().getScreenSize());
  81. dialog.setUndecorated(true);
  82. dialog.setSize(dialog.getMaximumSize());
  83. dialog.setModal(true);
  84. }
  85. //Singleton Pattern
  86. public static ScreenCapture getInstance() {
  87. return defaultCapturer;
  88. }
  89. /*
  90. 捕捉全屏慕
  91. */
  92. public Icon captureFullScreen() {
  93. fullScreenImage = robot.createScreenCapture(new Rectangle(
  94. Toolkit.getDefaultToolkit().getScreenSize()));
  95. ImageIcon icon = new ImageIcon(fullScreenImage);
  96. return icon;
  97. }
  98. /*
  99. 捕捉屏幕的一个矫形区域
  100. */
  101. public void captureImage() {
  102. fullScreenImage = robot.createScreenCapture(new Rectangle(
  103. Toolkit.getDefaultToolkit().getScreenSize()));
  104. ImageIcon icon = new ImageIcon(fullScreenImage);
  105. labFullScreenImage.setIcon(icon);
  106. dialog.setVisible(true);
  107. }
  108. /*
  109. 得到捕捉后的BufferedImage
  110. */
  111. public BufferedImage getPickedImage() {
  112. return pickedImage;
  113. }
  114. /*
  115. 得到捕捉后的Icon
  116. */
  117. public ImageIcon getPickedIcon() {
  118. return new ImageIcon(getPickedImage());
  119. }
  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. /*
  130. 储存为一个文件,为PNG格式
  131. */
  132. public void saveAsPNG(File file) throws IOException {
  133. ImageIO.write(getPickedImage(),"png",file);
  134. }
  135. /*
  136. 储存为一个JPEG格式图像文件
  137. */
  138. public void saveAsJPEG(File file) throws IOException {
  139. ImageIO.write(getPickedImage(),"JPEG",file);
复制代码

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马