java - LibGDX - 将屏幕截图保存在写保护文件夹中

标签 java libgdx

我的保存屏幕截图的类工作正常:

public class ScreenshotSaver {

    private static int counter = 1;

    public static void saveScreenshot() {
        FileHandle fh;
        do {
            fh = new FileHandle("screenshot" + counter++ + ".png");
        } while (fh.exists()); 
        Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);        
        PixmapIO.writePNG(fh, pixmap);
        pixmap.dispose();
    }

    private static Pixmap getScreenshot(int x, int y, int w, int h, boolean flipY) {
        Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1);

        final Pixmap pixmap = new Pixmap(w, h, Format.RGBA8888);
        ByteBuffer pixels = pixmap.getPixels();
        Gdx.gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels);

        final int numBytes = w * h * 4;
        byte[] lines = new byte[numBytes];
        if (flipY) {
            pixels.clear();
            pixels.get(lines);

        } else {
            final 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;
    }

}

问题是,当我在写保护区域执行程序时。程序只是关闭。我宁愿它只是不保存屏幕截图。如何做到这一点?

最佳答案

为什么不直接使用 try/catch?

try{
  PixmapIO.writePNG(fh, pixmap);
} catch (Exception e) {
  // save it somewhere else
}
pixmap.dispose();

关于java - LibGDX - 将屏幕截图保存在写保护文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20933718/

相关文章:

java - Android EditText - 显示顶部图标时文本移动

java - 改变 nimbus JPopupmenu 行为

java编程和java单例多线程问题(单例与多线程)

java - jnlp 中的 jar 资源未由同一证书签名 ETRADE Pro 应用程序 Kubuntu 18.04

java - Gradle 项目 --Failed to configure a DataSource'url' attribute is not specified and no embedded datasource could be configured

java - 盒子2D。试图了解需要多大的力量

android - 如何移动角色 - 尝试应用 touchDown 和 touchUp

java - libgdx 剪切图像

java - 对在 libgdx 中实现渲染方法的不同方法的疑问

java - LibGDX:根据单词的长度在屏幕上创建文本按钮的数量?