0

I am trying to get the screen using glReadPixels() then converting that into an image. I get an error saying image == null.

ByteBuffer _frame = ByteBuffer.allocate( 4 * Main.gui.glCanvas.getWidth() * Main.gui.glCanvas.getHeight() );
    System.out.println( "Num Pixels " + Main.width * Main.height );
    gl.glReadPixels(
            0,
            0,
            Main.gui.glCanvas.getWidth(),
            Main.gui.glCanvas.getHeight(),
            GL3.GL_RGBA,
            GL3.GL_UNSIGNED_BYTE,
            _frame
            );

    try {
        BufferedImage bi = ImageIO.read( new ByteArrayInputStream( _frame.array() ) );
        ImageIO.write( bi, "png", new File( "Slices/slicu.png" ) );
    } catch ( Exception e ) {
        System.out.println( e.getMessage() + " @ Filler.draw()" );
        e.printStackTrace();
    }

1 Answer 1

2

Rather use com.jogamp.common.nio.Buffers.newDirectByteBuffer() instead of ByteBuffer.allocate(). If you use an undirect NIO buffer here, JOGL will have to make a direct NIO buffer under the hood and you can't be sure that a NIO buffer is backed by an array anyway, it can be null. Then, create an array and copy the content of your buffer into it.

You could use com.jogamp.opengl.util.awt.Screenshot, com.jogamp.opengl.util.GLReadBufferUtil or com.jogamp.opengl.util.awt.AWTGLReadBufferUtil instead, there are some examples on Github in jogl-demos.

I advise you to ask your questions about JOGL rather on our official forum.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.