将帧缓冲区保存到图像文件可以在桌面上使用,但不能 android

Save framebuffer to image file works on desktop but not android

我正在尝试绘制到屏幕外帧缓冲区,然后将其保存到文件中。这适用于桌面,但在 android 上它会产生一个空图像。没有抛出任何错误提示可能出了什么问题。下面是我正在使用的代码。任何人都知道为什么这不适用于 android?

private void saveOffscreenImage() {
    final int w = 256;
    final int h = 256;

    final OrthographicCamera camera = new OrthographicCamera(w, h);
    camera.setToOrtho(true, w, h);

    final Batch batch = new SpriteBatch();
    batch.setProjectionMatrix(camera.combined);

    final FrameBuffer fb = new FrameBuffer(Pixmap.Format.RGBA8888, w, h, false);

    fb.begin();

    batch.begin();
    Gdx.gl.glClearColor(1f, 0f, 0f, 1f);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // draw a texture
    batch.draw(img, 0, 0, 128, 128);
    batch.end();

    final Pixmap pixmap = new Pixmap(w, h, Pixmap.Format.RGB888);
    final ByteBuffer buf = pixmap.getPixels();
    Gdx.gl.glReadPixels(0, 0, w, h, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, buf);

    // Prints error code: 1282
    Gdx.app.log("!", "error: " + Gdx.gl.glGetError());

    fb.end();

    final FileHandle file = Gdx.files.absolute("path/test.png");
    PixmapIO.writePNG(file, pixmap);
}

看来你也必须阅读 alpha。

'''GL_RGB''' 应该是 '''GL_RGBA'''

glReadPixel returns zeros and error 1282 (Android)