2016-12-29 32 views
5

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

enter image description hereenter image description here

左边的一个是从窗口的屏幕剪辑工具的截图,这是什么样子运行的程序。右边是我发布的截图代码。为了澄清,我希望程序的屏幕截图能够获得左侧图像的结果,而透明度不会让人感到奇怪。

这是我的渲染代码,不介意坐标。由于我可以看到完美呈现的矩形,因此对于渲染方法中的错误我没有任何意义。但我仍然发布它。

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

我去研究,我只找到这个topic,这是完全一样的问题。尽管如此,它的信息稍微少一些,也没有答案。我希望你们有人能回答这个谜团。

+0

这不是一个错误。代码截图包括当时在后台缓冲区中的任何alpha,由于您有不透明的窗口,因此在运行时屏幕上不可见。 – Tenfour04

+0

嗯,是这样的。你知道要修复它吗? – Squiddie

+0

使用预乘alpha,绘制到FrameBuffer,然后在混合关闭的情况下绘制FrameBuffer进行屏幕显示,或者在保存之前手动替换像素图中每个像素的Alpha。 – Tenfour04

回答

3

我的问题已回答2017年2月23日作者Tenfour04但由于他没有兴趣发布他的解决方案作为答案,我正在做它来解决这个问题。非常感谢他。我所做的是设置在ByteBuffer每第四个元素(阿尔法值)getPixels()回到(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; 
} 
+0

非常感谢。我只是有同样的问题。 :) – Joschua