加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

用Java模拟出QQ桌面截图功能

发布时间:2020-12-15 00:13:23 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 Q的桌面截图功能非常方便,去年曾用Java模拟过一个,现整理出来。 本方法首先需要抓到屏幕的整个图象,将图象显示在一个JFrame中,再将JFrame全屏显示

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

Q的桌面截图功能非常方便,去年曾用Java模拟过一个,现整理出来。
本方法首先需要抓到屏幕的整个图象,将图象显示在一个JFrame中,再将JFrame全屏显示,这样就模拟出了一个桌面,Java也就可以获得鼠标的作用区域从而实现桌面中的小范围截屏。
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;

/**
 * 用Java模拟出QQ桌面截图功能
 * @author 五斗米 <如转载请保留作者和出处>
 * @blog <a href="http://blog.csdn.net/mq612">http://blog.csdn.net/mq612
 */

public class Test extends JFrame {

 private static final long serialVersionUID = -267804510087895906L;

 private JButton button = null;
  
 private JLabel imgLabel = null;

 public Test() {
  button = new JButton("模拟屏幕(点右键退出)");
  button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    try {
     new ScreenWindow(imgLabel);
    } catch (Exception e1) {
     JOptionPane.showConfirmDialog(null,"出现意外错误!","系统提示",JOptionPane.DEFAULT_OPTION,JOptionPane.ERROR_MESSAGE);
    }
   }
  });
  JPanel pane = new JPanel();
  pane.setBackground(Color.WHITE);
  imgLabel = new JLabel();
  pane.add(imgLabel);
  JScrollPane spane = new JScrollPane(pane);
  this.getContentPane().add(button,BorderLayout.NORTH);
  this.getContentPane().add(spane);
  this.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
  this.setSize(300,200);
  this.setLocationRelativeTo(null);
  this.setVisible(true);
 }

 public static void main(String[] args) {
  new Test();
 }
}

class ScreenWindow extends JFrame {

 private static final long serialVersionUID = -3758062802950480258L;
  
 private boolean isDrag = false;

 private int x = 0;

 private int y = 0;

 private int xEnd = 0;

 private int yEnd = 0;

 public ScreenWindow(final JLabel imgLabel) throws AWTException,InterruptedException {
  Dimension screenDims = Toolkit.getDefaultToolkit().getScreenSize();
  JLabel label = new JLabel(new ImageIcon(ScreenImage.getScreenImage(0,screenDims.width,screenDims.height)));
  label.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
  label.addMouseListener(new MouseAdapter() {
   public void mouseClicked(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON3) {
     dispose();
    }
   }

   public void mousePressed(MouseEvent e) {
    x = e.getX();
    y = e.getY();
   }

   public void mouseReleased(MouseEvent e) {
    if (isDrag) {
     xEnd = e.getX();
     yEnd = e.getY();
     if(x > xEnd){
      int temp = x;
      x = xEnd;
      xEnd = temp;
     }
     if(y > yEnd){
      int temp = y;
      y = yEnd;
      yEnd = temp;
     }
     try {
      imgLabel.setIcon(new ImageIcon(ScreenImage.getScreenImage(x,y,xEnd - x,yEnd - y)));
     } catch (Exception ex) {
      JOptionPane.showConfirmDialog(null,JOptionPane.ERROR_MESSAGE);
     }
     dispose();
    }
   }
  });
  label.addMouseMotionListener(new MouseMotionListener() {
   public void mouseDragged(MouseEvent e) {
    if(!isDrag)
     isDrag = true;
   }

   public void mouseMoved(MouseEvent e) {
    /** 拖动过程的虚线选取框需自己实现 */
   }
  });
  this.setUndecorated(true);
  this.getContentPane().add(label);
  this.setSize(screenDims.width,screenDims.height);
  this.setVisible(true);
  this.setExtendedState(JFrame.MAXIMIZED_BOTH);
 }
}

class ScreenImage {

 public static Image getScreenImage(int x,int y,int w,int h) throws AWTException,InterruptedException {
  Robot robot = new Robot();
  Image screen = robot.createScreenCapture(new Rectangle(x,w,h)).getScaledInstance(w,h,Image.SCALE_SMOOTH);
  MediaTracker tracker = new MediaTracker(new Label());
  tracker.addImage(screen,1);
  tracker.waitForID(0);
  return screen;
 }
}
 

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读