我在Java swing应用程序中使用GLJpanel
组件,我想从GLJPanel中的FFmpegFrameGrabber
绘制帧图像,所以我的想法是使用如下所述的组件图形。
import org.bytedeco.javacv.Frame;
import com.jogamp.opengl.awt.GLJPanel;
public void showImage(GLJPanel panel, Frame frame) {
Graphics2D graphics = (Graphics2D) panel.getGraphics();
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bfimage = converter.convert(frame);
graphics.drawImage(bfimage, null, 0,0);
}
这是在启用了GL的组件中绘制图像的正确方法,还是有另一种方法,我怀疑自己错了,但我无法证明这一点
我的GLJPanel
如下创建
final GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);
GLJPanel panel = new GLJPanel(capabilities);
GLEventListener
,此接口有4种方法display
,displayChanged
,init
和dispose
,然后是负责绘制的通用参数GLAutoDrawable
[在绘制图像BufferedImage
的情况下,我相信第一步是将任何输入的图像转换为BufferedImage
,在这种情况下,我将此行为包含在实用程序类中,并将转换后的图像送入队列。
要绘制,您必须使用Texture
,可以使用TextureData
创建此图像,这将使用以下缓冲图像
TextureData textureData = AWTTextureIO.newTextureData(gl.getGLProfile(), baseImage, false);
Texture texture = TextureIO.newTexture(textureData);
所以最后我的显示应该像
public synchronized void display(GLAutoDrawable drawable) { BufferedImage baseImage = frames.poll(); if(baseImage == null)return; final GL2 gl = drawable.getGL().getGL2(); gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT); // Create a TextureData and Texture from it TextureData textureData = AWTTextureIO.newTextureData(gl.getGLProfile(), baseImage, false); Texture texture = TextureIO.newTexture(textureData); // Now draw one quad with the texture texture.enable(gl); texture.bind(gl); gl.glTexEnvi(GL2.GL_TEXTURE_ENV, GL2.GL_TEXTURE_ENV_MODE, GL2.GL_REPLACE); TextureCoords coords = texture.getImageTexCoords(); gl.glBegin(GL2.GL_QUADS); gl.glTexCoord2f(coords.left(), coords.bottom()); gl.glVertex3f(0, 0, 0); gl.glTexCoord2f(coords.right(), coords.bottom()); gl.glVertex3f(1, 0, 0); gl.glTexCoord2f(coords.right(), coords.top()); gl.glVertex3f(1, 1, 0); gl.glTexCoord2f(coords.left(), coords.top()); gl.glVertex3f(0, 1, 0); gl.glEnd(); texture.disable(gl); texture.destroy(gl); //baseImage = null; }