Screenshots (especially after resize)

Having problems with building or using the JCEF Java binding? Ask your questions here.

Screenshots (especially after resize)

Postby thebiznatch » Thu Feb 13, 2020 11:45 am


First, is there a best practice suggested way to capture screenshots of what's rendered in the browser? Currently, we use an offscreen browser and access the GLCanvas to copy the buffer contents into a BufferedImage. It works great, except in the case of the GLCanvas waiting for rendering updates. Here is the code we use:

Code: Select all
private BufferedImage screenshotGLCanvas() {
        GLCanvas canvas = (GLCanvas)this.cefBrowser.getUIComponent();
        GL2 gl2 = canvas.getGL().getGL2();
        // size to actually grab
        Dimension size = this.view.getWebView().getSize();
        int width = size.width;
        int height = size.height;

        ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

        // be sure you are reading from the right fbo (here is supposed to be the default one)
        // bind the right buffer to read from
        gl2.glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

        int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
        for (int i = pixels.length - 1; i >= 0; i--) {
            int x = i % width, y = i / width * width;
            // rgba -> argb
            pixels[y + width - 1 - x] =
                (buffer.get() & 0xff) << 16 |
                (buffer.get() & 0xff) << 8 |
                buffer.get() & 0xff |
                (buffer.get() & 0xff) << 24;

        return image;

Now, if we resize the browser, we get pretty awful screenshots since there is lag on the rendering of the newly positioned elements. I guess my question is -- is there any way to 100% force the repaint and wait for that to complete? We've tried all sorts of techniques and none of them are reliable. We've waited for "animation frames" via javascript, we've taken multiple screenshots and detected if there are significant changes, etc. The only sure fired approach is to wait a few seconds -- but that will degrade performance quite a bit. Is our GLCanvas approach above acceptable or is there a better way?
Posts: 1
Joined: Thu Feb 13, 2020 11:36 am

Re: Screenshots (especially after resize)

Postby magreenblatt » Thu Feb 13, 2020 4:13 pm

thebiznatch wrote:I guess my question is -- is there any way to 100% force the repaint and wait for that to complete?

Not currently. You can watch issue #2773 which is related.
Site Admin
Posts: 10111
Joined: Fri May 29, 2009 6:57 pm

Return to JCEF Forum

Who is online

Users browsing this forum: No registered users and 7 guests