1

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();
    }
 }

this code i tried and its running fine but the image that is saved is saved with white color its not saved the orginal selected image so how can i do it please help i need to save the selected image and i had done white but not with white color but the selected image so please tell me how to do it and what im doing wrong.

1 Answers1

2

It seems there are two things going on here.

Firstly, instead of taking a snapshot of the Rectangle you are using to mark the selection, you probably want to take a snapshot of the root and use the selection rectangle to specify what part of the snapshot to take. So, instead of writing

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

try writing

        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);

I've also removed the rectangle from the root so that it doesn't appear in the output image.

The second thing that happens is that the image comes out pink. This appears to be an existing issue which is supposed to have been fixed in Java 8 but I can still reproduce it in Java 8 update 152. Adapting the code in the question I linked to, you would have to replace the line

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

with

            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);
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • thanks Luke the saving problem i had solved before ur post the prob is to select many area at same time in image for refrence i had updated image in my question – Prashant Kushwaha Jan 08 '18 at 08:11