这是一个 Java 版的截图程序
复制代码代码如下:
pacote com.hongyuan.test;
importar java.awt.awtexception;
importar java.awt.borderlayout;
importar java.awt.color;
importar java.awt.dimension;
importar java.awt.eventqueue;
importar java.awt.flowlayout;
importar java.awt.graphics;
importar java.awt.image;
importar java.awt.rectangle;
importar java.awt.robot;
importar java.awt.toolkit;
importar java.awt.event.actionEvent;
importar java.awt.event.actionListener;
importar java.awt.event.mouseAdapter;
importar java.awt.event.mouseevent;
importar java.awt.event.mousemotionAdapter;
importar java.awt.image.bufferiMage;
importar java.awt.image.rescaleop;
importar java.io.file;
importar java.io.ioException;
importar java.text.simpledateFormat;
importar java.util.date;
importar javax.imageio.imageio;
importar javax.swing.imageicon;
importar javax.swing.jbutton;
importar javax.swing.jfileChooser;
importar javax.swing.jtoolbar;
importar javax.swing.jwindow;
importar javax.swing.FileChooser.FileNameExtensionFilter;
importar javax.swing.FileChooser.FilesystemView;
classe pública Screenshotthest {
public static void main (string [] args) {
EventQueue.invokelater (new Runnable () {
@Override
public void run () {
tentar {
ScreenshotWindow ssw = new screenShotWindow ();
ssw.setVisible (true);
} catch (awtexception e) {
E.PrintStackTrace ();
}
}
});
}
}
/*
* 截图窗口
*/
Class de aula de screenshotWindow
{
Private Int Orgx, Orgy, Endx, Endy;
Imagem bufferImage privada = nulo;
private bufferImage tempimage = nulo;
private bufferImage saveImage = nulo;
Toolswindow Toolswindrow Ferramentas = NULL;
Public screenShotWindow () lança awtexception {
// 获取屏幕尺寸
Dimensão d = ferramentakit.getDefaultToolkit (). GetScreensize ();
this.setbounds (0, 0, d.width, d.Height);
// 截取屏幕
Robô robot = new robot ();
imagem = robot.createsCreencapture (novo retângulo (0, 0, d.width, d.height));
this.addmouselistener (new mouseAdapter () {
@Override
public void MousePressed (mouseevent e) {
// 鼠标松开时记录结束点坐标 , 并隐藏操作窗口
orgx = e.getx ();
orgy = e.gety ();
if (ferramentas! = null) {
ferramentas.setVisible (false);
}
}
@Override
public void mousereleden (mouseevent e) {
// 鼠标松开时 , 显示操作窗口
if (ferramentas == null) {
ferramentas = new Toolswindow (screenshotwindow.This, e.getx (), e.gety ());
}outro{
ferramentas.setLocation (e.getx (), e.gety ());
}
ferramentas.setVisible (true);
ferramentas.tofront ();
}
});
this.addmousemotionListener (new MouseMotionAdapter () {
@Override
public void Mousedragged (mouseevent e) {
// 鼠标拖动时 , 记录坐标并重绘窗口
endX = e.getx ();
endy = e.gety ();
// 临时图像 , 用于缓冲屏幕区域放置屏幕闪烁
Imagem tempimage2 = createImage (screenshotwindow.this.getwidth (), screenshotwindow.this.gethight ());
Gráficos g = tempimage2.getGraphics ();
G.Drawimage (Tempimage, 0, 0, NULL);
int x = math.min (orgx, endX);
int y = math.min (orgia, endy);
int width = math.abs (endx - orgx) +1;
int height = math.abs (endy - orgy) +1;
// 加上 1 防止 largura 或 altura0
g.setColor (color.blue);
G.DrawRect (X-1, Y-1, Largura+1, Altura+1);
// 减 1 加 1 都了防止图片矩形框覆盖掉
saveImage = image.getSubimage (x, y, largura, altura);
G.Drawimage (salvimage, x, y, nulo);
ScreenShotWindow.This.getgraphics ().
}
});
}
@Override
public void Paint (Gráfico G) {
Rescaleop RO = novo Rescaleop (0,8F, 0, NULL);
tempimage = ro.filter (imagem, nulo);
G.Drawimage (Tempimage, 0, 0, isto);
}
// 保存图像到文件
public void saveImage () lança ioexception {
JfileChooser jfc = new jfileChooser ();
jfc.setDialogtitle ("保存");
// 文件过滤器 , 用户过滤可选择文件
FilenameextensionFilter filtro = new FileNameExtensionFilter ("JPG", "JPG");
jfc.setFileFilter (filtro);
// 初始化一个默认文件 (此文件会生成到桌面上)
SimpledateFormat sdf = new SimpleDateFormat ("yyyymmddhhmmss");
String filename = sdf.format (new Date ());
Arquivo filepath = filesystemView.getFilesystemView (). Gethomedirectory ();
Arquivo defaultfile = novo arquivo (filepath + file.separator + nome do arquivo + ".jpg");
jfc.setSelectedFile (DefaultFile);
int flag = jfc.showsAvedialog (this);
if (flag == jfileChooser.approve_option) {
Arquivo de arquivo = jfc.getSelectedFile ();
String path = file.getPath ();
// 检查文件后缀 , 放置用户忘记输入后缀或者输入不正确的后缀
if (! (path.endswith (". jpg") || path.endswith (". jpg"))) {
caminho+= ". jpg";
}
// 写入文件
Imageio.write (saveImage, "jpg", novo arquivo (caminho));
System.Exit (0);
}
}
}
/*
* 操作窗口
*/
Classe Toolswindow estende JWindow
{
Screenshotwindow Parent ScreenShotwindow privado;
Public ToolSwindow (ScreenShotWindow Parent, int x, int y) {
this.parent = pai;
this.init ();
this.setLocation (x, y);
this.pack ();
this.setVisible (true);
}
private void init () {
this.setLayout (new BorderLayout ());
Barra de ferramentas jtoolbar = new jtoolbar ("java 截图");
// 保存按钮
Jbutton saveButton = new jbutton (novo imageicon ("imagens/save.gif"));
saveButton.addactionListener (new ActionListener () {
@Override
public void ActionPormed (ActionEvent E) {
tentar {
parent.SaveImage ();
} catch (ioexception e1) {
e1.printStackTrace ();
}
}
});
ToolBar.add (salvamento);
// 关闭按钮
Closebutton jbutton = novo jbutton (novo imageicon ("imagens/close.gif"));
closebutton.addactionListener (new ActionListener () {
@Override
public void ActionPormed (ActionEvent E) {
System.Exit (0);
}
});
ToolBar.Add (Blokbutton);
this.add (barra de ferramentas, borderlayout.north);
}
}