我想同时显示面部Rectangle的数组列表和视频作为背景。这是我的代码,我已经详细阐述,但我还没有达到预期的目标。此代码仅允许显示矩形列表,并固定在视频的第一个图像上(它不会将视频显示为背景)。我不知道错误的来源。有人可以帮我吗?
@SuppressWarnings("serial")
public class DirectMediaPlayerComponentTest {
private final int width = 720;
private final int height = 480;
private final JPanel panel;
private final BufferedImage image;
static ArrayList<FaceRectangle> recList;
ArrayList<FaceRectangle> recToDisplay = new ArrayList<FaceRectangle>();
public static void main(String[] args) {
final String mrl = "dance_redacted1.mp4";
NativeLibrary.addSearchPath(RuntimeUtil.getLibVlcLibraryName(), "lib");
Native.loadLibrary(RuntimeUtil.getLibVlcLibraryName(), LibVlc.class);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new DirectMediaPlayerComponentTest().start(mrl);
}
});
}
private DirectMediaPlayerComponentTest() {
JFrame frame = new JFrame("vlcj Direct Media Player Component Test");
panel = new JPanel() {
@Override
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setColor(Color.black);
g2.fillRect(0, 0, getWidth(), getHeight());
g2.drawImage(image, null, 0, 0);
for ( FaceRectangle rec : recToDisplay ){
System.out.println(rec);
g2.setColor(rec.getColor(rec.getId()));
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2.draw(new Rectangle2D.Double((rec.getTop())*1000, (rec.getLeft())*1000, (rec.getWidth())*1000, (rec.getHeight())*1000));
}
}
};
panel.setBackground(Color.black);
panel.setOpaque(true);
panel.setPreferredSize(new Dimension(width, height));
panel.setMinimumSize(new Dimension(width, height));
panel.setMaximumSize(new Dimension(width, height));
image = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(width, height);
BufferFormatCallback bufferFormatCallback = new BufferFormatCallback() {
public BufferFormat getBufferFormat(int sourceWidth, int sourceHeight) {
return new RV32BufferFormat(width, height);
}
};
mediaPlayerComponent = new DirectMediaPlayerComponent(bufferFormatCallback) {
@Override
protected RenderCallback onGetRenderCallback() {
return new TestRenderCallbackAdapter();
}
};
frame.setContentPane(panel);
frame.setLocation(100, 100);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
// Start playing a movie.
private void start(String mrl) {
// One line of vlcj code to play the media...
mediaPlayerComponent.getMediaPlayer().playMedia(mrl);
}
private class TestRenderCallbackAdapter extends RenderCallbackAdapter {
private TestRenderCallbackAdapter() {
super(new int[width * height]);
}
@Override
protected void onDisplay(DirectMediaPlayer mediaPlayer, int[] rgbBuffer) {
// Simply copy buffer to the image and repaint
image.setRGB(0, 0, width, height, rgbBuffer, 0, width);
panel.repaint();
//for (ArrayList<FaceRectangle> rec : rectFace){
for ( FaceRectangle rec : recList ){
recToDisplay.clear();
recToDisplay.add(rec);
panel.repaint();
try {
Thread.sleep(512);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}