gpt4 book ai didi

java - 在 libgdx 中截图

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:40 25 4
gpt4 key购买 nike

我有一个应用程序,我想在其中截取游戏屏幕的屏幕截图并将其另存为图像并上传到 Facebook。我正在使用 Libgdx,我的重点是 android。

谁能帮助我如何以编程方式截取游戏屏幕的屏幕截图并将其保存为图像??

最佳答案

现在很容易了。 Libgdx 提供了一个示例,可以在页面上找到 Taking a Screenshot .

我必须添加一个语句才能使其正常工作。图片无法直接保存到/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;
}
}

关于java - 在 libgdx 中截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12625052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com