gpt4 book ai didi

java - 如何从vlcj Canvas 中删除黑条并将 Canvas 扩展到两行GridBagLayout?

转载 作者:行者123 更新时间:2023-12-01 11:01:00 37 4
gpt4 key购买 nike

我的视频播放器周围有黑条,如下图所示。我该如何摆脱它们?我尝试修复 AVPlayer 类中的大小,但是虽然这阻止了视频播放器的随机调整大小,但它并没有消除黑条。

我的第二个问题:如何使视频播放器位于现在的位置,但向上移动一点,以便播放器的顶部与组合框位于同一行?现在它与显示“English”的 JLabel 位于同一行,但我希望它在两行上扩展。

enter image description here

您将在下面找到我的 SSCCE。它仍然有点长,但我摆脱了所有依赖项。如果你编译它,你应该得到上面的图像。我把AVPlayer类作为内部类来简化代码,它实际上有自己的类。您需要添加视频源才能显示视频播放器。

public class SSCCE extends JPanel {

private final JLabel kanjiLabel;
private final JLabel englishLabel;
private final JPanel wordPanel, cbPanel;
private final JComboBox chapters;
private final DefaultComboBoxModel model;
private final JLabel chapLabel = new JLabel("Chapter");
private AVPlayer player;

private final GridBagConstraints constraints = new GridBagConstraints();

public SSCCE() {
setLayout(new GridBagLayout());
setConstraints();

//wordPanel
wordPanel = new JPanel();
wordPanel.setLayout(new BoxLayout(wordPanel, BoxLayout.Y_AXIS));
wordPanel.setPreferredSize(new Dimension(500, 150));

kanjiLabel = new JLabel();
englishLabel = new JLabel();

kanjiLabel.setFont(new java.awt.Font("Dialog", 0, 50));
englishLabel.setFont(new java.awt.Font("Dialog", 0, 40));

wordPanel.add(englishLabel);
wordPanel.add(Box.createRigidArea(new Dimension(10, 10)));
wordPanel.add(kanjiLabel);

//chapter combox
chapters = new JComboBox();
model = new DefaultComboBoxModel();
chapters.setModel(model);
cbPanel = new JPanel();
addCB();

constraints.gridy += 1;
add(wordPanel, constraints);
//add the audio video panel
player = new AVPlayer();
constraints.gridx += 1;
add(player, constraints);
}

private void setConstraints() {
constraints.gridx = 0;
constraints.gridy = 0;
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.CENTER;
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1.0;
constraints.weighty = 0.0;
}

private void addCB() {
setConstraints();
cbPanel.removeAll();
cbPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 10, 5));
cbPanel.add(chapLabel);
cbPanel.add(chapters);
add(cbPanel, constraints);

}

public void displayNewWord() {

kanjiLabel.setText("Kanji");
englishLabel.setText("English");
player.playVideo("video.mp4"); //replace this with a video on your computer
revalidate();
repaint();
}

public class AVPlayer extends JPanel {

private static final String VLCPATH = "C:\\VideoLAN\\VLC";

private final MediaPlayerFactory mpf;
private final EmbeddedMediaPlayer emp;

public AVPlayer() {

//this panels sizes
setPreferredSize(new Dimension(150, 150));
setMinimumSize(new Dimension(150, 150));
setMaximumSize(new Dimension(150, 150));

//canvas
setLayout(new BorderLayout());
Canvas canvas = new Canvas();
canvas.setPreferredSize(new Dimension(150, 150));

add(canvas, BorderLayout.CENTER);

//load the native vlc library
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), VLCPATH);
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
mpf = new MediaPlayerFactory();

emp = mpf.newEmbeddedMediaPlayer();
emp.setVideoSurface(mpf.newVideoSurface(canvas));

}

public void playVideo(String path) {
emp.prepareMedia(path);
emp.play();
emp.setRepeat(true);
}
}
}

以及主要方法

JFrame frame = new JFrame();
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

SSCCE sscce = new SSCCE();
frame.add(sscce, BorderLayout.NORTH);

frame.setVisible(true);
sscce.displayNewWord();
frame.revalidate();

最佳答案

黑条是嵌入式 VLC 媒体播放器视频输出的一部分。

您需要设置适当的纵横比和/或裁剪几何形状来消除它们。

例如:

mediaPlayer.setCropGeometry("4:3");         // W:H
mediaPlayer.setCropGeometry("719x575+0+0"); // WxH+L+T
mediaPlayer.setCropGeometry("6+10+6+10"); // L+T+R+B
mediaPlayer.setAspectRatio("185:100");

只有您知道正确的值应该是什么。

或者,如果黑条不在 native 视频输出中,则这是由于您的 Canvas 尺寸与视频的宽高比不同造成的。

您可以通过将 Canvas 背景设置为其他颜色来找出您遇到的两个问题中的哪一个。

关于java - 如何从vlcj Canvas 中删除黑条并将 Canvas 扩展到两行GridBagLayout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33322546/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com