使用ScreenUtils以截图保存为图像libgdx [英] using ScreenUtils to save screenshot as image in libgdx

查看:275
本文介绍了使用ScreenUtils以截图保存为图像libgdx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ScreenUtils.getFrameBufferPixels(...)拿游戏画面的截图。我想保存此方法在文件中的图像返回的字节数组。我使用libGDX和我在Android的焦点。

I am using ScreenUtils.getFrameBufferPixels(...) to take a screenshot of the game screen. I want to save the byte array returned by this method as an image in file. I am using libGDX and my focus in android.

推荐答案

现在是相当容易的。 Libgdx提供了一个例子,它可以发现这里

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

我不得不添加一个语句得到它的工作。图像无法直接保存到 /screenshot1.png 。只需prePEND Gdx.files.getLocalStoragePath()

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().

来源$ C ​​$ C:

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

这篇关于使用ScreenUtils以截图保存为图像libgdx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆