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