在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

Java实现简单的截图工具

2016-12-20 13:15| 发布者: zhangjf| 查看: 470| 评论: 0

摘要: Robot.rar ~ 116KB ScreenShot.java ~ 420B package Robot_Caputer;import java.awt.AWTException;import java.awt.EventQueue;public class ScreenShot { public static void main(String a ...
Robot.rar ~ 116KB ScreenShot.java ~ 420B
  1. package Robot_Caputer;
  2. import java.awt.AWTException;
  3. import java.awt.EventQueue;
  4. public class ScreenShot {
  5. public static void main(String[] args) {
  6. EventQueue.invokeLater(new Runnable() {
  7. public void run() {
  8. try{
  9. ScreenShotWindow ssw=new ScreenShotWindow();
  10. ssw.setVisible(true);
  11. }catch(AWTException e){
  12. e.printStackTrace();
  13. }
  14. }
  15. });
  16. }
  17. }
复制代码
[文件] ScreenShotWindow.java ~ 5KB (13)
  1. package Robot_Caputer;
  2. import images.*;
  3. import java.awt.AWTException;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.GraphicsDevice;
  8. import java.awt.GraphicsEnvironment;
  9. import java.awt.Image;
  10. import java.awt.Rectangle;
  11. import java.awt.Robot;
  12. import java.awt.Toolkit;
  13. import java.awt.event.MouseAdapter;
  14. import java.awt.event.MouseEvent;
  15. import java.awt.event.MouseMotionAdapter;
  16. import java.awt.image.BufferedImage;
  17. import java.awt.image.RescaleOp;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.Date;
  22. import javax.imageio.ImageIO;
  23. import javax.swing.JFileChooser;
  24. import javax.swing.JWindow;
  25. import javax.swing.filechooser.FileNameExtensionFilter;
  26. import javax.swing.filechooser.FileSystemView;
  27. //Jwindow 也是四大顶级组件之一,地位等同于JFrame,是一个无标题栏的窗口
  28. public class ScreenShotWindow extends JWindow {
  29. /**
  30. *
  31. */
  32. private static final long serialVersionUID = 1L;
  33. private int orgx,orgy,endx,endy;
  34. /**image的作用:
  35. * 1.获取整个屏幕的截图*/
  36. private BufferedImage image=null;
  37. private BufferedImage tempImage=null;
  38. private BufferedImage saveImage=null;
  39. private ToolsWindow tools=null;
  40. public ScreenShotWindow() throws AWTException {
  41. //获取默认屏幕设备
  42. GraphicsEnvironment environment=GraphicsEnvironment.getLocalGraphicsEnvironment();
  43. GraphicsDevice screen=environment.getDefaultScreenDevice();
  44. //获取屏幕尺寸
  45. Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
  46. this.setBounds(0, 0, d.width, d.height);
  47. //获取屏幕截图
  48. Robot robot=new Robot(screen);
  49. // Robot robot=new Robot();
  50. // image=new BufferedImage((int)d.getWidth(),(int)d.getHeight(),BufferedImage.TYPE_4BYTE_ABGR);
  51. image=robot.createScreenCapture(new Rectangle(0, 0, d.width, d.height));
  52. //设置鼠标敲击的时间监听
  53. this.addMouseListener(new MouseAdapter() {
  54. //鼠标按下的事件监听
  55. @Override
  56. public void mousePressed(MouseEvent e) {
  57. //
  58. orgx=e.getX();
  59. orgy=e.getY();
  60. if(tools!=null){
  61. tools.setVisible(false);
  62. }
  63. }
  64. //鼠标抬起的事件监听
  65. @Override
  66. public void mouseReleased(MouseEvent e) {
  67. if(tools==null){
  68. tools=new ToolsWindow(ScreenShotWindow.this,e.getX(),e.getY());
  69. }else{
  70. tools.setLocation(e.getX(), e.getY());
  71. }
  72. tools.setVisible(true);
  73. tools.toFront();
  74. }
  75. });
  76. //对于鼠标移动的监听
  77. this.addMouseMotionListener(new MouseMotionAdapter() {
  78. //鼠标滑动的监听
  79. //在滑动过程中会被反复调用
  80. @Override
  81. public void mouseDragged(MouseEvent e) {
  82. endx=e.getX();
  83. endy=e.getY();
  84. //临时图像,用于缓冲屏幕区域放置屏幕闪烁
  85. Image tempImage2=createImage(ScreenShotWindow.this.getWidth(),ScreenShotWindow.this.getHeight());
  86. //用于绘图
  87. Graphics g=tempImage2.getGraphics();
  88. g.drawImage(tempImage, 0, 0,null);
  89. int x=Math.min(orgx, endx);
  90. int y=Math.min(orgy, endy);
  91. int width=Math.abs(endx-orgx)+1;
  92. int height=Math.abs(endy-orgy)+1;
  93. g.setColor(Color.RED);
  94. //保证图片矩形不被边框覆盖
  95. g.drawRect(x-1, y-1, width+1, height+1);
  96. //getSubimage(int x,int y,int w,int h)用于返回规定位置中的矩形图像到BufferedImag对象中
  97. saveImage=image.getSubimage(x, y, width, height);
  98. //用于画当前图像中的可用图像
  99. g.drawImage(saveImage, x, y, null);
  100. ScreenShotWindow.this.getGraphics().drawImage(tempImage2,
  101. 0, 0,ScreenShotWindow.this);
  102. }
  103. });
  104. }
  105. //重写了绘画的方法
  106. @Override
  107. public void paint(Graphics g) {
  108. //new RescaleOp(float[] scaleFactors, float[] offsets, RenderingHints hints)
  109. //构造一个具有所希望的缩放因子和偏移量的新 RescaleOp。
  110. //RescaleOp 是有关图像缩放的类
  111. //RescaleOp.filter(BufferedImage src,BufferedImage dest)
  112. //用于对源图像src进行缩放
  113. RescaleOp ro=new RescaleOp(0.8f,0, null);
  114. tempImage=ro.filter(image, null);
  115. g.drawImage(tempImage, 0, 0, this);
  116. }
  117. //保存图像到文件
  118. public void saveImage() throws IOException{
  119. JFileChooser jfc=new JFileChooser();
  120. jfc.setDialogTitle("保存");
  121. //文件过滤器,用户过滤可选择的文件
  122. FileNameExtensionFilter filter=new FileNameExtensionFilter("JPG", "jpg");
  123. jfc.setFileFilter(filter);
  124. //初始化一个默认文件(此文件会生成在桌面)
  125. SimpleDateFormat sdf=new SimpleDateFormat("yyyymmddHHmmss");
  126. String filename=sdf.format(new Date());
  127. File filePath=FileSystemView.getFileSystemView().getHomeDirectory();
  128. File defaultFile=new File(filePath+File.separator+filename+".jpg");
  129. jfc.setSelectedFile(defaultFile);
  130. int flag=jfc.showSaveDialog(this);
  131. if(flag==JFileChooser.APPROVE_OPTION){
  132. File file=jfc.getSelectedFile();
  133. String path=file.getPath();
  134. //检查文件后缀,放置用户忘记输入后缀或输入不正确的后缀
  135. if(!(path.endsWith(".jpg")||path.endsWith("JPG"))){
  136. path+=".jpg";
  137. }
  138. //写入文件
  139. ImageIO.write(saveImage, "jpg", new File(path));
  140. System.exit(0);
  141. }
  142. }
  143. }
复制代码
[文件] ToolsWindow.java ~ 1KB
  1. package Robot_Caputer;
  2. import java.awt.BorderLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.IOError;
  6. import java.io.IOException;
  7. import javax.swing.ImageIcon;
  8. import javax.swing.JButton;
  9. import javax.swing.JToolBar;
  10. import javax.swing.JWindow;
  11. public class ToolsWindow extends JWindow{
  12. /**
  13. *
  14. */
  15. private static final long serialVersionUID = 1L;
  16. private ScreenShotWindow parent;
  17. public ToolsWindow(ScreenShotWindow parent,int x,int y) {
  18. this.parent=parent;
  19. this.init();
  20. //将组件移到(x,y)的位置
  21. this.setLocation(x, y);
  22. //调整窗口的大小来适应控件
  23. this.pack();
  24. this.setVisible(true);
  25. }
  26. private void init() {
  27. this.setLayout(new BorderLayout());
  28. JToolBar toolBar=new JToolBar("Java截图");
  29. //保存按钮
  30. JButton saveButton=new JButton(new ImageIcon("src/images/SaveIcon.gif"));
  31. saveButton.addActionListener(new ActionListener() {
  32. public void actionPerformed(ActionEvent e) {
  33. try{
  34. parent.saveImage();
  35. }catch(IOException ex1){
  36. ex1.printStackTrace();
  37. }
  38. }
  39. });
  40. toolBar.add(saveButton);
  41. //关闭按钮
  42. JButton closedButton=new JButton(new ImageIcon("src/images/closedIcon.gif"));
  43. closedButton.addActionListener(new ActionListener() {
  44. public void actionPerformed(ActionEvent e) {
  45. System.exit(0);
  46. }
  47. });
  48. toolBar.add(closedButton);
  49. this.add(toolBar, BorderLayout.NORTH);
  50. }
  51. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-8-23 04:25

Copyright 2015-2025 djqfx

返回顶部