12.3、Libgdx的图像之截屏

简介: (官网:www.libgdx.cn) 接下来的示例说明怎样进行截屏并且保存为PNG格式。 public class ScreenshotFactory { private static int counter = 1; public st...

(官网:www.libgdx.cn

接下来的示例说明怎样进行截屏并且保存为PNG格式。

public class ScreenshotFactory {
private static int counter = 1;
public static void saveScreenshot(){
    try{
        FileHandle fh;
        do{
            fh = new FileHandle("screenshot" + counter++ + ".png");
        }while (fh.exists());
        Pixmap pixmap = getScreenshot(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
        PixmapIO.writePNG(fh, pixmap);
        pixmap.dispose();
    }catch (Exception e){           
    }
}

private static Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown){
    final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);
    if (yDown) {
        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;
}
}

www.libgdx.cn版权所有,如需转载,注明出处)

目录
相关文章
|
1月前
|
编解码 监控 算法
如何使用Pillow库进行拍照和截屏
如何使用Pillow库进行拍照和截屏
22 0
|
4月前
|
XML Java Android开发
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
Android Studio App开发之使用相机拍摄照片和从相册中选取图片(附源码 超详细必看)
155 0
|
4月前
|
XML 算法 Java
Android Studio App开发之利用图像解码器ImageDecoder播放GIF动图、Webp、HEIF图片(附源码 简单实用)
Android Studio App开发之利用图像解码器ImageDecoder播放GIF动图、Webp、HEIF图片(附源码 简单实用)
104 0
|
11月前
|
Android开发
图标提取,一键完成,再也不用截屏抠图了!
图标提取,一键完成,再也不用截屏抠图了!
|
文字识别 算法 Swift
毕业设计在iOS上使用OpenCV实现图片中的文字框选文字识别
毕业设计在iOS上使用OpenCV实现图片中的文字框选文字识别
464 0
毕业设计在iOS上使用OpenCV实现图片中的文字框选文字识别
|
iOS开发 MacOS Windows
Unity2D像素游戏开发——Aseprite简单人物绘画+动画制作导出精灵表示例
Unity2D像素游戏开发——Aseprite简单人物绘画+动画制作导出精灵表示例
503 0
Unity2D像素游戏开发——Aseprite简单人物绘画+动画制作导出精灵表示例
|
图形学
Unity3d 截屏
Unity3d 截屏
73 0
|
前端开发
前端必备的截屏取色小工具推荐——Faststone Capture
前端必备的截屏取色小工具推荐——Faststone Capture
前端必备的截屏取色小工具推荐——Faststone Capture
|
Java Android开发
autojs-camera2-照相机-实现单拍和连拍
autojs-camera2-照相机-实现单拍和连拍
899 0
|
前端开发 Go Android开发
autojs量角器-相机背景
牙叔教程 简单易懂
97 0