该绘图区域使用画布绘制图像

        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
    }
}

最佳答案

使用JavaFX中的PGM图像的唯一方法是通过Java Advanced Imaging提到的ImageJ或@VGR或另一个第三方插件。

请检查包含此类IO功能代码的this plugin

您可能还想看看this answer,它可以向您显示可以轻松使用JavaFX处理的格式。

关于java - 在JavaFX中的file.pgm上打开,编辑和保存图像 Canvas ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24314668/

10-10 11:39