简体   繁体   中英

Resize video with JMF

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

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM