Java 模拟 QQ 微信截图功能
这只是简单的模拟,功能略显简陋,可供学习参考。
QQ 截图,点击截图按钮,桌面画面会定住不动,画矩形框截取矩形框内容。若这时你在播放视频你会发现截图时视频画面停止了,但是背景音乐还在继续播放,其实这时只是个障眼法,一张全屏静止图片覆盖了整个电脑屏幕,然后用矩形裁剪静止图片。
这里我们使用 JFrame,两个类,启动类 MainFrame,图片窗口类 ImageFrame。代码参考自网络并作些许修改。
MainFrame
启动类,按钮截全屏并传图片给图片容器 ImageFrame 显示。
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class MainFrame extends JFrame {
JButton capture;
public MainFrame() {capture = new JButton(" 截图 ");
Container container = getContentPane();
container.add(capture);
setSize(130, 80);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setAlwaysOnTop(true);
// 居中显示按钮 这里我就不给居中了 直接显示右下角
// setLocationRelativeTo(null);
setLocation(Toolkit.getDefaultToolkit().getScreenSize().width - 180, Toolkit.getDefaultToolkit().getScreenSize().height - 100);
capture.addActionListener(e -> {
// 使自己隐藏
setVisible(false);
// 截图
Robot robot = null;
try {robot = new Robot();
} catch (AWTException awtException) {awtException.printStackTrace();
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
int width = toolkit.getScreenSize().width;
int height = toolkit.getScreenSize().height;
System.out.println(width + "x" + height);
BufferedImage capture = robot.createScreenCapture(new Rectangle(width, height));
new ImageFrame(capture);
// 截图后显示
setVisible(true);
});
setVisible(true);
}
public static void main(String[] args) {new MainFrame();
}
}
ImageFrame
显示启动类传来的全屏图片,按住鼠标左键拖动绘制矩形,松开鼠标按 Enter 键保存图片至剪切板,双击鼠标左键保存图片至剪切板并且提示保存至本地的 Windows 图片文件夹。
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class ImageFrame extends JFrame {
BufferedImage image;
// 记录鼠标坐标 宽高
int x, y;
int w, h;
boolean flag = false;
boolean clickState = false;
Graphics graphics;
public ImageFrame(BufferedImage image) {
this.image = image;
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension screenSize = toolkit.getScreenSize();
int height = screenSize.height;
int width = screenSize.width;
setSize(width, height);
// 无修饰边框
setUndecorated(true);
setVisible(true);
// 一旦显示整个面板就绘制图片
repaint();
addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) { }
@Override
public void keyPressed(KeyEvent e) { }
@Override
public void keyReleased(KeyEvent e) {System.out.println(e.getKeyCode());
// esc 退出
if (e.getKeyCode() == 27) {dispose();
}
// Enter 键直接复制图片到剪切板
if (e.getKeyCode() == 10) {
Robot robot = null;
try {robot = new Robot();
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(x, y, w, h));
setClipboardImage(screenCapture);
} catch (AWTException awtException) {awtException.printStackTrace();
}
dispose();}
}
});
// 绘制矩形 注册鼠标监听
// 鼠标按下获取开始坐标 鼠标释放时获得终点坐标
addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
// 鼠标双击截图保存到文件
if (flag && e.getClickCount() == 2) {
// 保存矩形内截图
try {Robot robot = new Robot();
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(x, y, w, h));
setClipboardImage(screenCapture);
JFileChooser chooser = new JFileChooser();
String dateString = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS"));
// 截图保存 Windows 用户文件夹
String imgPath = System.getProperties().getProperty("user.home") + "\\Pictures\\" + dateString + ".jpg";
chooser.setSelectedFile(new File(imgPath));
int xz = chooser.showSaveDialog(null);
String absolutePath = null;
if (xz == 0) {
// 获得用户选择文件
File selectedFile = chooser.getSelectedFile();
absolutePath = selectedFile.getAbsolutePath();
System.out.println(absolutePath);
}
// 写入图片
if (absolutePath != null)
ImageIO.write(screenCapture, "jpeg", new File(absolutePath));
} catch (AWTException | IOException awtException) {awtException.printStackTrace();
}
dispose();}
}
@Override
public void mousePressed(MouseEvent e) {
clickState = true;
if (!flag) {x = e.getXOnScreen();
y = e.getY();}
}
@Override
public void mouseReleased(MouseEvent e) {
clickState = false;
if (!flag) {w = e.getX() - x;
h = e.getY() - y;
repaint();
flag = true;
}
}
@Override
public void mouseEntered(MouseEvent e) { }
@Override
public void mouseExited(MouseEvent e) {}});
addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {if (clickState) {
// 鼠标移动重绘矩形
w = e.getX() - x;
h = e.getY() - y;
repaint();}
}
@Override
public void mouseMoved(MouseEvent e) {}});
}
public static void main(String[] args) {new ImageFrame(null);
}
@Override
public void paint(Graphics g) {super.paint(g);
g.drawImage(image, 0, 0, null);
graphics = g;
g.drawRect(x, y, w, h);
}
/**
* 设置图片到剪切板
*
* @param image
*/
public static void setClipboardImage(final Image image) {Transferable trans = new Transferable() {
@Override
public DataFlavor[] getTransferDataFlavors() {return new DataFlavor[]{DataFlavor.imageFlavor};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {return DataFlavor.imageFlavor.equals(flavor);
}
@Override
public Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {if (isDataFlavorSupported(flavor))
return image;
throw new UnsupportedFlavorException(flavor);
}
};
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans,
null);
}
}
目录
Java 模拟 QQ 微信截图功能
- MainFrame
- ImageFrame
目录
Java 模拟 QQ 微信截图功能
- MainFrame
- ImageFrame