[英]Linux: How to save a image using ImageIO.write()?
I am writing a screen capture program on Linux using Java. 我正在使用Java在Linux上编写一个屏幕捕获程序。 How can I use ImageIO.write()
like I used it on windows like: 我如何使用ImageIO.write()
就像我在Windows上使用它一样:
ImageIO.write(screenshot, "png", new File("c:/output.png"));
On Linux there is no "C:\\" drive. 在Linux上没有“C:\\”驱动器。 Instead, your drive is mounted at a mount point (usually /
). 而是将驱动器安装在安装点 (通常为/
)。 You could write to your home directory (equivalent of Win7's C:\\Users\\yourusername\\
) with either of these: 您可以使用以下任一方法写入您的主目录(相当于Win7的C:\\Users\\yourusername\\
):
ImageIO.write(screenshot, "png", new File("/home/yourusername/output.png"));
ImageIO.write(screenshot, "png", new File("~/output.png"));
or to the temp folder (if you have permissions) with: 或者临时文件夹(如果你有权限):
ImageIO.write(screenshot, "png", new File("/tmp/output.png"));
You could also write to the current directory with a simple: 您也可以使用简单的方法写入当前目录:
ImageIO.write(screenshot, "png", new File("output.png"));
To find your drive's mount point, run df -h
in a terminal to see all mounted drives. 要查找驱动器的安装点,请在终端中运行df -h
以查看所有已安装的驱动器。
If you're writing a screen capture program , then you probably want to use a FileChooser to allow the user to choose where to output the file. 如果您正在编写屏幕捕获程序 ,那么您可能希望使用FileChooser来允许用户选择输出文件的位置。
Here's a simple example of how you could implement one: 这是一个如何实现一个的简单示例:
JFileChooser jfc = new JFileChooser();
int returnVal = jfc.showSaveDialog();
if(returnVal == JFileChooser.APPROVE_OPTION) {
File outputFile = jfc.getSelectedFile();
ImageIO.write(screenshot, "png", outputFile);
}
This will also help to make your code fully cross-platform, instead of hard-coding platform-specific paths into the program. 这也有助于使您的代码完全跨平台,而不是将特定于平台的特定路径硬编码到程序中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.