[英]Screenshot capture in Window 8 OS
为了在我的 Java 应用程序中捕获屏幕截图,我编写了以下代码
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "png", new File("resources/img/screenshot.png"));
这工作成功并捕获屏幕截图,但这在 Windows 8 操作系统中不起作用。 还有谁遇到过这种类型的问题并获得解决方案?
我的应用程序安装到程序文件夹中,而 Windows 8 不允许在那里写入,我现在如何写入?
不要把它写在那里! 操作系统制造商以及 Sun/Oracle 多年来一直在说不要将文件写入应用程序的安装目录。 它不仅是编写它们的错误位置,而且正如您发现的那样,它不为典型的 Java 应用程序提供写入权限。
而是将屏幕截图放在user.home
例如在这个答案中看到的。
您可以使用以下代码在本地计算机中不写入文件的情况下执行此操作
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] imageByte = null;
try
{
//To Get the the size of the screen.
Rectangle screenRect = new Rectangle(Toolkit
.getDefaultToolkit().getScreenSize());
//To Store the scrreen shot in buffer.
BufferedImage capture = new Robot()
.createScreenCapture(screenRect);
//To Save the image on specific location.
ImageIO.write(capture, "png",bos);
bos.flush();
imageByte=bos.toByteArray();
bos.close();
// File file = new File("resources/img/screenshot.png");
MultipartEntity mpEntity = new MultipartEntity();
// ContentBody cfBody = new FileBody(file);
ContentBody cfBody = new ByteArrayBody(imageByte,"screenshot.png");
mpEntity.addPart("screenshot", cfBody);
}
直接发送字节数组代替图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.