手机版
你好,游客 登录 注册
背景:
阅读新闻

基于Java Robot类的屏幕捕获工具

[日期:2006-10-10] 来源:  作者: [字体: ]
Capture允许你把捕获的图像保存为一个jpeg文件。你通过一个保存文件选择器指定文件名,选择器由Capture类的构造函数创建:// Construct a save file-chooser. Initialize the starting directory to
// the current directory, do not allow the user to select the "all files"
// filter, and restrict the files that can be selected to those ending
// with .jpg or .jpeg extensions.
final JFileChooser fcSave = new JFileChooser ();
fcSave.setCurrentDirectory (new File (System.getProperty ("user.dir")));
fcSave.setAcceptAllFileFilterUsed (false);
fcSave.setFileFilter (new ImageFileFilter ());
为了限制文件选择器的选择是文件夹或者是以.jpg或.jpeg为后缀的文件,就使用了ImageFileFilter类的一个实例作为保存时文件选择器的文件过滤器。该方法对于任何非文件夹和后缀名非.jpg/.jpeg的文件都返回false:public boolean accept (File f)
{
// Allow the user to select directories so that the user can navigate the
// file system.
if (f.isDirectory ())
return true;
// Allow the user to select files ending with a .jpg or a .jpeg
// extension.
String s = f.getName ();
int i = s.lastIndexOf ('.');
if (i > 0 && i
当你选择了Save As…菜单项时,它的监听器就会显示一个保存文件选择器。假定你没有退出选择器,监听器就会确保你选择的文件名是以.jpg或.jpeg为后缀名。继续,监听器会确定文件是否存在,这样你就不会无意中覆盖一个存在的文件。// Present the "save" file-chooser without any file selected.
// If the user cancels this file-chooser, exit this method.
fcSave.setSelectedFile (null);
if (fcSave.showSaveDialog (Capture.this) !=
JFileChooser.APPROVE_OPTION)
return;
// Obtain the selected file. Validate its extension, which
// must be .jpg or .jpeg. If extension not present, append
// .jpg extension.
File file = fcSave.getSelectedFile ();
String path = file.getAbsolutePath ().toLowerCase ();
if (!path.endsWith (".jpg") && !path.endsWith (".jpeg"))
file = new File (path += ".jpg");
// If the file exists, inform the user, who might not want
// to accidentally overwrite an existing file. Exit method
// if the user specifies that it is not okay to overwrite
// the file.
if (file.exists ())
{
int choice = JOptionPane.
showConfirmDialog (null,
"Overwrite file?",
"Capture",
JOptionPane.
YES_NO_OPTION);
if (choice == JOptionPane.NO_OPTION)
return;
}
如果文件不存在或者你允许覆盖已经存在的文件,监听器就会将捕获的内容保存为一个选择的文件。为了完成这个任务,监听器使用Java的ImageIO框架选择一个jpeg writer,指定文件作为writer的目标,设置writer的压缩品质为95%,然后把图像写入到文件中。ImageWriter writer = null;
ImageOutputStream ios = null;
try
{
// Obtain a writer based o­n the jpeg format.
Iterator iter;
iter = ImageIO.getImageWritersByFormatName ("jpeg");
// Validate existence of writer.
if (!iter.hasNext ())
{
showError ("Unable to save image to jpeg file type.");
return;
}
// Extract writer.
writer = (ImageWriter) iter.next();
// Configure writer output destination.
ios = ImageIO.createImageOutputStream (file);
writer.setOutput (ios);
// Set jpeg compression quality to 95%.
ImageWriteParam iwp = writer.getDefaultWriteParam ();
iwp.setCompressionMode (ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality (0.95f);
// Write the image.
writer.write (null,
new IIOImage ((BufferedImage)
ia.getImage (), null, null),
iwp);
}
catch (IOException e2)
{
showError (e2.getMessage ());
}
finally
{
try
{
// Cleanup.
if (ios != null)
{
ios.flush ();
ios.close ();
}
if (writer != null)
writer.dispose ();
}
catch (IOException e2)
{
}
}
让代码自己清理一直是一个不错的主意。我把ImageIO的清理代码放在了finally子句中,以至于无论是正常结束还是抛出异常,它都可以执行。
总结
Capture限制了捕获的内容只能在主屏幕设备内。你可能想增强Capture来捕获所有附加屏幕设备(或许是一个巨大的虚拟屏幕)的内容。增强之一,你需要包含下面的代码,它捕获所有屏幕的内容,将它和Capture.java已经存在的代码集成。GraphicsEnvironment graphenv = GraphicsEnvironment.getLocalGraphicsEnvironment ();
GraphicsDevice [] screens = graphenv.getScreenDevices ();
BufferedImage [] captures = new BufferedImage [screens.length];
for (int i = 0; i
把以上代码放到Capture菜单项的动作监听器内。然后先引入代码创建一个bigScreen要引用的足够大的BufferedImage,它可以保存被captures数组引用的所有BufferedImage内容;接着引入代码把它们的绘制到bigScreen中。Capture现在成为了多屏幕捕获器就好像是一个单屏幕捕获器。
关于作者
Jeff Friesen是一个自由软件开发者和教育家,特别是在C、C++和Java技术领域linux
相关资讯      
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款