Java 屏幕截图例程提供一个带有LibGdx的空白图像
我想做一个屏幕截图保护程序。我将代码用作基础,因此生成的代码如下所示:Java 屏幕截图例程提供一个带有LibGdx的空白图像,java,libgdx,Java,Libgdx,我想做一个屏幕截图保护程序。我将代码用作基础,因此生成的代码如下所示: public void update(float deltaTime) { if(Gdx.input.isKeyPressed(Keys.ESCAPE)) { Gdx.app.exit(); } if(Gdx.input.isKeyPressed(Keys.F10)) { this.saveScreenshot(new FileH
public void update(float deltaTime) {
if(Gdx.input.isKeyPressed(Keys.ESCAPE)) {
Gdx.app.exit();
}
if(Gdx.input.isKeyPressed(Keys.F10)) {
this.saveScreenshot(new FileHandle(new File("screenshots/screenShot001.png")));
}
}
public void saveScreenshot(FileHandle file) {
Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
PixmapIO.writePNG(file, pixmap);
pixmap.dispose();
}
public Pixmap getScreenshot(int x, int y, int w, int h, boolean flipY) {
Gdx.gl.glPixelStorei(GL10.GL_PACK_ALIGNMENT, 1);
final Pixmap pixmap = new Pixmap(w, h, Format.RGBA8888);
ByteBuffer pixels = pixmap.getPixels();
Gdx.gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, pixels);
final int numBytes = w * h * 4;
byte[] lines = new byte[numBytes];
if (flipY) {
final 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);
} else {
pixels.clear();
pixels.get(lines);
}
return pixmap;
}
public void更新(float deltaTime){
if(Gdx.input.isKeyPressed(Keys.ESCAPE)){
Gdx.app.exit();
}
如果(Gdx.input.isKeyPressed(键F10)){
这个.saveScreenshot(新文件句柄(新文件(“screenshots/screenShot001.png”));
}
}
public void保存屏幕截图(FileHandle文件){
Pixmap Pixmap=getScreenshot(0,0,Gdx.graphics.getWidth(),Gdx.graphics.getHeight(),true);
writePNG(文件,pixmap);
dispose();
}
公共Pixmap getScreenshot(整数x、整数y、整数w、整数h、布尔flipY){
Gdx.gl.glPixelStorei(GL10.gl\u-PACK\u-ALIGNMENT,1);
最终Pixmap Pixmap=新的Pixmap(w,h,Format.rgba888);
ByteBuffer pixels=pixmap.getPixels();
glReadPixels(x,y,w,h,GL10.gl_RGBA,GL10.gl_无符号字节,像素);
最终整数单位=w*h*4;
字节[]行=新字节[numBytes];
如果(flipY){
最终整数numBytesPerLine=w*4;
对于(int i=0;i
该文件已创建,它似乎是一个大小正确的PNG图像,但它是一个空白图像。该应用程序是安装ui制作的示例,显示libGDX徽标。你知道这个问题吗?摘自你的评论:
@Override public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
controller.update(Gdx.graphics.getDeltaTime());
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
}
问题是,您需要清除颜色,然后检查输入(并制作屏幕截图),然后渲染徽标
移动
controller.update(Gdx.graphics.getDeltaTime())
在渲染徽标(batch.end()
)之后,在渲染方法的结尾处进行渲染。是否仍在某处渲染徽标?是的,我在我的渲染方法@Override public void render(){Gdx.gl.glClearColor(1,1,1,1);Gdx.gl.glClear(GL20.gl\u COLOR\u BUFFER\u BIT);controller.update中渲染徽标(Gdx.graphics.getDeltaTime());batch.setProjectionMatrix(camera.combined);batch.begin();sprite.draw(batch);batch.end();}