【问题标题】:Libgdx: ScreenUtils.getFrameBufferPixmap returning blank black Pixmap on AndroidLibgdx:ScreenUtils.getFrameBufferPixmap 在 Android 上返回空白黑色 Pixmap
【发布时间】:2015-03-05 01:51:17
【问题描述】:

我正在尝试使用 libgdx 在 android 上截取屏幕截图,将其保存为 .PNG 图像,然后使用 Intent 提供共享选项。到目前为止,代码运行没有错误消息并且 Intent 工作正常,但是 .PNG 被保存为完全空白(黑色)图像,尽管它似乎至少具有正确的尺寸。运行桌面项目时保存的 .PNG 文件看起来还不错,并且通过反复试验,我设法将问题根源的搜索范围缩小到 ScreenUtils.getFrameBufferPixmap() 函数。在桌面上,它返回一个包含我的屏幕截图的像素图,就像我预期的那样,但是当我在 android 上运行它时,它返回一个完全空白的像素图。关于可能导致这种情况的任何想法?以下是部分代码:

public void share() {

         try{
                FileHandle fh;
                fh = new FileHandle(Gdx.files.getExternalStoragePath() + "TrailBlazerScores" + ".png");
                System.out.println(fh);
                fh.delete();
                Pixmap pixmap = getScreenshot((Gdx.graphics.getWidth()-WIDTH)/2, 0, WIDTH, HEIGHT, true);
                PixmapIO.writePNG(fh, pixmap);
                pixmap.dispose();
            }catch (Exception e){
                System.out.println(e);
            }
        this.actionResolver.shareScreenshot(fh.toString()); //This runs the android-specific Intent code
}

private static Pixmap getScreenshot(int x, int y, int w, int h, boolean yDown){
    final Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(x, y, w, h);
    ByteBuffer pixels = pixmap.getPixels();
    final int numBytes = w * h * 4;
    byte[] lines = new byte[numBytes];
    if (yDown) {
        // Flip the pixmap upside down
        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);
    }
    pixmap.setColor(Color.RED);
    pixmap.fillCircle(0, 0, 60);
    return pixmap;
}

我在返回之前在像素图上绘制的圆圈只是为了查看问题是在保存文件还是在构建像素图。 android 创建的文件除了左上角的红色圆圈外完全是黑色的,所以我知道 PixmapIO 工作正常。

【问题讨论】:

  • 尝试注释掉所有的换行代码。 (立即返回像素图。)另外,您的渲染器是否在做任何有趣或不寻常的事情?
  • @P.T.我已经尝试过了 - Pixmap 仍然是空白的。我使用 scene2d 和 SpriteBatch 的普通 draw() 方法来渲染我所有的东西,所以我很确定那里什么都没有。
  • 您可以尝试在渲染循环中调用它时四处移动。你用的是什么设备? (哪个 GPU?)Android 日志中有什么有趣的东西吗? (你的代码看起来不错,和我的很相似......)
  • @P.T.我已经尝试在渲染循环中移动它,但是在所有元素都渲染到屏幕之前,我只能放置这么多地方而不调用它。我正在三星 Galaxy SII Skyrocket 上进行测试,它有一个 Adreno 220。不过,我没有足够的硬件知识来开始理解这是否是问题所在。
  • 您可以尝试检查 GLErrors 吗? (请务必在屏幕截图之前检查错误,因为它们仍然存在。)您的设备看起来足够健全。您可以尝试使用原始 glReadPixels 调用,看看是否可以更改其读取格式或其他内容?

标签: android image file-io libgdx screenshot


【解决方案1】:

我没有对 libgdx atm 的访问权限,但如果您有 libgdx,那么您也可以访问 lwgjl natives,因为它包含在 libgdx 中。

这是我之前在 lwgjl-natives 中编造的。

 private void screenShot(){
             //Creating an rbg array of total pixels
             int[] pixels = new int[WIDTH * HEIGHT];
             int bindex;
             // allocate space for RBG pixels
             ByteBuffer fb = ByteBuffer.allocateDirect(WIDTH * HEIGHT * 3);

             // grab a copy of the current frame contents as RGB
             glReadPixels(0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, fb);

             BufferedImage imageIn = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);
             // convert RGB data in ByteBuffer to integer array
             for (int i=0; i < pixels.length; i++) {
                 bindex = i * 3;
                 pixels[i] =
                     ((fb.get(bindex) << 16))  +
                     ((fb.get(bindex+1) << 8))  +
                     ((fb.get(bindex+2) << 0));
             }
             //Allocate colored pixel to buffered Image
             imageIn.setRGB(0, 0, WIDTH, HEIGHT, pixels, 0 , WIDTH);

             //Creating the transformation direction (horizontal)
             AffineTransform at =  AffineTransform.getScaleInstance(1, -1);
             at.translate(0, -imageIn.getHeight(null));

             //Applying transformation
             AffineTransformOp opRotated = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
             BufferedImage imageOut = opRotated.filter(imageIn, null);

             try {//Try to screate image, else show exception.
                 ImageIO.write(imageOut, format , fileLoc);
             }
             catch (Exception e) {
                 System.out.println("ScreenShot() exception: " +e);
             }
         }

【讨论】:

    猜你喜欢
    • 2011-02-26
    • 1970-01-01
    • 2015-01-06
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 2016-01-05
    • 2014-06-02
    • 1970-01-01
    相关资源
    最近更新 更多