网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  一个简单的用java实现的屏幕抓图(源代码)     
  文章作者:未知  文章来源:水木森林  
  查看:103次  录入:管理员--2007-11-17  
 
  主要使用的是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);
dialog.setcursor(cursor.getpredefinedcursor(cursor.crosshair_cursor));
dialog.setalwaysontop(true);
dialog.setmaximumsize(
toolkit.getdefaulttoolkit().getscreensize());
dialog.setundecorated(true);
dialog.setsize(dialog.getmaximumsize());
dialog.setmodal(true);
}
//singleton pattern
public static screencapture getinstance() {
return defaultcapturer;
}
/**捕捉全屏慕*/
public icon capturefullscreen() {
fullscreenimage = robot.createscreencapture(new rectangle(
toolkit.getdefaulttoolkit().getscreensize()));
imageicon icon = new imageicon(fullscreenimage);
return icon;
}
/**捕捉屏幕的一个矫形区域
*/
public void captureimage() {
fullscreenimage = robot.createscreencapture(new rectangle(
toolkit.getdefaulttoolkit().getscreensize()));
imageicon icon = new imageicon(fullscreenimage);
labfullscreenimage.seticon(icon);
dialog.setvisible(true);
}
/**得到捕捉后的bufferedimage*/
public bufferedimage getpickedimage() {
return pickedimage;
}
/**得到捕捉后的icon*/
public imageicon getpickedicon() {
return new imageicon(getpickedimage());
}
/**储存为一个文件,为png格式
*@deprecated
*replaced by saveaspng(file file)
**/
@deprecated
public void savetofile(file file) throws ioexception{
imageio.write(getpickedimage(),defaultimageformater,file);
}
/**储存为一个文件,为png格式*/
public void saveaspng(file file) throws ioexception {
imageio.write(getpickedimage(),"png",file);
}
/**储存为一个jpeg格式图像文件*/
public void saveasjpeg(file file) throws ioexception {
imageio.write(getpickedimage(),"jpeg",file);
}

/**写入一个outputstream*/
public void write(outputstream out) throws ioexception{
imageio.write(getpickedimage(),defaultimageformater,out);
}

//singleton design pattern
private static screencapture defaultcapturer = new screencapture();
private int x1,y1,x2,y2;
private int recx,recy,rech,recw; //截取的图像
private boolean isfirstpoint = true;
private backgroundimage labfullscreenimage = new backgroundimage();
private robot robot;
private bufferedimage fullscreenimage;
private bufferedimage pickedimage;
private string defaultimageformater = "png";
private jdialog dialog = new jdialog();
}

/**显示图片的label*/
class backgroundimage extends jlabel{
public void paintcomponent(graphics g) {
super.paintcomponent(g);
g.drawrect(x,y,w,h);
string area = integer.tostring(w)+" * "+ integer.tostring(h);
g.drawstring(area,x+(int)w/2-15,y+(int)h/2);
g.drawline(linex,0,linex,getheight());
g.drawline(0,liney,getwidth(),liney);
}

public void drawrectangle(int x,int y,int width,int height) {
this.x = x;
this.y = y;
h = height;
w = width;
repaint();
}

public void drawcross(int x,int y) {
linex = x;
liney = y;
repaint();
}

int linex,liney;
int x,y,h,w;
}
 
 
上一篇: 一个简单的定时器应用: vartimer (java)    下一篇: 一个经典的java applet程序(二)
  相关文档
保存java程序状态及设置之properties文件 11-17
ejb轻松进阶之目录 11-16
技巧:如何在applet中引用jar中的资源文件 11-17
ceil 方法 11-16
使用isinstanceof()测试对象类型 11-16
struts+spring+hibernate实现上传下载 11-17
旋转你的3d世界 11-17
仔细触摸java编程中的“文档”和“包” 11-16
java中类似于c语言中sizeof功能实现(一) 11-17
java web service 11-17
教你学会xml web service 的安全性 11-17
网页软键盘,有效的防止监测键盘的木马软件 11-16
java核心代码例程之:rmiexamplesetup.java 11-17
如何在java中使用robot类 11-17
里程碑式的革新—全新j2se1.5新特性简介 11-17
实现 java 平台的三种方式v 11-17
java中的常见问题 11-17
310-025 scjp exam dumps 06/10/02 11-17
ajax:开发者新的技术天地介绍 11-17
java基础-如何编写一个java的队列类 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息