代码之家  ›  专栏  ›  技术社区  ›  mustafa-naeem

矩形表示特定的媒体播放器javafx

  •  0
  • mustafa-naeem  · 技术社区  · 6 年前

    在这个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 谢谢你

    1 回复  |  直到 6 年前
        1
  •  0
  •   Pagbo    6 年前

    我真的不知道您的需要,但在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>