截取 Android OpenGL 的屏幕截图 [英] Taking screenshot of Android OpenGL

查看:38
本文介绍了截取 Android OpenGL 的屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试截取 Android OpenGL 的屏幕截图.

I'm trying to take a screenshot of Android OpenGL.

我找到的代码如下:

nt size = width * height;
    ByteBuffer buf = ByteBuffer.allocateDirect(size * 4);
    buf.order(ByteOrder.nativeOrder());
    glContext.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, buf);
    int data[] = new int[size];
    buf.asIntBuffer().get(data);
    buf = null;
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    bitmap.setPixels(data, size-width, -width, 0, 0, width, height);
    data = null;

    short sdata[] = new short[size];
    ShortBuffer sbuf = ShortBuffer.wrap(sdata);
    bitmap.copyPixelsToBuffer(sbuf);
    for (int i = 0; i < size; ++i) {
        //BGR-565 to RGB-565
        short v = sdata[i];
        sdata[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
    }
    sbuf.rewind();
    bitmap.copyPixelsFromBuffer(sbuf);

    try {
        FileOutputStream fos = new FileOutputStream("/sdcard/screeshot.png");
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
    } catch (Exception e) {
        // handle
    }

我还尝试了该站点的代码链接文字

I tried also a code from that site link text

在每种情况下,结果都是一个完全黑色的 png 文件.我发现 glReadPixels 方法存在一些问题,但我不知道如何绕过它.

In each case the result is a png file which is completely black. I found there is some problem with glReadPixels method but I don't know how to bypass it.

推荐答案

抱歉回复晚了...

为了执行正确的屏幕截图,您必须将以下代码放入您的 onDrawFrame(GL10 gl) 处理程序:

In order to perform a correct screenshot You have to put into Your onDrawFrame(GL10 gl) handler the following code:

if(screenshot){                     
                int screenshotSize = width * height;
                ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
                bb.order(ByteOrder.nativeOrder());
                gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
                int pixelsBuffer[] = new int[screenshotSize];
                bb.asIntBuffer().get(pixelsBuffer);
                bb = null;
                Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
                bitmap.setPixels(pixelsBuffer, screenshotSize-width, -width, 0, 0, width, height);
                pixelsBuffer = null;

                short sBuffer[] = new short[screenshotSize];
                ShortBuffer sb = ShortBuffer.wrap(sBuffer);
                bitmap.copyPixelsToBuffer(sb);

                //Making created bitmap (from OpenGL points) compatible with Android bitmap
                for (int i = 0; i < screenshotSize; ++i) {                  
                    short v = sBuffer[i];
                    sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
                }
                sb.rewind();
                bitmap.copyPixelsFromBuffer(sb);
                lastScreenshot = bitmap;

                screenshot = false;
            }

每当用户按下按钮创建屏幕截图时,屏幕截图"类字段设置为 true或在您想要的任何其他情况下.在if"正文中您可以放置​​在互联网上找到的任何屏幕截图创建代码示例 - 最重要的是拥有当前的 GL10 实例.例如,当您将 GL10 实例保存到类变量中,然后在事件之外使用它来创建屏幕截图时,您最终会得到完全空白的图像.这就是为什么您必须在 GL10 实例是当前实例的 OnDrawFrame 事件处理程序中截取屏幕截图的原因.希望对您有所帮助.

The "screenshot" class field is set to true whenever the user presses the button to create a screenshot or at any other circumstances You want. Inside the "if" body You may place any screenshot creating code sample You find in th internet - the most important thing is having the current instance of GL10. For example when You just save the GL10 instance to the class variable and then use it outside the event to create the screenshot You'll end up with the completely blank image. That's why You have to take a screenshot inside the OnDrawFrame event handler where the GL10 instance is the current one. Hope that it helps.

最好的问候,戈登.

这篇关于截取 Android OpenGL 的屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆