java实现区域内屏幕截图示例
这是一个java版的截图程序
package com.hongyuan.test;
import java.awt.awtexception;
import java.awt.borderlayout;
import java.awt.color;
import java.awt.dimension;
import java.awt.eventqueue;
import java.awt.flowlayout;
import java.awt.graphics;
import java.awt.image;
import java.awt.rectangle;
import java.awt.robot;
import java.awt.toolkit;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.event.mouseadapter;
import java.awt.event.mouseevent;
import java.awt.event.mousemotionadapter;
import java.awt.image.bufferedimage;
import java.awt.image.rescaleop;
import java.io.file;
import java.io.ioexception;
import java.text.simpledateformat;
import java.util.date;
import javax.imageio.imageio;
import javax.swing.imageicon;
import javax.swing.jbutton;
import javax.swing.jfilechooser;
import javax.swing.jtoolbar;
import javax.swing.jwindow;
import javax.swing.filechooser.filenameextensionfilter;
import javax.swing.filechooser.filesystemview;
public class screenshottest {
public static void main(string[] args) {
eventqueue.invokelater(new runnable() {
@override
public void run() {
try {
screenshotwindow ssw=new screenshotwindow();
ssw.setvisible(true);
} catch (awtexception e) {
e.printstacktrace();
}
}
});
}
}
/*
* 截图窗口
*/
class screenshotwindow extends jwindow
{
private int orgx, orgy, endx, endy;
private bufferedimage image=null;
private bufferedimage tempimage=null;
private bufferedimage saveimage=null;
private toolswindow tools=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() {
@override
public void mousepressed(mouseevent e) {
//鼠标松开时记录结束点坐标,并隐藏操作窗口
orgx = e.getx();
orgy = e.gety();
if(tools!=null){
tools.setvisible(false);
}
}
@override
public void mousereleased(mouseevent e) {
//鼠标松开时,显示操作窗口
if(tools==null){
tools=new toolswindow(screenshotwindow.this,e.getx(),e.gety());
}else{
tools.setlocation(e.getx(),e.gety());
}
tools.setvisible(true);
tools.tofront();
}
});
this.addmousemotionlistener(new mousemotionadapter() {
@override
public void mousedragged(mouseevent e) {
//鼠标拖动时,记录坐标并重绘窗口
endx = e.getx();
endy = e.gety();
//临时图像,用于缓冲屏幕区域放置屏幕闪烁
image tempimage2=createimage(screenshotwindow.this.getwidth(),screenshotwindow.this.getheight());
graphics 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防止width或height0
g.setcolor(color.blue);
g.drawrect(x-1, y-1, width+1, height+1);
//减1加1都了防止图片矩形框覆盖掉
saveimage = image.getsubimage(x, y, width, height);
g.drawimage(saveimage, x, y, null);
screenshotwindow.this.getgraphics().drawimage(tempimage2,0,0,screenshotwindow.this);
}
});
}
@override
public void paint(graphics 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 filter = new filenameextensionfilter("jpg", "jpg");
jfc.setfilefilter(filter);
//初始化一个默认文件(此文件会生成到桌面上)
simpledateformat sdf = new simpledateformat("yyyymmddhhmmss");
string filename = sdf.format(new date());
file filepath = filesystemview.getfilesystemview().gethomedirectory();
file defaultfile = new file(filepath + file.separator + filename + ".jpg");
jfc.setselectedfile(defaultfile);
int flag = jfc.showsavedialog(this);
if(flag==jfilechooser.approve_option){
file 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);
}
}
}
/*
* 操作窗口
*/
class toolswindow extends jwindow
{
private screenshotwindow parent;
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 toolbar=new jtoolbar("java 截图");
//保存按钮
jbutton savebutton=new jbutton(new imageicon("images/save.gif"));
savebutton.addactionlistener(new actionlistener() {
@override
public void actionperformed(actionevent e) {
try {
parent.saveimage();
} catch (ioexception e1) {
e1.printstacktrace();
}
}
});
toolbar.add(savebutton);
//关闭按钮
jbutton closebutton=new jbutton(new imageicon("images/close.gif"));
closebutton.addactionlistener(new actionlistener() {
@override
public void actionperformed(actionevent e) {
system.exit(0);
}
});
toolbar.add(closebutton);
this.add(toolbar,borderlayout.north);
}
}
上一篇: Java中方法的重写与成员变量的隐藏