Rendering HD video in Java Swing? Too slow?
I would like to enable HD video playback for my Java Swing application but I am concerned about scalability and apparent lack of direct screen write access. I have a native C++ library which will do encoding and decoding (h.264) I will access this library through JNI and it will return an int[] that I will render to the screen.
Encode/decode performance is pretty good thus far through JNI. I am concerned, however, about the rendering overhead. I would like to have multiple renderings of video open at the same time so I w开发者_开发百科ould like to ensure that I'm doing things in the most efficient way possible.
Is this the most efficient way of rendering video with Java/Swing? Is there any way I can write directly to the screen and not have to go through the Java paint system?
This is how my code looks: ` public class VideoDisplay extends JLabel {
BufferedImage m_bufferedImage = null;
public VideoDisplay(Dimension frameSize) {
// If there is nothing to show, make sure the widget is black.
setBackground(Color.black);
m_bufferedImage = new BufferedImage(frameSize.width, frameSize.height,
BufferedImage.TYPE_INT_ARGB);
ImageIcon frameIcon = new ImageIcon(m_bufferedImage);
setIcon(frameIcon );
}
public void displayNewFrame(int[] newPixels, Dimension imageSize) {
m_bufferedImage.setRGB(0, 0, imageSize.width, imageSize.height,
newPixels, 0, imageSize.width);
repaint();
}
}`
Right now my overhead for displaying 720p video at 24FPS seems to be about 6% of CPU on a Core2 Quad 6600 CPU.
Is there any faster way of doing this?
The DJ Native Swing project has a win32 media player and VLC player which can be embedded in Swing: http://djproject.sourceforge.net/ns
You should bypass Java completely and either use a surface, or put the data in a direct buffer and a volatile image back this.
Edit: doesn't look like you might be able to do the latter easily. Anyway, transfer the image data through JNI via a direct byte buffer. You can get a buffer pointer in C.
精彩评论