矩形指示特定的媒体播放器 javafx
Rectangle indicate to a specific media player javafx
在此 JavaFX 项目中
public class FXMLDocumentController implements Initializable {
@FXML
private MediaView media_view;
private Media me;
private MediaPlayer mp;
@Override
public void initialize(URL url, ResourceBundle rb)
{
String s="/home/mustafa987/Videos/sampels/video10.mp4";
me=new Media(new File(s).toURI().toString());
mp=new MediaPlayer(me);
media_view.setMediaPlayer(mp);
mp.play();
mp.setRate(1);
Robot robot=new Robot();
Rectangle rectangle=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
//in the line above the Rectangle indicate to the desktop screen so the robot can capture the screenshot
BufferedImage buffer=robot.createScreenCapture(rectangle);
//take screenshot
Image image=SwingFXUtils.toFXImage(buffer, null);
}
}
如何让矩形指示程序 MediaView 而不是桌面屏幕(或者)是否有办法让机器人直接指示程序 MediaView
谢谢
不是很了解你的需求,但是在初始化方法里做个图片对我来说意义不大
一种快速解决方案可能是以下一种:
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"));
}
}
它拍摄了包含 MediaView
的 VBox
的快照,因为我没有要显示的媒体,但它也应该与 MediaView
一起使用,因为此组件继承自 [=15] =].
由于您没有提供,这是我使用的 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>
在此 JavaFX 项目中
public class FXMLDocumentController implements Initializable {
@FXML
private MediaView media_view;
private Media me;
private MediaPlayer mp;
@Override
public void initialize(URL url, ResourceBundle rb)
{
String s="/home/mustafa987/Videos/sampels/video10.mp4";
me=new Media(new File(s).toURI().toString());
mp=new MediaPlayer(me);
media_view.setMediaPlayer(mp);
mp.play();
mp.setRate(1);
Robot robot=new Robot();
Rectangle rectangle=new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
//in the line above the Rectangle indicate to the desktop screen so the robot can capture the screenshot
BufferedImage buffer=robot.createScreenCapture(rectangle);
//take screenshot
Image image=SwingFXUtils.toFXImage(buffer, null);
}
}
如何让矩形指示程序 MediaView 而不是桌面屏幕(或者)是否有办法让机器人直接指示程序 MediaView 谢谢
不是很了解你的需求,但是在初始化方法里做个图片对我来说意义不大
一种快速解决方案可能是以下一种:
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"));
}
}
它拍摄了包含 MediaView
的 VBox
的快照,因为我没有要显示的媒体,但它也应该与 MediaView
一起使用,因为此组件继承自 [=15] =].
由于您没有提供,这是我使用的 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>