4

ゲーム画面のスクリーンショットを撮り、画像として保存してFacebookにアップロードしたいアプリケーションがあります。私は Libgdx を使用しており、私の焦点はアンドロイドです。

プログラムでゲーム画面のスクリーンショットを撮り、画像として保存する方法を教えてもらえますか??

4

5 に答える 5

5

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;
    }
}
于 2014-06-28T11:13:32.927 に答える
2

簡単な解決策:

Image screenShot = new Image(ScreenUtils.getFrameBufferTexture());
于 2013-12-27T23:39:43.663 に答える
2

ここに何かを追加したいだけです。

スクリーンショットを撮影するときは、ブラックバーとサイズ変更されたウィンドウも考慮する必要があります。viewPort (モバイル デバイス用) がない場合は、ガターの寸法を 0 に置き換えます。

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

次に、PixmapIO を使用してピックスマップを保存できます。https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/PixmapIO.html

注: y は下部のガターではなく、上部のガターであることに注意してください。座標系が違うからです。画像が逆さまになるのもそのためです。

また、以下のリンクのコードを使用して、ピックスマップを反転することもできます (テクスチャに変換してからスプライトに変換しない場合のみ)。

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

于 2016-03-27T13:58:21.527 に答える
1

FrameBuffer を作成し、frameBuffer.begin()、すべてをレンダリングし、frameBuffer.end() を作成します。

その後、Pixmap を取得できます。これには、画像ファイルとして保存するために必要なものがすべて含まれています。

于 2012-09-28T05:15:53.510 に答える
0

リンクhttp://code.google.com/p/libgdx-users/wiki/Screenshotsを使用して解決しましたか?ただし、PNGクラスがないというエラーが発生するため、PNG.toPNGの代わりにPixmapIO.wirtePNG(pixmap、fileHandle)を使用しました。

私を助けてくれたStick2に感謝します。

于 2012-10-02T07:32:42.473 に答える