这是一个java版的截图程序
复制代码代码如下:
パッケージcom.hongyuan.test;
java.awt.awtexceptionをインポートします。
java.awt.borderlayoutをインポートします。
java.awt.colorをインポートします。
java.awt.dimensionをインポートします。
java.awt.eventqueueをインポートします。
java.awt.flowlayoutをインポートします。
java.awt.graphicsをインポートします。
java.awt.imageをインポートします。
java.awt.rectangleをインポートします。
java.awt.robotをインポートします。
java.awt.toolkitをインポートします。
java.awt.event.actioneventをインポートします。
java.awt.event.actionlistenerをインポートします。
java.awt.event.mouseadapterをインポートします。
java.awt.event.MouseEventをインポートします。
java.awt.event.mousemotionAdapterをインポートします。
java.awt.image.bufferedimageをインポートします。
java.awt.image.rescaleopをインポートします。
java.io.fileをインポートします。
java.io.ioexceptionをインポートします。
java.text.simpledateformatをインポートします。
Import Java.util.date;
javax.imageio.imageioをインポートします。
javax.swing.imageiconをインポートします。
javax.swing.jbuttonをインポートします。
javax.swing.jfilechooserをインポートします。
javax.swing.jtoolbarをインポートします。
javax.swing.jwindowをインポートします。
javax.swing.filechooser.filenameextensionfilterをインポートします。
javax.swing.filechooser.filesystemviewをインポートします。
Public Class Screenshottest {
public static void main(string [] args){
eventqueue.invokelater(new runnable(){
@オーバーライド
public void run(){
試す {
screenshotwindow ssw = new ScreenshotWindow();
ssw.setVisible(true);
} catch(awtexception e){
e.printstacktrace();
}
}
});
}
}
/*
*截图窗口
*/
クラススクリーンショットウィンドウはjwindowを拡張します
{
Private Int Orgx、Orgy、Endx、Endy;
private bufferedimage image = null;
private bufferedimage tempimage = null;
private bufferedimage saveimage = null;
プライベートツールスイスツール= null;
public screenshotwindow()throws awtexception {
//获取屏幕尺寸
dimension d = toolkit.getDefaultToolkit()。getScreensize();
this.setbounds(0、0、d.width、d.height);
//截取屏幕
Robot Robot = new Robot();
image = robot.createscreencapture(new Rectangle(0、0、d.Width、D.Height));
this.addmouseListener(new Mouseadapter(){
@オーバーライド
public void mousepressed(mousevent e){
//鼠标松开时记录结束点坐标、并隐藏操作窗口
orgx = e.getx();
Orgy = e.gety();
if(tools!= null){
tools.setVisible(false);
}
}
@オーバーライド
public void mouseReleased(MouseEvent e){
//鼠标松开时、显示操作窗口
if(tools == null){
Tools = new Toolswindow(screenshotwindow.this、e.getx()、e.gety());
}それ以外{
tools.setlocation(e.getx()、e.gety());
}
tools.setVisible(true);
tools.tofront();
}
});
this.addmousemotionListener(new MousemotionAdapter(){
@オーバーライド
public void mousedragged(mousevent e){
//鼠标拖动时、记录坐标并重绘窗口
endx = e.getx();
endy = e.gety();
//临时图像、用于缓冲屏幕区域放置屏幕闪烁
Image Tempimage2 = createImage(screenshotwindow.this.getWidth()、screenshotwindow.this.getheigh());
グラフィックg = tempimage2.getgraphics();
G.drawimage(Tempimage、0、0、null);
int x = math.min(orgx、endx);
int y = math.min(Orgy、Endy);
int width = math.abs(endx -orgx)+1;
int height = math.abs(endy -Orgy)+1;
//加上1
g.setcolor(color.blue);
G.drawrect(x-1、y-1、width+1、height+1);
//
SaveImage = image.getSubimage(x、y、幅、高さ);
G.Drawimage(SaveImage、x、y、null);
screenshotwindow.this.getGraphics()。drawimage(tempimage2,0,0、screenshotwindow.this);
}
});
}
@オーバーライド
public void paint(グラフィックG){
Rescaleop ro = new Rescaleop(0.8F、0、null);
tempimage = ro.filter(image、null);
G.drawimage(Tempimage、0、0、this);
}
//保存图像到文件
public void saveimage()throws ioexception {
jfilechooser jfc = new jfilechooser();
jfc.setdialogtitle( "保存");
//文件过滤器、用户过滤可选择文件
filenameExtensionFilterフィルター= new FilenameExtensionFilter( "jpg"、 "jpg");
jfc.setFilefilter(フィルター);
// 初始化一个默认文件(此文件会生成到桌面上)
SimpleDateFormat sdf = new SimpledateFormat( "yyyymmddhhmmss");
文字列filename = sdf.format(new date());
file filepath = filesystemView.getFilesSystemView()。gethomedirectory();
ファイルdefaultFile = new file(filepath + file.separator + filename + ".jpg");
jfc.setselectedfile(defaultfile);
int flag = jfc.showsavedialog(this);
if(flag == jfilechooser.approve_option){
ファイルfile = jfc.getSelectedFile();
string path = file.getPath();
//检查文件后缀、放置用户忘记输入后缀或者输入不正确的后缀
if(!(path.endswith( "。jpg")|| path.endswith( "。jpg"))){
PATH+= "。JPG";
}
//写入文件
Imageio.write(saveimage、 "jpg"、new file(path));
System.Exit(0);
}
}
}
/*
*操作窗口
*/
クラスツールスウィンドウはJWindowを拡張します
{
プライベートスクリーンショットウィンドウの親。
public Toolswindow(screenshotwindow parent、int x、int y){
this.parent = parent;
this.init();
this.setlocation(x、y);
this.pack();
this.setVisible(true);
}
private void init(){
this.setlayout(new borderlayout());
jtoolbarツールバー= new Jtoolbar( "Java截图");
//保存按钮
jbutton savebutton = new jbutton(new ImageIcon( "Images/Save.gif"));
SaveButton.AddActionListener(new ActionListener(){
@オーバーライド
public void actionperformed(ActionEvent e){
試す {
parent.saveImage();
} catch(ioexception e1){
e1.printstacktrace();
}
}
});
Toolbar.Add(SaveButton);
//关闭按钮
jbutton closebutton = new jbutton(new ImageIcon( "Images/close.gif"));
closebutton.addActionListener(new ActionListener(){
@オーバーライド
public void actionperformed(ActionEvent e){
System.Exit(0);
}
});
Toolbar.Add(CloseButton);
this.add(Toolbar、borderlayout.north);
}
}