第一页
第二页
第三页
主窗口的背景像素不是捕获的图像的一部分;就不可能把它们包含在修剪的图片内。因此,无论何时把背景像素包含在修剪区域内,操作都会失败,并且会给出一个“Out of bounds”错误信息。
修剪操作由ImageArea的public Boolean crop()方法处理。如果完成了修剪或者没有选择子图像(当没有选中内容时调用这个方法是非常方便的)该方法(如下所示)返回true。如果在选择区域中包含了背景像素则返回false。
public boolean crop ()
{
// There is nothing to crop if the selection rectangle is only a single
// point.
if (srcx == destx && srcy == desty)
return true;
// Assume success.
boolean succeeded = true;
// Compute upper-left and lower-right coordinates for selection rectangle
// corners.
int x1 = (srcx < destx) ? srcx : destx;
int y1 = (srcy < desty) ? srcy : desty;
int x2 = (srcx > destx) ? srcx : destx;
int y2 = (srcy > desty) ? srcy : desty;
// Compute width and height of selection rectangle.
int width = (x2-x1)+1;
int height = (y2-y1)+1;
// Create a buffer to hold cropped image.
BufferedImage biCrop = new BufferedImage (width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = biCrop.createGraphics ();
// Perform the crop operation.
try
{
BufferedImage bi = (BufferedImage) image;
BufferedImage bi2 = bi.getSubimage (x1, y1, width, height);
g2d.drawImage (bi2, null, 0, 0);
}
catch (RasterFormatException e)
{
succeeded = false;
}
g2d.dispose ();
if (succeeded)
setImage (biCrop); // Implicitly remove selection rectangle.
else
{
// Prepare to remove selection rectangle.
srcx = destx;
srcy = desty;
// Explicitly remove selection rectangle.
repaint ();
}
return succeeded;
}
crop()方法调用BufferedImage的public BufferedImage
getSubimage(int x, int y, int w, int
h)方法摘取选择区域内的子图像。如果该方法的参数没有指定BufferedImage内的图像,它就会抛出一个
java.awt.image.RasterFormatException,因此就会返回false。
图像保存
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 < s.length ()-1)
{
String ext = s.substring (i+1).toLowerCase ();
if (ext.equals ("jpg") || ext.equals ("jpeg"))
return true;
}
// Nothing else can be selected.
return false;
}
当你选择了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.util.Robot类来捕获屏幕,可以实现对屏幕一个矩形区域的捕获,通过这个类,我们也可以实现一个远程桌面控制的程序。
Java Robot对象实现服务器屏幕远程监视
Java中利用Robot实现模拟按键,按下和松开。
Java Robot对象实现服务器屏幕远程监视
java的robot类实现屏幕截图,java的robot类实现屏幕截图。
用java robot实现超简单爬虫
RobotFramework中实现PageObject模式的轻量级关键字库_Python_RobotFramework.zip
robotframework-selenium2library-3.0.0安装包,已试,可用
Servo robot 激光跟踪传感器是目前世界上最先进的跟踪传感器,可实现6-D跟踪,与kuka机器人有良好的接口。其详细性如下: Servo-Robot全文共3页,当前为第1页。 Servo-Robot全文共3页,当前为第1页。 Servo-Robot...
B-ROBOT_EVO2-master
RobotFramework实现嵌套For循环 RobotFramework实现嵌套For循环 现在,RobotFramework已不再⽀持 :FOR \ 的写法。 ⼀般循环 FOR ${i} IN RANGE 10 Log ${i} END 当FOR循环内嵌套有另⼀个循环时,内层循环不可识别。 ...
Exploring Robotics with ROBOTIS Systems, Second Edition By Dr. Chi N. Thai English | PDF | 2017 | 350 Pages | ISBN : 3319598309 | 29.6 MB This 2nd edition textbook has been expanded to include of 175...
一个深入的分步教程,用于使用来自robot_localization 的扩展卡尔曼滤波器节点实现传感器融合!这里解释了协方差和卡尔曼滤波器等基本概念! 本教程特别有用,因为还没有一个完整的端到端实现教程,用于使用 robots...
网络安全_基于C++开发的网络安全检测工具WebRobot实现
robotframework-selenium2library-1.7.4.win-amd64.exe
RobotFramework+Selenium2,从别人那边下载的,看着好用
robotframework-selenium2library-1.7.3.win-amd64.exe
fanuc robot的开发介绍文档,类似于SDK,介绍文档里面描述了大概可以进行哪些数据的的取值
本篇文章是对Java中用Robot实现控制鼠标和键盘的方法进行了详细的分析介绍,需要的朋友参考下
robotframework调⽤python类⽅法_(五)RobotFramework基 础。... *** Test Cases *** test case13 Comment 这是注释 #这也是注释 如果你熟悉 Python 编程语⾔,那么 Robot Framework ⼏乎没有实现