简体   繁体   English

Java ImageIO-相同图像到特定路径

[英]Java ImageIO - same image to a specific path

Im implementing a screenshot function and currently it just saves to the project file but it would be nice if it saved to a specific file or location such as the desktop. 我正在实现一个屏幕截图功能,当前它只是保存到项目文件中,但是如果保存到特定文件或位置(例如桌面)中,那就太好了。

Currently: 目前:

try {
   imageId = random.nextInt(9999);
   ImageIO.write(MainGame.image, "png" , new File("Sinecure_" + imageId + ".png/"));
   System.out.println("Image Saved as Sinecure_" + imageId);
} catch (IOException e) {
   e.printStackTrace();
}

you can find the user's home directory using this code snippet: 您可以使用以下代码段找到用户的主目录:

String userHome = System.getProperty( "user.home" );

then you could (depending on the operating system) construct the file path like so: 那么您可以(取决于操作系统)构造文件路径,如下所示:

String fullPath = userHome+File.seperator+"Desktop"+File.seperator+"Sinecure_" + imageId + ".png";
ImageIO.write(MainGame.image, "png" , new File(fullPath));

the above is for windows 7. you will need to adjust for other operating systems, of course. 以上是针对Windows 7的。当然,您需要针对其他操作系统进行调整。 the easiest way to detect what OS youre running under would be another system property: 检测您在哪个操作系统下运行的最简单方法是另一个系统属性:

String OS = System.getProperty("os.name")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM