首页 > 学院 > 开发设计 > 正文

一个简单的用JAVA实现的屏幕抓图(源代码)

2019-11-18 13:40:25
字体:
来源:转载
供稿:网友

  主要使用的是java.util.Robot类来捕捉屏幕,可以实现对屏幕一个矩形区域的捕捉,通过这个类,我们也可以实现一个远程桌面控制的程序


package com.qiu.util;
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.io.*;

/**@Author Qiu_BaiChao
*一个简单的屏幕抓图
*
**/

public class ScreenCapture {
//test main
public static void main(String[] args) throws Exception{
String userdir = System.getPRoperty("user.dir");
File tempFile = new File("d:","temp.png");
ScreenCapture capture = ScreenCapture.getInstance();
capture.captureImage();
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
JLabel imagebox = new JLabel();
panel.add(BorderLayout.CENTER,imagebox);
imagebox.setIcon(capture.getPickedIcon());
capture.saveToFile(tempFile);
capture.captureImage();
imagebox.setIcon(capture.getPickedIcon());
frame.setContentPane(panel);
frame.setSize(400,300);
frame.show();
System.out.println("Over");
}

private ScreenCapture() {

try{
robot = new Robot();
}
catch(AWTException e) {
System.err.println("Internal Error: " + e);
e.printStackTrace();
}
JPanel cp = (JPanel)dialog.getContentPane();
cp.setLayout(new BorderLayout());
labFullScreenImage.addMouseListener(new MouseAdapter() {
public void mouseReleased(MouseEvent evn) {
isFirstPoint = true;
pickedImage = fullScreenImage.getSubimage(recX,recY,recW,recH);
dialog.setVisible(false);
}
});

labFullScreenImage.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent evn) {
if(isFirstPoint) {
x1 = evn.getX();
y1 = evn.getY();
isFirstPoint = false;
}
else {
x2 = evn.getX();
y2 = evn.getY();
int maxX = Math.max(x1,x2);
int maxY = Math.max(y1,y2);
int minX = Math.min(x1,x2);
int minY = Math.min(y1,y2);
recX = minX;
recY = minY;
recW = maxX-minX;
recH = maxY-minY;
labFullScreenImage.drawRectangle(recX,recY,recW,recH);
}
}

public void mouseMoved(MouseEvent e) {
labFullScreenImage.drawCross(e.getX(),e.getY());
}
});

cp.add(BorderLayout.CENTER,labFullScreenImage);

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表