使用 JMF 调整视频大小

发布于 2024-07-29 23:43:07 字数 2001 浏览 5 评论 0原文

我正在尝试使用 JMF 在我的 java 应用程序中播放视频。 该视频播放正常,但我正在尝试将视频放大。 下面的代码被放置在另一个带有 gridbag 布局的 jpanel 中。

我目前添加它时没有 FILL 约束,因此它应该以其正常大小显示。

当我添加填充约束时,它会拉伸视频,使宽高比倾斜。

我想我问是否有人知道如何手动调整视频大小或如何锁定宽高比

public class VideoPanel extends JPanel{
private Player mediaPlayer;
private File file;

public VideoPanel(String videoFile, String path){
    setOpaque(false);
    file = new File(path+"/video/"+videoFile); 

    URL mediaURL = null;
    try {
        mediaURL = file.toURI().toURL();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    setLayout( new BorderLayout() );      
    Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
    try{
        mediaPlayer = Manager.createRealizedPlayer( mediaURL );
        Component video = mediaPlayer.getVisualComponent();
        Component controls = mediaPlayer.getControlPanelComponent();

        double scale = getScale(this.getWidth(),this.getHeight(),video.getWidth(),video.getHeight());

        video.setPreferredSize(new Dimension((int)scale*video.getWidth(),(int)scale*video.getHeight()));



        if(video != null) 
            add(video,BorderLayout.CENTER );
        if(controls != null) 
            add(controls,BorderLayout.SOUTH );
    }
    catch ( NoPlayerException noPlayerException ){
        System.err.println( "No media player found" );
    }
    catch ( CannotRealizeException cannotRealizeException ){
        System.err.println( "Could not realize media player" );
    }
    catch ( IOException iOException ){
        System.err.println( "Error reading from the source" );
    }
}

private double getScale(int panelWidth, int panelHeight, int imageWidth, int imageHeight) {
    double scale = 1;
    double xScale;
    double yScale;

    xScale = (double) panelWidth / imageWidth;
    yScale = (double) panelHeight / imageHeight;
    scale = Math.min(xScale, yScale);
    return scale;
}

public void stopPlay(){     
    mediaPlayer.stop();
}

}

Im trying to play a video in my java application using JMF. The video is playing fine but im trying to make the video larger. the code below is being placed inside another jpanel with a gridbag layout.

I currently have it being added with no FILL constraint so it should be displaying at its normal size.

When i do add a fill constraint it stretches the video skewing the aspect ratio.

I guess im asking if anyone knows how to resize a video manually or how to lock the aspect ratio

public class VideoPanel extends JPanel{
private Player mediaPlayer;
private File file;

public VideoPanel(String videoFile, String path){
    setOpaque(false);
    file = new File(path+"/video/"+videoFile); 

    URL mediaURL = null;
    try {
        mediaURL = file.toURI().toURL();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    setLayout( new BorderLayout() );      
    Manager.setHint( Manager.LIGHTWEIGHT_RENDERER, true );
    try{
        mediaPlayer = Manager.createRealizedPlayer( mediaURL );
        Component video = mediaPlayer.getVisualComponent();
        Component controls = mediaPlayer.getControlPanelComponent();

        double scale = getScale(this.getWidth(),this.getHeight(),video.getWidth(),video.getHeight());

        video.setPreferredSize(new Dimension((int)scale*video.getWidth(),(int)scale*video.getHeight()));



        if(video != null) 
            add(video,BorderLayout.CENTER );
        if(controls != null) 
            add(controls,BorderLayout.SOUTH );
    }
    catch ( NoPlayerException noPlayerException ){
        System.err.println( "No media player found" );
    }
    catch ( CannotRealizeException cannotRealizeException ){
        System.err.println( "Could not realize media player" );
    }
    catch ( IOException iOException ){
        System.err.println( "Error reading from the source" );
    }
}

private double getScale(int panelWidth, int panelHeight, int imageWidth, int imageHeight) {
    double scale = 1;
    double xScale;
    double yScale;

    xScale = (double) panelWidth / imageWidth;
    yScale = (double) panelHeight / imageHeight;
    scale = Math.min(xScale, yScale);
    return scale;
}

public void stopPlay(){     
    mediaPlayer.stop();
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2024-08-05 23:43:08

您可能只想将视频组件放入另一个容器面板中并从容器中控制大小。 在我的应用程序中,我在 JFrame 中设置了视频组件,并且要调整视频大小,我只需调整容器大小即可。

You may just want to throw the video component in another container panel and control the size from the container. In my app, I have the video component set in a JFrame and to resize the video I simply resize my container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文