Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
libGdx中的捕获屏幕_Libgdx - Fatal编程技术网

libGdx中的捕获屏幕

libGdx中的捕获屏幕,libgdx,Libgdx,我想在libGdx中捕获屏幕。我用这个代码解决我的问题。在桌面上,它可以工作。但当我在android上运行时,我找不到图像捕获屏幕。我怎样才能修好它?谢谢你阅读我的问题 public class ScreenshotFactory { private static int counter = 1; public static void saveScreenshot() { try { FileHandle fh; do { i

我想在libGdx中捕获屏幕。我用这个代码解决我的问题。在桌面上,它可以工作。但当我在android上运行时,我找不到图像捕获屏幕。我怎样才能修好它?谢谢你阅读我的问题

public class ScreenshotFactory {

private static int counter = 1;

public static void saveScreenshot() {
    try {
        FileHandle fh;
        do {

            if (Gdx.app.getType() == ApplicationType.Desktop)
                Infor.linkScreenShot = "D://chupngoc" + counter + ".png";
            else
                Infor.linkScreenShot = Gdx.files.getExternalStoragePath()
                        + counter + ".png";
            Infor.nameImage = counter + ".png";
            fh = new FileHandle(Infor.linkScreenShot);
            counter++;
        } while (fh.exists());

        Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(),
                Gdx.graphics.getHeight(), true);
        PixmapIO.writePNG(fh, pixmap);
        pixmap.dispose();
    } catch (Exception e) {
    }
}

private static Pixmap getScreenshot(int x, int y, int w, int h,
        boolean yDown) {

    Gdx.gl.glPixelStorei(GL20.GL_PACK_ALIGNMENT, 1);

    final Pixmap pixmap1 = new Pixmap(w, h, Format.RGBA8888);
    ByteBuffer pixels1 = pixmap1.getPixels();
    Gdx.gl.glReadPixels(x, y, w, h, GL20.GL_RGBA, GL20.GL_UNSIGNED_BYTE,
            pixels1);

    Pixmap pixmap = pixmap1;

    if (yDown) {
        // 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;
}
公共类屏幕快照工厂{
专用静态整数计数器=1;
公共静态void保存屏幕截图(){
试一试{
文件句柄fh;
做{
if(Gdx.app.getType()==ApplicationType.Desktop)
Infor.linkScreenShot=“D://chupngoc”+counter+”.png”;
其他的
Infor.linkScreenShot=Gdx.files.getExternalStoragePath()
+计数器+“.png”;
Infor.nameImage=counter+“.png”;
fh=新文件句柄(信息链接屏幕截图);
计数器++;
}而(fh.exists());
Pixmap Pixmap=getScreenshot(0,0,Gdx.graphics.getWidth(),
Gdx.graphics.getHeight(),true);
PixmapIO.writePNG(fh,pixmap);
dispose();
}捕获(例外e){
}
}
私有静态Pixmap getScreenshot(整数x,整数y,整数w,整数h,
布尔值(向下){
Gdx.gl.glPixelStorei(GL20.gl\u-PACK\u-ALIGNMENT,1);
最终Pixmap pixmap1=新的Pixmap(w,h,Format.rgba888);
ByteBuffer pixels1=pixmap1.getPixels();
Gdx.gl.glReadPixels(x,y,w,h,GL20.gl_RGBA,GL20.gl_无符号_字节,
像素1);
Pixmap-Pixmap=pixmap1;
如果(yDown){
//将像素贴图翻转过来
ByteBuffer pixels=pixmap.getPixels();
int numBytes=w*h*4;
字节[]行=新字节[numBytes];
int numBytesPerLine=w*4;
对于(int i=0;i

}你的问题就在这里

fh = new FileHandle(Infor.linkScreenShot);
这种创建文件句柄的方法仅适用于桌面,在Android上不起作用

您应该使用
Gdx.files.external
创建一个文件,而不是

fh = Gdx.files.external(Infor.linkScreenShot);

很抱歉我修好了。我在Android Mainfest上添加权限