首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在GLJpanel中绘制图像的正确方法

在GLJpanel中绘制图像的正确方法
EN

Stack Overflow用户
提问于 2019-12-25 21:06:29
回答 1查看 212关注 0票数 1

我在我的Java swing应用程序中使用GLJpanel组件,我想在我的GLJPanel中绘制来自FFmpegFrameGrabber的帧图像,为此,我的想法是使用下面提到的组件图形。

代码语言:javascript
复制
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创建如下

代码语言:javascript
复制
final GLProfile profile = GLProfile.get(GLProfile.GL2);
        GLCapabilities capabilities = new GLCapabilities(profile);

        GLJPanel panel = new GLJPanel(capabilities);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-04 16:19:58

我要做的就是创建一个GLEventListener,这个接口有4个方法displaydisplayChangedinitdispose,然后是负责绘图的公共参数GLAutoDrawable

在绘制图像BufferedImage的情况下,我认为第一步是将任何图像输入转换为BufferedImage,在我的例子中,我将此行为封装在一个实用程序类中,并使用转换后的图像输入队列。

要绘制,您必须使用Texture,这可以使用TextureData创建,它将使用缓冲的图像,如下所示

代码语言:javascript
复制
TextureData textureData = AWTTextureIO.newTextureData(gl.getGLProfile(), baseImage, false);
Texture texture = TextureIO.newTexture(textureData);

最后我的显示应该是这样的

代码语言:javascript
复制
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;
} 
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59478688

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档