我在我的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);发布于 2020-01-04 16:19:58
我要做的就是创建一个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;
} https://stackoverflow.com/questions/59478688
复制相似问题