簡體   English   中英

在libgdx中截取屏幕截圖

[英]taking screenshot in libgdx

我有一個應用程序,我想在其中截取游戲屏幕截圖並將其保存為圖像並上傳到Facebook。 我正在使用Libgdx,我的重點是android。

誰能幫助我如何以編程方式截取游戲畫面並將其保存為圖像?

現在相當容易。 Libgdx提供了一個示例,可以在此處找到。

我必須添加一個語句才能使其正常工作。 圖像無法直接保存到/screenshot1.png 只需添加Gdx.files.getLocalStoragePath()

源代碼:

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

簡單方案:

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

我只想在這里添加一些東西。

在拍攝screeenShot時,我們也需要考慮黑框和調整大小的窗口。 如果您沒有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不是底部排水溝,而是頂部排水溝。 由於坐標系的不同。 這也是為什么圖像顛倒了。

您也可以使用下面鏈接中的代碼翻轉像素圖(僅當您不打算將其轉換為紋理然后使用Sprite)。

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

你想創建一個FrameBuffer,frameBuffer.begin(),渲染一切,frameBuffer.end()。

然后你可以獲得Pixmap。 這包含將其保存為任何圖像文件所需的一切。

謝謝我使用http://code.google.com/p/libgdx-users/wiki/Screenshots鏈接解決了這個問題? 但是使用了PixmapIO.wirtePNG(pixmap,fileHandle)而不是PNG.toPNG,因為它給出了沒有PNG類的錯誤。

感謝Stick2幫助了我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM