WEB开发网
开发学院WEB开发Jsp 一个简单的用JAVA实现的屏幕抓图(源代码) 阅读

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

 2008-01-05 09:39:16 来源:WEB开发网   
核心提示:主要使用的是java.util.Robot类来捕捉屏幕,可以实现对屏幕一个矩形区域的捕捉,通过这个类,我们也可以实现一个远程桌面控制的程序package com.qiu.util;import java.io.*;import java.net.*;import javax.swing.*;import java.aw

  主要使用的是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);

Tags:一个 简单 JAVA

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接