Squ*_*die 5 java screenshot libgdx
我遇到了一个在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);
}
Run Code Online (Sandbox Code Playgroud)
这是截取屏幕截图的代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
我去研究,我只发现了这个话题,这是完全相同的问题.但它的信息略少,没有答案.我希望你们中的某个人能够回答这个谜团.
小智 4
Tenfour04于2017 年 2 月 23 日回答了我的问题,但由于他没有兴趣发布他的解决方案作为答案,所以我这样做是为了解决这个问题。非常感谢他。我所做的是将返回的每四个元素(alpha 值)设置为(不透明),这是我的结果:ByteBuffergetPixels()(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;
}
Run Code Online (Sandbox Code Playgroud)