如何在javafx中保存所选图像?

时间:2018-01-06 06:03:55

标签: java javafx selection

like this blank image is saving

 import java.io.File;
    import java.io.IOException;
    import javafx.application.Application;
    import javafx.embed.swing.SwingFXUtils;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.SnapshotParameters;
    import javafx.scene.effect.Light.Point;
    import javafx.scene.image.WritableImage;
    import javafx.scene.input.KeyCode;
    import javafx.scene.input.KeyEvent;
    import javafx.scene.layout.Pane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.web.WebEngine;
    import javafx.scene.web.WebView;
    import javafx.stage.Stage;
    import javax.imageio.ImageIO;

public class JavaFxSelectPlay extends Application {

    public static void main(String[] args) {
        launch();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane root = new Pane();
        WebView wv = new WebView();
        WebEngine Img = wv.getEngine();
        Img.load("https://app.leadlock.pro/upload/69/1019/images/welcome.jpeg");
        final Rectangle selection = new Rectangle();
        final Point anchor = new Point();

        wv.setOnMousePressed(event -> {
            anchor.setX(event.getX());
            anchor.setY(event.getY());
            selection.setX(event.getX());
            selection.setY(event.getY());
            selection.setFill(null); // transparent 
            selection.setStroke(Color.BLACK); // border
            selection.getStrokeDashArray().add(10.0);
         root.getChildren().add(selection);
        });

        wv.setOnMouseDragged(event -> {
            selection.setWidth(Math.abs(event.getX() - anchor.getX()));
            selection.setHeight(Math.abs(event.getY() - anchor.getY()));
            selection.setX(Math.min(anchor.getX(), event.getX()));
            selection.setY(Math.min(anchor.getY(), event.getY()));
        });

        wv.setOnMouseReleased(event -> {
            // Do what you want with selection's properties here
            System.out.printf("X: %.2f, Y: %.2f, Width: %.2f, Height: %.2f%n", 
                    selection.getX(), selection.getY(), selection.getWidth(), selection.getHeight());
          //  root.getChildren().remove(selection);
           // selection.setWidth(0);
          //  selection.setHeight(0);
        });
          wv.addEventFilter(KeyEvent.KEY_RELEASED, (KeyEvent e1) -> {
            if (e1.getCode() == KeyCode.SPACE ) {
     selection.setFill(Color.WHITE); // transparent 
            }
            });
          wv.setOnKeyPressed(new EventHandler<KeyEvent>() {
 public void handle(final KeyEvent keyEvent) {
   if (keyEvent.getCode() == KeyCode.F5) {
    System.out.println("F5 pressed");
    //Stop letting it do anything else
   // WritableImage croppedImage = selection.snapshot(null, null);


           WritableImage image = selection.snapshot(new SnapshotParameters(), null);

    // TODO: probably use a file chooser here
     File file = new File("C:/temp/snapshot.jpg");

    try {
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), "jpg", file);
    } catch (IOException e) {
        // TODO: handle exception here
    }
}
            System.out.println("snapshot saved: " );
   }
 });

        root.getChildren().add(wv);
        Scene scene = new Scene(root, 300, 200);
        primaryStage.setScene(scene);
        primaryStage.setTitle("Primary Stage");
        primaryStage.show();
    }
 }

此代码我试过并且运行正常,但保存的图像以白色保存,未保存原始选择的图像,所以我怎么能这样做,请帮助我需要保存所选图像,我做了白色但是不是白色而是选择的图像所以请告诉我如何做以及我做错了什么。

1 个答案:

答案 0 :(得分:1)

这里似乎有两件事情。

首先,您可能想要拍摄根的快照并使用选择矩形指定要拍摄的快照的哪个部分,而不是拍摄用于标记选区的Rectangle的快照。所以,而不是写

        WritableImage image = selection.snapshot(new SnapshotParameters(), null);

尝试写作

        SnapshotParameters params = new SnapshotParameters();
        params.setViewport(
            new Rectangle2D(
                selection.getX(),
                selection.getY(),
                selection.getWidth(),
                selection.getHeight()));

        root.getChildren().remove(selection);
        WritableImage image = root.snapshot(params, null);

我还从根目录中删除了矩形,使其不会出现在输出图像中。

第二件事就是图像呈粉红色。这似乎是existing issue which is supposed to have been fixed in Java 8但我仍然可以在Java 8更新152中重现它。在我链接到的问题中调整代码,你将不得不替换行

            ImageIO.write(SwingFXUtils.fromFXImage(image, null), "jpg", file);

            BufferedImage bufferedImage = SwingFXUtils.fromFXImage(image, null);
            BufferedImage imageRGB = new BufferedImage(
                bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.OPAQUE);
            Graphics2D graphics = imageRGB.createGraphics();
            graphics.drawImage(bufferedImage, 0, 0, null);
            ImageIO.write(imageRGB, "jpg", file);