此绘图区域用画布绘制图像
Canvas canvas= new Canvas();
canvas.setHeight(500);
canvas.setWidth(700);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setFill(Color.WHITE);
gc.fillRect(1, 1, canvas.getWidth() - 2, canvas.getHeight() - 2);
我用这个方法将绘图画布导出到imagem.pgn或快照及其正确的
public void snapshotCanvasImageToPNG(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
try {
WritableImage writableImage = new WritableImage((int) canvas.getWidth(), (int) canvas.getHeight());
canvas.snapshot(null, writableImage);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
ImageIO.write(renderedImage, "png", file);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我对使这些方法打开编辑并将画布绘图保存到file.pgm有很多疑问 我做了这个,但它很难完成
public void openFilePGM(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
//doubt here
}
}
public void saveFilePGM(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
//doubt here
}
}
答案 0 :(得分:1)
在JavaFX中使用PGM图像的唯一方法是通过Java Advanced Imaging或其他第三方插件提到的ImageJ或@VGR。
请检查包含此类IO功能代码的this plugin。
您可能还想查看this answer,它可以向您展示使用JavaFX可以轻松处理的格式。