java实现区域内屏幕截图示例

 更新时间:2014年04月14日 06:40:24   作者:  
这篇文章主要介绍了java截图示例,需要的朋友可以参考下

这是一个java版的截图程序

复制代码 代码如下:

package com.hongyuan.test;

import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JToolBar;
import javax.swing.JWindow;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;

public class ScreenShotTest {

 public static void main(String[] args) {

  EventQueue.invokeLater(new Runnable() { 
   @Override
   public void run() {
    try {
     ScreenShotWindow ssw=new ScreenShotWindow();
     ssw.setVisible(true);
    } catch (AWTException e) {
     e.printStackTrace();
    }
   }
  });
 }

}
/*
 * 截图窗口
 */
class ScreenShotWindow extends JWindow

 private int orgx, orgy, endx, endy;
    private BufferedImage image=null;
    private BufferedImage tempImage=null;
    private BufferedImage saveImage=null;

    private ToolsWindow tools=null;

 public ScreenShotWindow() throws AWTException{
   //获取屏幕尺寸
   Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
   this.setBounds(0, 0, d.width, d.height);

   //截取屏幕
   Robot robot = new Robot();
   image = robot.createScreenCapture(new Rectangle(0, 0, d.width,d.height));

   this.addMouseListener(new MouseAdapter() {
    @Override
   public void mousePressed(MouseEvent e) {
    //鼠标松开时记录结束点坐标,并隐藏操作窗口
             orgx = e.getX();
             orgy = e.getY();

             if(tools!=null){
              tools.setVisible(false);
             }
   }
   @Override
   public void mouseReleased(MouseEvent e) {
    //鼠标松开时,显示操作窗口
    if(tools==null){
     tools=new ToolsWindow(ScreenShotWindow.this,e.getX(),e.getY());
    }else{
     tools.setLocation(e.getX(),e.getY());
    }
    tools.setVisible(true);
    tools.toFront();
   }
  });

   this.addMouseMotionListener(new MouseMotionAdapter() {

   @Override
   public void mouseDragged(MouseEvent e) {
    //鼠标拖动时,记录坐标并重绘窗口
                endx = e.getX();
                endy = e.getY();

                //临时图像,用于缓冲屏幕区域放置屏幕闪烁
                Image tempImage2=createImage(ScreenShotWindow.this.getWidth(),ScreenShotWindow.this.getHeight());
                Graphics g =tempImage2.getGraphics();
                g.drawImage(tempImage, 0, 0, null);
                int x = Math.min(orgx, endx);
                int y = Math.min(orgy, endy);
                int width = Math.abs(endx - orgx)+1;
                int height = Math.abs(endy - orgy)+1;
                // 加上1防止width或height0
                g.setColor(Color.BLUE);
                g.drawRect(x-1, y-1, width+1, height+1);
                //减1加1都了防止图片矩形框覆盖掉
                saveImage = image.getSubimage(x, y, width, height);
                g.drawImage(saveImage, x, y, null);

                ScreenShotWindow.this.getGraphics().drawImage(tempImage2,0,0,ScreenShotWindow.this);
   }
  });
 }

    @Override
    public void paint(Graphics g) {
        RescaleOp ro = new RescaleOp(0.8f, 0, null);
        tempImage = ro.filter(image, null);
        g.drawImage(tempImage, 0, 0, this);
    }
    //保存图像到文件
 public void saveImage() throws IOException {
  JFileChooser jfc=new JFileChooser();
  jfc.setDialogTitle("保存");

  //文件过滤器,用户过滤可选择文件
  FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG", "jpg");
  jfc.setFileFilter(filter);

  //初始化一个默认文件(此文件会生成到桌面上)
  SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
     String fileName = sdf.format(new Date());
     File filePath = FileSystemView.getFileSystemView().getHomeDirectory();
     File defaultFile = new File(filePath + File.separator + fileName + ".jpg");
     jfc.setSelectedFile(defaultFile);

  int flag = jfc.showSaveDialog(this);
  if(flag==JFileChooser.APPROVE_OPTION){
   File file=jfc.getSelectedFile();
   String path=file.getPath();
   //检查文件后缀,放置用户忘记输入后缀或者输入不正确的后缀
   if(!(path.endsWith(".jpg")||path.endsWith(".JPG"))){
    path+=".jpg";
   }
   //写入文件
   ImageIO.write(saveImage,"jpg",new File(path));
   System.exit(0);
  }
 }
}
/*
 * 操作窗口
 */
class ToolsWindow extends JWindow
{
 private ScreenShotWindow parent;

 public ToolsWindow(ScreenShotWindow parent,int x,int y) {
  this.parent=parent;
  this.init();
  this.setLocation(x, y);
  this.pack();
  this.setVisible(true);
 }

 private void init(){

  this.setLayout(new BorderLayout());
  JToolBar toolBar=new JToolBar("Java 截图");

  //保存按钮
  JButton saveButton=new JButton(new ImageIcon("images/save.gif"));
  saveButton.addActionListener(new ActionListener() { 
   @Override
   public void actionPerformed(ActionEvent e) {
    try {
     parent.saveImage();
    } catch (IOException e1) {
     e1.printStackTrace();
    }
   }
  });
  toolBar.add(saveButton);

  //关闭按钮
  JButton closeButton=new JButton(new ImageIcon("images/close.gif"));
  closeButton.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
    System.exit(0);
   }
  });
  toolBar.add(closeButton);

  this.add(toolBar,BorderLayout.NORTH);
 }
}



相关文章

  • 浅谈Java并发编程之Lock锁和条件变量

    浅谈Java并发编程之Lock锁和条件变量

    这篇文章主要介绍了浅谈Java并发编程之Lock锁和条件变量,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-08-08
  • Java虚拟机JVM优化实战的过程全记录

    Java虚拟机JVM优化实战的过程全记录

    有人说Java之所以能够崛起,JVM功不可没。Java虚拟机最初服务于让Java语言凌驾于平台之上,实现“编写一次,到处运行”,那么下面这篇文章主要给大家分享了个关于Java虚拟机JVM优化实战的过程全记录,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08
  • Java的布隆过滤器你了解吗

    Java的布隆过滤器你了解吗

    这篇文章主要为大家详细介绍了Java的布隆过滤器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-03-03
  • 关于Spring MVC框架中拦截器Interceptor的使用解读

    关于Spring MVC框架中拦截器Interceptor的使用解读

    这篇文章主要介绍了关于Spring MVC框架中拦截器Interceptor的使用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Springboot 自定义校验代码实例

    Springboot 自定义校验代码实例

    这篇文章主要介绍了Springboot 自定义校验代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • Java时间转换成unix时间戳的方法

    Java时间转换成unix时间戳的方法

    这篇文章主要为大家详细介绍了Java时间转换成unix时间戳的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • java中的forkjoin框架的使用

    java中的forkjoin框架的使用

    这篇文章主要介绍了java中的fork join框架的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-03-03
  • 使用MyBatis返回其它类对象的字段处理

    使用MyBatis返回其它类对象的字段处理

    这篇文章主要介绍了使用MyBatis返回其它类对象的字段处理方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • SpringBoot用多线程批量导入数据库实现方法

    SpringBoot用多线程批量导入数据库实现方法

    这篇文章主要介绍了SpringBoot用多线程批量导入数据库实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2023-02-02
  • Java Main 函数启动不退出的解决方案

    Java Main 函数启动不退出的解决方案

    这篇文章主要介绍了Java Main 函数启动不退出的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-05-05

最新评论