繁体   English   中英

LibGDX截图奇怪的行为

[英]LibGDX screenshot strange behaviour

我遇到了一个在LibGDX中截取我的桌面应用程序的相当奇怪的行为。 我重新制作了一个小程序来重现这个“bug”,它只渲染黑色背景和红色矩形。 那些图像是结果:

在此输入图像描述 在此输入图像描述

左边是来自窗口屏幕剪辑工具的屏幕截图,这就像运行程序一样。 右边是我发布的屏幕截图代码。 为了澄清,我希望程序的屏幕截图能够获得左图像的结果,而不会让透明度变得非常奇怪。

这是我的渲染代码,不介意坐标。 因为我可以看到矩形被完美呈现,所以对于我在渲染方法中的错误没有任何意义。 但无论如何我都会张贴它。

@Override
public void render() {

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Gdx.gl.glActiveTexture(GL20.GL_TEXTURE0);
    Gdx.gl.glEnable(GL20.GL_BLEND);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

    shape.begin(ShapeType.Filled);
    shape.setColor(Color.BLACK);
    shape.rect(0, 0, 300, 300);

    shape.setColor(1f, 0f, 0f, 0.5f);
    shape.rect(100, 100, 100, 100);
    shape.end();

    Gdx.gl.glDisable(GL20.GL_BLEND);

}

这是截取屏幕截图的代码:

public static void screenshot() {

    Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    PixmapIO.writePNG(new FileHandle(Gdx.files.getLocalStoragePath() + "screenshots/test.png"), pixmap);
    pixmap.dispose();

}

private static Pixmap getScreenshot(int x, int y, int w, int h) {
    final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);

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

我去研究,我只发现了这个话题 ,这是完全相同的问题。 但它的信息略少,没有答案。 我希望你们中的某个人能够回答这个谜团。

我的问题在2017年2月23日Tenfour04回答,但由于他没有兴趣发布他的解决方案作为答案,我正在这样做来解决这个问题。 非常感谢他。 我所做的是将getPixels()返回的ByteBuffer中的每个第四个元素(alpha值)设置为(byte) 255 (不透明)这是我的结果:

private static Pixmap getScreenshot(int x, int y, int width, int height) {

    final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, width, height);

    ByteBuffer pixels = pixmap.getPixels();
    for(int i = 4; i < pixels.limit(); i += 4) {
        pixels.put(i - 1, (byte) 255);
    }

    int numBytes = width * height * 4;
    byte[] lines = new byte[numBytes];
    int numBytesPerLine = width * 4;
    for(int i = 0; i < height; i++) {
        pixels.position((height - i - 1) * numBytesPerLine);
        pixels.get(lines, i * numBytesPerLine, numBytesPerLine);
    }
    pixels.clear();
    pixels.put(lines);
    pixels.clear();

    return pixmap;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM