2016-12-29 80 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