|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
令人可喜的是java现在已经开源了,所以我想我上述的想法也许有一天会实现,因为java一直都是不断创新的语言,每次创新都会给我们惊喜,这也是我喜欢java的一个原因。源代码次要利用的是java.util.Robot类来捕捉屏幕,能够完成对屏幕一个矩形地区的捕捉,经由过程这个类,我们也能够完成一个远程桌面把持的程序
packagecom.qiu.util;
importjava.io.*;
importjava.net.*;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
importjava.awt.image.*;
importjavax.imageio.*;
importjava.io.*;
/**@AuthorQiu_BaiChao
*一个复杂的屏幕抓图
*
**/
publicclassScreenCapture{
//testmain
publicstaticvoidmain(String[]args)throwsException{
Stringuserdir=System.getProperty("user.dir");
FiletempFile=newFile("d:","temp.png");
ScreenCapturecapture=ScreenCapture.getInstance();
capture.captureImage();
JFrameframe=newJFrame();
JPanelpanel=newJPanel();
panel.setLayout(newBorderLayout());
JLabelimagebox=newJLabel();
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");
}
privateScreenCapture(){
try{
robot=newRobot();
}
catch(AWTExceptione){
System.err.println("InternalError:"+e);
e.printStackTrace();
}
JPanelcp=(JPanel)dialog.getContentPane();
cp.setLayout(newBorderLayout());
labFullScreenImage.addMouseListener(newMouseAdapter(){
publicvoidmouseReleased(MouseEventevn){
isFirstPoint=true;
pickedImage=fullScreenImage.getSubimage(recX,recY,recW,recH);
dialog.setVisible(false);
}
});
labFullScreenImage.addMouseMotionListener(newMouseMotionAdapter(){
publicvoidmouseDragged(MouseEventevn){
if(isFirstPoint){
x1=evn.getX();
y1=evn.getY();
isFirstPoint=false;
}
else{
x2=evn.getX();
y2=evn.getY();
intmaxX=Math.max(x1,x2);
intmaxY=Math.max(y1,y2);
intminX=Math.min(x1,x2);
intminY=Math.min(y1,y2);
recX=minX;
recY=minY;
recW=maxX-minX;
recH=maxY-minY;
labFullScreenImage.drawRectangle(recX,recY,recW,recH);
}
}
publicvoidmouseMoved(MouseEvente){
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);
}
//SingletonPattern
publicstaticScreenCapturegetInstance(){
returndefaultCapturer;
}
/**捕获全屏慕*/
publicIconcaptureFullScreen(){
fullScreenImage=robot.createScreenCapture(newRectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
ImageIconicon=newImageIcon(fullScreenImage);
returnicon;
}
/**捕获屏幕的一个矫形地区
*/
publicvoidcaptureImage(){
fullScreenImage=robot.createScreenCapture(newRectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
ImageIconicon=newImageIcon(fullScreenImage);
labFullScreenImage.setIcon(icon);
dialog.setVisible(true);
}
/**失掉捕获后的BufferedImage*/
publicBufferedImagegetPickedImage(){
returnpickedImage;
}
/**失掉捕获后的Icon*/
publicImageIcongetPickedIcon(){
returnnewImageIcon(getPickedImage());
}
/**贮存为一个文件,为PNG格局
*@deprecated
*replacedbysaveAsPNG(Filefile)
**/
@Deprecated
publicvoidsaveToFile(Filefile)throwsIOException{
ImageIO.write(getPickedImage(),defaultImageFormater,file);
}
/**贮存为一个文件,为PNG格局*/
publicvoidsaveAsPNG(Filefile)throwsIOException{
ImageIO.write(getPickedImage(),"png",file);
}
/**贮存为一个JPEG格局图象文件*/
publicvoidsaveAsJPEG(Filefile)throwsIOException{
ImageIO.write(getPickedImage(),"JPEG",file);
}
/**写进一个OutputStream*/
publicvoidwrite(OutputStreamout)throwsIOException{
ImageIO.write(getPickedImage(),defaultImageFormater,out);
}
//singletondesignpattern
privatestaticScreenCapturedefaultCapturer=newScreenCapture();
privateintx1,y1,x2,y2;
privateintrecX,recY,recH,recW;//截取的图象
privatebooleanisFirstPoint=true;
privateBackgroundImagelabFullScreenImage=newBackgroundImage();
privateRobotrobot;
privateBufferedImagefullScreenImage;
privateBufferedImagepickedImage;
privateStringdefaultImageFormater="png";
privateJDialogdialog=newJDialog();
}
/**显现图片的Label*/
classBackgroundImageextendsJLabel{
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
g.drawRect(x,y,w,h);
Stringarea=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);
}
publicvoiddrawRectangle(intx,inty,intwidth,intheight){
this.x=x;
this.y=y;
h=height;
w=width;
repaint();
}
publicvoiddrawCross(intx,inty){
lineX=x;
lineY=y;
repaint();
}
intlineX,lineY;
intx,y,h,w;
}
C++编译的是本地码,优点是启动快,而且可以精确控制资源因此可以开发很高效的程序.缺点是编程麻烦,而且容易留下安全隐患.跨平台靠源代码在各个平台间分别编译(一处编写到处编译) |
|