gpt4 book ai didi

java - 打印部分屏幕

转载 作者:行者123 更新时间:2023-12-01 17:23:03 24 4
gpt4 key购买 nike

是否可以打印鼠标周围的部分屏幕?我尝试使用:

Toolkit tool = Toolkit.getDefaultToolkit();
Dimension d = tool.getScreenSize();
Rectangle rect = new Rectangle(d);
Robot robot = new Robot();
File f = new File("screenshot.jpg");
BufferedImage img = robot.createScreenCapture(rect);
ImageIO.write(img,"jpeg",f);

但它打印所有屏幕,我可以看到我可以设置矩形的大小,但我不知道如何将矩形居中,使其位于鼠标周围。

最佳答案

public static BufferedImage printScrAroundCursor(int width, int height)
{
Toolkit tool = Toolkit.getDefaultToolkit();
Robot robot = new Robot();

PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();

int topLeftX = Math.max(0, x - (width / 2));
int topLeftY = Math.max(0, y - (height / 2));
if (topLeftX + width > tool.getScreenSize().getWidth())
width = tool.getScreenSize().getWidth() - topLeftX;
if (topLeftX + width > tool.getScreenSize().getHeight())
width = tool.getScreenSize().getHeight() - topLeftY;
return robot.createScreenCapture(new Rectangle(topLeftX , topLeftY , width, height));
}

关于java - 打印部分屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17208575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com