如何消除使用 Robot 拍摄的图像上的黑色背景?
在“按钮单击”中,我已经实现了代码,当我第一次单击按钮时,它将获取 x, y 位置值,当我第二次单击按钮时,它将获取 x1,y1 值并捕获图像。但不知何故,它还为原始图片添加了黑色背景。我怎样才能避免这种情况?
Toolkit tool = Toolkit.getDefaultToolkit();
c++;
Dimension d = tool.getScreenSize();
if(c==1)
{
x = MouseInfo.getPointerInfo().getLocation().x;
y = MouseInfo.getPointerInfo().getLocation().y;
}
if(c==2)
{
int x1= MouseInfo.getPointerInfo().getLocation().x;
int y1= MouseInfo.getPointerInfo().getLocation().y;
Rectangle rect = new Rectangle(x,y,x1,y1);
Robot robot = new Robot();
String J="Screen";
J=J+""+i;
//*************
String ext = ".jpg";
String path = loc+J+ ext;
File f = new File(path);
i++;
Thread t1 = new Thread();
t1.sleep(100);
BufferedImage img = robot.createScreenCapture(rect);
// img.createGraphics();
ImageIO.write(img,"jpeg",f);
tool.beep();
c=0;
x=0;
y=0;
x1=0;
y1=0;
}
In Button click I have implemented the code, when I click the button first time it will get the x, y postion value, when I click the button the second time it will get the x1,y1 value and capture the image. But somehow it is additionally adding a black background to the original picture. How can I avoid this?
Toolkit tool = Toolkit.getDefaultToolkit();
c++;
Dimension d = tool.getScreenSize();
if(c==1)
{
x = MouseInfo.getPointerInfo().getLocation().x;
y = MouseInfo.getPointerInfo().getLocation().y;
}
if(c==2)
{
int x1= MouseInfo.getPointerInfo().getLocation().x;
int y1= MouseInfo.getPointerInfo().getLocation().y;
Rectangle rect = new Rectangle(x,y,x1,y1);
Robot robot = new Robot();
String J="Screen";
J=J+""+i;
//*************
String ext = ".jpg";
String path = loc+J+ ext;
File f = new File(path);
i++;
Thread t1 = new Thread();
t1.sleep(100);
BufferedImage img = robot.createScreenCapture(rect);
// img.createGraphics();
ImageIO.write(img,"jpeg",f);
tool.beep();
c=0;
x=0;
y=0;
x1=0;
y1=0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果这是在
mouseClicked(MouseEvent event)
方法中(在MouseListener
),为什么使用:您可能应该使用
MouseListener
上的方法。 com/javase/6/docs/api/java/awt/event/MouseEvent.html" rel="nofollow">MouseEvent
:或者
可能是
MouseInfo
方法给你不正确的值。If this is in a
mouseClicked(MouseEvent event)
method (in aMouseListener
), why are you using:You should probably be using the methods on
MouseEvent
:or
Perhaps the
MouseInfo
methods are giving you incorrect values.我想我发现了这个问题。
矩形
采用x
和y
起始位置,以及height
和width
。看起来您给了它 2 个 x/y 点。试试这个:
I think I found the issue. The constructor for
Rectangle
takes anx
and ay
starting position, and aheight
and awidth
. It looks like you are giving it 2 x/y points.Try this instead: