taking screenshot in libgdx

假如想象 提交于 2019-12-12 09:44:26

问题


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 ??


回答1:


It is now fairly easy. Libgdx provides an example, which can be found here.

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;
    }
}



回答2:


Simple solution:

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



回答3:


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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/12625052/taking-screenshot-in-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!