Taking screenshots in libgdx

I have an application in which I want to take a 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 someone help me how to programmatically display a screenshot of the game screen and save it as an image?

+4
source share
5 answers

Now it's pretty easy. Libgdx gives an example that can be found here .

I had to add one expression to make it work. Image cannot be saved directly to /screenshot1.png . Just add Gdx.files.getLocalStoragePath() .

Source:

 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; } } 
+5
source

A simple solution:

 Image screenShot = new Image(ScreenUtils.getFrameBufferTexture()); 
+2
source

I just want to add something here.

When shooting screeenShot, we also need to look at the black panels and resize the windows. If you do not have viewPort (for mobile devices), simply replace the size of the gutter with 0.

Here's how you do fullScreen-screenShot correctly:

 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 can save pixmap using PixmapIO. https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/PixmapIO.html

NOTE: Please note that y is not the bottom, but the top one. Due to the difference in the coordinate system. That's why the image goes upside down.

You can also flip the pixmap (Only if you are not going to turn it into Texture and then Sprite) using the code in the link below.

fooobar.com/questions/533576 / ...

+2
source

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

Then you can get the Pixmap. It has everything you need to save it as any image file.

+1
source

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 an error that there is no PNG class.

Thanks Stick2 who helped me.

0
source

Source: https://habr.com/ru/post/1436645/


All Articles