我真的不知道您的需要,但在initialize方法中创建图像对我来说没有任何意义。
一个快速解决方案可以是以下解决方案:
public class FXMLDocumentController extends BorderPane {
@FXML
private MediaView mediaView;
@FXML
private VBox container;
private Media me;
private MediaPlayer mp;
/**
* Constructor.
*/
public FXMLDocumentController() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Media.fxml"));
loader.setRoot(this);
loader.setController(this);
try {
loader.load();
} catch(IOException e) {
System.out.println("An error occurs while trying to load the media component." + e);
}
}
@FXML
private void initialize() {
// String s = "/home/mustafa987/Videos/sampels/video10.mp4";
// me = new Media(new File(s).toURI().toString());
// mp = new MediaPlayer(me);
// mediaView.setMediaPlayer(mp);
// mp.play();
// mp.setRate(1);
}
@FXML
private void handleSnap() throws IOException {
System.out.println("User asks snap.");
WritableImage img = container.snapshot(new SnapshotParameters(), null);
BufferedImage bufImg = SwingFXUtils.fromFXImage(img, null);
ImageIO.write(bufImg, "png", new File("./save/snap.png"));
}
}
它拍摄
VBox
包含
MediaView
因为我没有可显示的媒体,但它也可以使用
MediaView
此组件继承自
Node
。
由于您没有提供,这是我使用的FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.media.MediaView?>
<?import javafx.scene.layout.VBox?>
<fx:root xmlns:fx="http://javafx.com/fxml/1" type="BorderPane">
<top>
<GridPane >
<children>
<Label GridPane.rowIndex="0" GridPane.columnIndex="0" text="Snap frame" />
<HBox GridPane.rowIndex="0" GridPane.columnIndex="1" alignment="CENTER">
<children>
<Button text="Take snap.." onAction="#handleSnap" />
</children>
</HBox>
</children>
<columnConstraints>
<ColumnConstraints percentWidth="90.0" />
<ColumnConstraints percentWidth="10.0" />
</columnConstraints>
</GridPane>
</top>
<center>
<VBox fx:id="container" style="-fx-background-color:rgb(200,200,200); -fx-border-color:DARKBLUE">
<children>
<Label text="Media Player"/>
<MediaView fx:id="mediaView"/>
</children>
</VBox>
</center>
<bottom>
<Label text="Not in the snap scope (NITSS)"/>
</bottom>
<left>
<Label text="NITSS"/>
</left>
<right>
<Label text="NITSS"/>
</right>
</fx:root>