gpt4 book ai didi

java - 如何在javafx中保存选定的图像?

转载 作者:行者123 更新时间:2023-11-30 06:21:11 26 4
gpt4 key购买 nike

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

我尝试过这段代码,它运行良好,但是保存的图像是以白色保存的,它没有保存原始选定的图像,所以我该怎么做,请帮助我需要保存选定的图像,我已经做了白色,但是不是白色,而是所选的图像,所以请告诉我该怎么做以及我做错了什么。

最佳答案

这里似乎发生了两件事。

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

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

关于java - 如何在javafx中保存选定的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48124777/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com