Ramiz Raja
Ramiz Raja

Reputation: 310

taking screenshot in libgdx

I have an application in which i want to take the screenshot of the game screen and save it as an image and upload to Facebook. I am using Libgdx and my focus is android.

Can anyone help me that how to take screenshot of the game screen programmatically and save it as an image ??

Upvotes: 4

Views: 7010

Answers (5)

Niklas
Niklas

Reputation: 25411

It is now fairly easy. Libgdx provides an example, which can be found on the page Taking a Screenshot.

I had to add one statement to get it working. The image could not be saved directly to /screenshot1.png. Simply prepend Gdx.files.getLocalStoragePath().

Source Code:

public class ScreenshotFactory {

    private static int counter = 1;
    public static void saveScreenshot(){
        try{
            FileHandle fh;
            do{
                fh = new FileHandle(Gdx.files.getLocalStoragePath() + "screenshot" + counter++ + ".png");
            }while (fh.exists());
            Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
            PixmapIO.writePNG(fh, pixmap);
            pixmap.dispose();
        }catch (Exception e){           
        }
    }

    private static Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown){
        final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);

        if (yDown) {
            // Flip the pixmap upside down
            ByteBuffer pixels = pixmap.getPixels();
            int numBytes = w * h * 4;
            byte[] lines = new byte[numBytes];
            int numBytesPerLine = w * 4;
            for (int i = 0; i < h; i++) {
                pixels.position((h - i - 1) * numBytesPerLine);
                pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
            }
            pixels.clear();
            pixels.put(lines);
        }

        return pixmap;
    }
}

Upvotes: 5

ossobuko
ossobuko

Reputation: 871

I just want to add somethings here.

When taking a screeenShot we need to consider blackbars and resized windows too. If you don't have a viewPort (for mobile devices), just replace gutter dimensions with 0.

Here is how you properly take a fullScreen-screenShot:

final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(
                    MyGdxGame.viewport.getLeftGutterWidth(),
                    MyGdxGame.viewport.getTopGutterHeight(),
                    Gdx.graphics.getWidth() - MyGdxGame.viewport.getLeftGutterWidth() - MyGdxGame.viewport.getRightGutterWidth(),
                    Gdx.graphics.getHeight() - MyGdxGame.viewport.getTopGutterHeight() - MyGdxGame.viewport.getBottomGutterHeight());

Then you could save a pixmap using PixmapIO. https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/PixmapIO.html

NOTE: Note that y is not bottom gutter but the top one. Because of the difference in coordinate system. That also why the image comes upside down.

Also you could flip the pixmap (Only if you are not going to turn it to a Texture and then Sprite) using the code in the link below.

https://stackoverflow.com/a/19746741/2205307

Upvotes: 2

Pascalius
Pascalius

Reputation: 14659

Simple solution:

Image screenShot = new Image(ScreenUtils.getFrameBufferTexture());

Upvotes: 2

Ramiz Raja
Ramiz Raja

Reputation: 310

Thanks i solved it using the link http://code.google.com/p/libgdx-users/wiki/Screenshots? but used PixmapIO.wirtePNG ( pixmap , fileHandle ) instead of PNG.toPNG because it gives error that there is no PNG class.

Thanks to Stick2 who helped me.

Upvotes: 0

Richard Taylor
Richard Taylor

Reputation: 2742

You want to create a FrameBuffer, frameBuffer.begin(), render everything, frameBuffer.end().

You can then get the Pixmap. This has everything you need to save it as any image file.

Upvotes: 1

Related Questions