[英]JavaFX: Get image from FileChooser and save it in a byte[]
I want to select an image using FileChooser
and then save the selected image in a byte[] variable, I open the dialog我想使用FileChooser
select 图像,然后将所选图像保存在 byte[] 变量中,我打开对话框
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.showOpenDialog(new Stage());
Now, How can I get the image file from FileChooser
and save it in byte[] variable?现在,如何从FileChooser
获取图像文件并将其保存在 byte[] 变量中?
You can use Files.readAllBytes(Path path)
:您可以使用Files.readAllBytes(Path path)
:
FileChooser fileChooser = new FileChooser();
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PNG", "*.png"));
File pngImage = fileChooser.showOpenDialog(window);
if (pngImage != null) {
try {
byte[] imageBytes = Files.readAllBytes(pngImage.toPath());
} catch (IOException e) {
System.err.println("File couldn't be read to byte[].");
}
}
An alternative: IOUtils
:另一种选择: IOUtils
:
byte[] bytes = IOUtils.toByteArray(new FileInputStream(pngImage));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.