微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 JFrame 中实现视频文件

如何解决在 JFrame 中实现视频文件

这一次我真的很难破解。我能够实现一个幻灯片程序,该程序能够在给定的时间内依次显示许多随机图片。该程序也会对按钮按下做出反应。

现在我的任务是让它能够显示视频文件,这让我很头疼。需要解决的任务如下:

  1. 文件的分辨率应取决于屏幕的实际大小。如果图像或视频的分辨率高于屏幕,则应按比例缩小(请参阅示例代码)。
  2. 图像和视频应该在 JFrame 中实现为 JComponent,JFrame 本身应该由几个元素组成,例如图像/视频区域、文本区域和按钮等 - (我为图片解决了这个问题) .
  3. 经过一定时间后,幻灯片应该显示下一张图片/视频。对于图片,时间是固定的,但在播放视频时,时间应该取决于视频本身的持续时间(我们不希望幻灯片跳到视频中间的下一张幻灯片

为了更简单的解释,让我先展示一下我是如何解决图片幻灯片中的实现的:

'''

public class displayImage extends JComponent {
   private static final long serialVersionUID = 2613775805584208452L;


   private static Image image;

   public static Image displayImage(File f,Dimension screenSize) throws IOException {
    
       //This method loads a file from the computer and resizes it in comparison to the size of the computer screen. The image is then returned for further processing.
    
       BufferedImage img = ImageIO.read(f);
       Image dimg;
       double width = screenSize.getWidth()*0.75;
       double z1 = (img.getWidth()/width);
       double z2 = (img.getHeight()/screenSize.getHeight());
    
       if (img.getHeight()/z1 <= width && img.getHeight()/z1 < screenSize.getHeight()) {
           dimg = img.getScaledInstance((int)(img.getWidth()/z1),(int) (img.getHeight()/z1),Image.SCALE_SMOOTH);
       } else {
           dimg = img.getScaledInstance((int)(img.getWidth()/z2),(int)(img.getHeight()/z2),Image.SCALE_SMOOTH);
       }
       return dimg;
   }


   public void setimage(Image image1) {
      //When an image is resized,it is given to this method.
      //It replaces the global variable "image" with the new loaded image so the JFrame in the slideshow is actually reset and will display the new image.

      image = image1;
      repaint();
      invalidate();
    }}

'''

如您所见,我完全可以加载新图像并使用它重写图像以及类的 JComponent。 来到一个视频文件,它会变得很乱。我能够使用 Maven 从这里某处获取的另一个代码加载视频文件,但我没有成功地将它实现为 JComponent(我已经浏览了 stackoverflow 和 google 好几天了,但找不到我的问题的解决方案)。到目前为止,我唯一能做的就是在幻灯片之外启动一个额外的播放器,好像它们没有任何共同点:

'''

public void playVideo(File f,Dimension screenSize) throws IOException,JCodecException {
    
    Picture img = FrameGrab.getFrameAtSec(f,1);
    double width = screenSize.getWidth()*0.75;
    double z1 = (img.getWidth()/width);
    double z2 = (img.getHeight()/screenSize.getHeight());
    
    NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(),"C:\\Program Files\\VideoLAN\\VLC");
    Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(),LibVlc.class);
    JFrame frame = new JFrame("vlcj Tutorial");
    MediaPlayerFactory mediaPlayerFactory = new MediaPlayerFactory();
    Canvas c = new Canvas();
    c.setBackground(Color.black);
    JPanel p = new JPanel();
    p.setLayout(new BorderLayout());
    p.add(c,BorderLayout.CENTER);
    frame.add(p,BorderLayout.CENTER);
    EmbeddedMediaPlayer mediaPlayer = mediaPlayerFactory.newEmbeddedMediaPlayer();
    mediaPlayer.setVideoSurface(mediaPlayerFactory.newVideoSurface(c));
    
    if (img.getHeight()/z1 <= width && img.getHeight()/z1 < screenSize.getHeight()) {
        frame.setSize((int)(img.getWidth()/z1),(int)(img.getHeight()/z1));
    } else {
        frame.setSize((int)(img.getWidth()/z2),(int)(img.getHeight()/z2));
    }
    frame.setSize(screenSize);
    frame.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    mediaPlayer.playMedia(f.getPath());
}

'''

混乱已经开始,我无法实际获得视频文件本身的测量值(即宽度和高度)。我一直在为实现不同的框架如 JavaCV、Xuggle、MarvinFramework 等等而绞尽脑汁,但这一点都不好。我唯一能做的就是从视频中获取一个帧作为图片类型,如本例所示。但这对我来说无法返回 JComponent 或 BufferedImage (如第一种方法中看到的图片)。更糟糕的是:我发现没有办法在加载视频文件时实际重置 JFrame,导致在新播放器中启动视频时它立即冻结。之后就只剩下终止开关了。 所以我在这里迷路了。非常感谢任何帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。